Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 132 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
tao_actions_form_Users | |
0.00% |
0 / 132 |
|
0.00% |
0 / 6 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
getUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
initForm | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
initElements | |
0.00% |
0 / 80 |
|
0.00% |
0 / 1 |
110 | |||
initLoginElement | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
getSanitizerRegexValidator | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * 2022 (original work) Open Assessment Technologies SA. |
23 | */ |
24 | |
25 | use oat\generis\model\OntologyRdfs; |
26 | use oat\generis\model\user\UserRdf; |
27 | use oat\oatbox\service\ServiceManager; |
28 | use oat\tao\helpers\ApplicationHelper; |
29 | use oat\tao\model\controller\SignedFormInstance; |
30 | use oat\oatbox\user\UserLanguageServiceInterface; |
31 | use oat\generis\model\user\PasswordConstraintsService; |
32 | |
33 | /** |
34 | * This container initialize the user edition form. |
35 | * |
36 | * @author Joel Bout, <joel.bout@tudor.lu> |
37 | */ |
38 | class tao_actions_form_Users extends SignedFormInstance |
39 | { |
40 | /** @var core_kernel_classes_Resource */ |
41 | protected $user; |
42 | |
43 | /** @var string */ |
44 | protected $formName = ''; |
45 | |
46 | /** |
47 | * Short description of method __construct |
48 | * |
49 | * @access public |
50 | * @author Joel Bout, <joel.bout@tudor.lu> |
51 | * |
52 | * @param core_kernel_classes_Class $clazz |
53 | * @param core_kernel_classes_Resource $user |
54 | * @param boolean $forceAdd |
55 | * @param array $options |
56 | * |
57 | * @throws common_exception_Error |
58 | */ |
59 | public function __construct( |
60 | core_kernel_classes_Class $clazz, |
61 | core_kernel_classes_Resource $user = null, |
62 | $forceAdd = false, |
63 | $options = [] |
64 | ) { |
65 | if (empty($clazz)) { |
66 | throw new Exception('Set the user class in the parameters'); |
67 | } |
68 | |
69 | $this->formName = 'user_form'; |
70 | |
71 | $service = tao_models_classes_UserService::singleton(); |
72 | if (!empty($user)) { |
73 | $this->user = $user; |
74 | $options['mode'] = 'edit'; |
75 | } else { |
76 | if (isset($_POST[$this->formName . '_sent']) && isset($_POST['uri'])) { |
77 | $this->user = new core_kernel_classes_Resource(tao_helpers_Uri::decode($_POST['uri'])); |
78 | } else { |
79 | $this->user = $service->createInstance($clazz, $service->createUniqueLabel($clazz)); |
80 | } |
81 | $options['mode'] = 'add'; |
82 | } |
83 | |
84 | if ($forceAdd) { |
85 | $options['mode'] = 'add'; |
86 | } |
87 | |
88 | $userLangService = \oat\oatbox\service\ServiceManager::getServiceManager()->get( |
89 | UserLanguageServiceInterface::class |
90 | ); |
91 | if (!$userLangService->isDataLanguageEnabled()) { |
92 | $options['excludedProperties'][] = UserRdf::PROPERTY_DEFLG; |
93 | } |
94 | |
95 | $options['topClazz'] = UserRdf::CLASS_URI; |
96 | |
97 | parent::__construct($clazz, $this->user, $options); |
98 | } |
99 | |
100 | /** |
101 | * Short description of method getUser |
102 | * |
103 | * @access public |
104 | * @author Joel Bout, <joel.bout@tudor.lu> |
105 | * @return core_kernel_classes_Resource |
106 | */ |
107 | public function getUser() |
108 | { |
109 | return $this->user; |
110 | } |
111 | |
112 | /** |
113 | * Short description of method initForm |
114 | * |
115 | * @access protected |
116 | * @author Joel Bout, <joel.bout@tudor.lu> |
117 | * @return mixed |
118 | */ |
119 | protected function initForm() |
120 | { |
121 | parent::initForm(); |
122 | |
123 | $this->form->setName($this->formName); |
124 | |
125 | $actions = tao_helpers_form_FormFactory::getCommonActions('top'); |
126 | $this->form->setActions($actions, 'top'); |
127 | $this->form->setActions($actions, 'bottom'); |
128 | } |
129 | |
130 | /** |
131 | * Short description of method initElements |
132 | * |
133 | * @access protected |
134 | * @author Joel Bout, <joel.bout@tudor.lu> |
135 | */ |
136 | protected function initElements() |
137 | { |
138 | if (!isset($this->options['mode'])) { |
139 | throw new Exception('Please set a mode into container options'); |
140 | } |
141 | |
142 | parent::initElements(); |
143 | |
144 | $this->initLoginElement(); |
145 | |
146 | //set default lang to the languages fields |
147 | $langService = tao_models_classes_LanguageService::singleton(); |
148 | $userLangService = \oat\oatbox\service\ServiceManager::getServiceManager()->get( |
149 | UserLanguageServiceInterface::class |
150 | ); |
151 | if ($userLangService->isDataLanguageEnabled()) { |
152 | $dataLangElt = $this->form->getElement(tao_helpers_Uri::encode(UserRdf::PROPERTY_DEFLG)); |
153 | $dataLangElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
154 | $dataUsage = new core_kernel_classes_Resource( |
155 | tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_DATA |
156 | ); |
157 | $dataOptions = []; |
158 | foreach ($langService->getAvailableLanguagesByUsage($dataUsage) as $lang) { |
159 | $dataOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); |
160 | } |
161 | $dataLangElt->setOptions($dataOptions); |
162 | } |
163 | |
164 | $uiLangElt = $this->form->getElement(tao_helpers_Uri::encode(UserRdf::PROPERTY_UILG)); |
165 | $uiLangElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
166 | $guiUsage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI); |
167 | $guiOptions = []; |
168 | foreach ($langService->getAvailableLanguagesByUsage($guiUsage) as $lang) { |
169 | $guiOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); |
170 | } |
171 | $uiLangElt->setOptions($guiOptions); |
172 | |
173 | // roles field |
174 | $property = new core_kernel_classes_Property(UserRdf::PROPERTY_ROLES); |
175 | $roles = $property->getRange()->getInstances(true); |
176 | $rolesOptions = []; |
177 | foreach ($roles as $r) { |
178 | $rolesOptions[tao_helpers_Uri::encode($r->getUri())] = $r->getLabel(); |
179 | } |
180 | asort($rolesOptions); |
181 | |
182 | $userService = tao_models_classes_UserService::singleton(); |
183 | $rolesOptions = $userService->getPermittedRoles($userService->getCurrentUser(), $rolesOptions); |
184 | |
185 | $rolesElt = $this->form->getElement(tao_helpers_Uri::encode($property->getUri())); |
186 | $rolesElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
187 | $rolesElt->setOptions($rolesOptions); |
188 | |
189 | // password field |
190 | $this->form->removeElement(tao_helpers_Uri::encode(UserRdf::PROPERTY_PASSWORD)); |
191 | |
192 | if ($this->options['mode'] === 'add') { |
193 | $pass1Element = tao_helpers_form_FormFactory::getElement('password1', 'Hiddenbox'); |
194 | $pass1Element->setDescription(__('Password')); |
195 | $pass1Element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
196 | $pass1Element->addValidators(PasswordConstraintsService::singleton()->getValidators()); |
197 | $pass1Element->setBreakOnFirstError(false); |
198 | |
199 | $this->form->addElement($pass1Element); |
200 | |
201 | $pass2Element = tao_helpers_form_FormFactory::getElement('password2', 'Hiddenbox'); |
202 | $pass2Element->setDescription(__('Repeat password')); |
203 | $pass2Element->addValidators([ |
204 | tao_helpers_form_FormFactory::getValidator('NotEmpty'), |
205 | tao_helpers_form_FormFactory::getValidator('Password', ['password2_ref' => $pass1Element]), |
206 | ]); |
207 | $this->form->addElement($pass2Element); |
208 | } else { |
209 | if (ApplicationHelper::isDemo()) { |
210 | $warning = tao_helpers_form_FormFactory::getElement('warningpass', 'Label'); |
211 | $warning->setValue(__('Unable to change passwords in demo mode')); |
212 | $this->form->addElement($warning); |
213 | $this->form->createGroup("pass_group", __("Change the password"), ['warningpass']); |
214 | } else { |
215 | $pass2Element = tao_helpers_form_FormFactory::getElement('password2', 'Hiddenbox'); |
216 | $pass2Element->setDescription(__('New password')); |
217 | $pass2Element->addValidators(PasswordConstraintsService::singleton()->getValidators()); |
218 | $pass2Element->setBreakOnFirstError(false); |
219 | $this->form->addElement($pass2Element); |
220 | |
221 | $pass3Element = tao_helpers_form_FormFactory::getElement('password3', 'Hiddenbox'); |
222 | $pass3Element->setDescription(__('Repeat new password')); |
223 | $pass3Element->addValidators([ |
224 | tao_helpers_form_FormFactory::getValidator('Password', ['password2_ref' => $pass2Element]), |
225 | ]); |
226 | $this->form->addElement($pass3Element); |
227 | |
228 | $this->form->createGroup("pass_group", __("Change the password"), ['password2', 'password3']); |
229 | if (empty($_POST[$pass2Element->getName()]) && empty($_POST[$pass3Element->getName()])) { |
230 | $pass2Element->setForcedValid(); |
231 | $pass3Element->setForcedValid(); |
232 | } |
233 | } |
234 | } |
235 | |
236 | $this->addSanitizerValidator( |
237 | $this->getSanitizerRegexValidator(), |
238 | [ |
239 | OntologyRdfs::RDFS_LABEL, |
240 | UserRdf::PROPERTY_LOGIN, |
241 | UserRdf::PROPERTY_FIRSTNAME, |
242 | UserRdf::PROPERTY_LASTNAME, |
243 | ] |
244 | ); |
245 | } |
246 | |
247 | private function initLoginElement(): void |
248 | { |
249 | /** @var tao_helpers_form_FormElement $element */ |
250 | $element = $this->form->getElement(tao_helpers_Uri::encode(UserRdf::PROPERTY_LOGIN)); |
251 | |
252 | $element->feedInputValue(); |
253 | $value = $element->getInputValue() ?? $element->getRawValue(); |
254 | |
255 | if ($this->options['mode'] !== 'add' && $this->getSanitizerRegexValidator()->evaluate($value)) { |
256 | $element->setAttributes( |
257 | [ |
258 | 'readonly' => 'readonly', |
259 | 'disabled' => 'disabled', |
260 | ] |
261 | ); |
262 | |
263 | return; |
264 | } |
265 | |
266 | $element->addValidators([ |
267 | tao_helpers_form_FormFactory::getValidator('NotEmpty'), |
268 | tao_helpers_form_FormFactory::getValidator( |
269 | 'Callback', |
270 | [ |
271 | 'object' => tao_models_classes_UserService::singleton(), |
272 | 'method' => 'loginAvailable', |
273 | 'message' => __('This Login is already in use'), |
274 | ] |
275 | ) |
276 | ]); |
277 | } |
278 | |
279 | private function getSanitizerRegexValidator(): tao_helpers_form_Validator |
280 | { |
281 | return ServiceManager::getServiceManager()->getContainer()->get( |
282 | tao_helpers_form_validators_Regex::USER_FORM_SERVICE_ID |
283 | ); |
284 | } |
285 | } |