* @package tao */ class tao_actions_form_Users extends SignedFormInstance { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute user * * @access protected * @var Resource */ protected $user; /** * Short description of attribute formName * * @access protected * @var string */ protected $formName = ''; // --- OPERATIONS --- /** * Short description of method __construct * * @access public * @author Joel Bout, * * @param core_kernel_classes_Class $clazz * @param core_kernel_classes_Resource $user * @param boolean $forceAdd * @param array $options * * @throws common_exception_Error */ public function __construct( core_kernel_classes_Class $clazz, core_kernel_classes_Resource $user = null, $forceAdd = false, $options = [] ) { if (empty($clazz)) { throw new Exception('Set the user class in the parameters'); } $this->formName = 'user_form'; $service = tao_models_classes_UserService::singleton(); if (!empty($user)) { $this->user = $user; $options['mode'] = 'edit'; } else { if (isset($_POST[$this->formName . '_sent']) && isset($_POST['uri'])) { $this->user = new core_kernel_classes_Resource(tao_helpers_Uri::decode($_POST['uri'])); } else { $this->user = $service->createInstance($clazz, $service->createUniqueLabel($clazz)); } $options['mode'] = 'add'; } if ($forceAdd) { $options['mode'] = 'add'; } $userLangService = \oat\oatbox\service\ServiceManager::getServiceManager()->get(UserLanguageServiceInterface::class); if (!$userLangService->isDataLanguageEnabled()) { $options['excludedProperties'][] = UserRdf::PROPERTY_DEFLG; } $options['topClazz'] = GenerisRdf::CLASS_GENERIS_USER; parent::__construct($clazz, $this->user, $options); } /** * Short description of method getUser * * @access public * @author Joel Bout, * @return core_kernel_classes_Resource */ public function getUser() { return $this->user; } /** * Short description of method initForm * * @access protected * @author Joel Bout, * @return mixed */ protected function initForm() { parent::initForm(); $this->form->setName($this->formName); $actions = tao_helpers_form_FormFactory::getCommonActions('top'); $this->form->setActions($actions, 'top'); $this->form->setActions($actions, 'bottom'); } /** * Short description of method initElements * * @access protected * @author Joel Bout, */ protected function initElements() { if (!isset($this->options['mode'])) { throw new Exception("Please set a mode into container options "); } parent::initElements(); //login field $loginElement = $this->form->getElement(tao_helpers_Uri::encode(GenerisRdf::PROPERTY_USER_LOGIN)); if ($this->options['mode'] === 'add') { $loginElement->addValidators([ tao_helpers_form_FormFactory::getValidator('NotEmpty'), tao_helpers_form_FormFactory::getValidator('Callback', [ 'object' => tao_models_classes_UserService::singleton(), 'method' => 'loginAvailable', 'message' => __('This Login is already in use') ]) ]); } else { $loginElement->setAttributes(['readonly' => 'readonly', 'disabled' => 'disabled']); } //set default lang to the languages fields $langService = tao_models_classes_LanguageService::singleton(); $userLangService = \oat\oatbox\service\ServiceManager::getServiceManager()->get(UserLanguageServiceInterface::class); if ($userLangService->isDataLanguageEnabled()) { $dataLangElt = $this->form->getElement(tao_helpers_Uri::encode(GenerisRdf::PROPERTY_USER_DEFLG)); $dataLangElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $dataUsage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_DATA); $dataOptions = []; foreach ($langService->getAvailableLanguagesByUsage($dataUsage) as $lang) { $dataOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); } $dataLangElt->setOptions($dataOptions); } $uiLangElt = $this->form->getElement(tao_helpers_Uri::encode(GenerisRdf::PROPERTY_USER_UILG)); $uiLangElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $guiUsage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI); $guiOptions = []; foreach ($langService->getAvailableLanguagesByUsage($guiUsage) as $lang) { $guiOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); } $uiLangElt->setOptions($guiOptions); // roles field $property = new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_ROLES); $roles = $property->getRange()->getInstances(true); $rolesOptions = []; foreach ($roles as $r) { $rolesOptions[tao_helpers_Uri::encode($r->getUri())] = $r->getLabel(); } asort($rolesOptions); $userService = tao_models_classes_UserService::singleton(); $rolesOptions = $userService->getPermittedRoles($userService->getCurrentUser(), $rolesOptions); $rolesElt = $this->form->getElement(tao_helpers_Uri::encode($property->getUri())); $rolesElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $rolesElt->setOptions($rolesOptions); // password field $this->form->removeElement(tao_helpers_Uri::encode(GenerisRdf::PROPERTY_USER_PASSWORD)); if ($this->options['mode'] === 'add') { $pass1Element = tao_helpers_form_FormFactory::getElement('password1', 'Hiddenbox'); $pass1Element->setDescription(__('Password')); $pass1Element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $pass1Element->addValidators(PasswordConstraintsService::singleton()->getValidators()); $pass1Element->setBreakOnFirstError(false); $this->form->addElement($pass1Element); $pass2Element = tao_helpers_form_FormFactory::getElement('password2', 'Hiddenbox'); $pass2Element->setDescription(__('Repeat password')); $pass2Element->addValidators([ tao_helpers_form_FormFactory::getValidator('NotEmpty'), tao_helpers_form_FormFactory::getValidator('Password', ['password2_ref' => $pass1Element]), ]); $this->form->addElement($pass2Element); } else { if (ApplicationHelper::isDemo()) { $warning = tao_helpers_form_FormFactory::getElement('warningpass', 'Label'); $warning->setValue(__('Unable to change passwords in demo mode')); $this->form->addElement($warning); $this->form->createGroup("pass_group", __("Change the password"), ['warningpass']); } else { $pass2Element = tao_helpers_form_FormFactory::getElement('password2', 'Hiddenbox'); $pass2Element->setDescription(__('New password')); $pass2Element->addValidators(PasswordConstraintsService::singleton()->getValidators()); $pass2Element->setBreakOnFirstError(false); $this->form->addElement($pass2Element); $pass3Element = tao_helpers_form_FormFactory::getElement('password3', 'Hiddenbox'); $pass3Element->setDescription(__('Repeat new password')); $pass3Element->addValidators([ tao_helpers_form_FormFactory::getValidator('Password', ['password2_ref' => $pass2Element]), ]); $this->form->addElement($pass3Element); $this->form->createGroup("pass_group", __("Change the password"), ['password2', 'password3']); if (empty($_POST[$pass2Element->getName()]) && empty($_POST[$pass3Element->getName()])) { $pass2Element->setForcedValid(); $pass3Element->setForcedValid(); } } } } }