Debian Codegraph @

lib/baseType.inc

raw
1 <?php
2
3 use LAM\TYPES\ConfiguredType;
4
5 /*
6
7 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
8 Copyright (C) 2005 - 2026 Roland Gruber
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
24 */
25
26 /**
27 * This is the parent class of all account types.
28 *
29 * @package types
30 * @author Roland Gruber
31 */
32
33 /**
34 * This is the parent class of all account types.
35 *
36 * @package types
37 */
38 class baseType {
39
40 /** label to create another account */
41 public string $LABEL_CREATE_ANOTHER_ACCOUNT;
42 /** label to return to account list */
43 public string $LABEL_BACK_TO_ACCOUNT_LIST;
44
45 /** type configuration */
46 private ?ConfiguredType $type;
47
48 /**
49 * Creates a new type object.
50 *
51 * @param ConfiguredType|null $type configuration
52 */
53 public function __construct(?ConfiguredType $type) {
54 $this->type = $type;
55 $this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another account');
56 $this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to account list');
57 }
58
59 /**
60 * Returns the alias name of this account type.
61 * This function must be overwritten by the child classes.
62 *
63 * @return string alias name
64 */
65 public function getAlias(): string {
66 return "baseType";
67 }
68
69 /**
70 * Returns the description of this account type.
71 * This function must be overwritten by the child classes.
72 *
73 * @return string description
74 */
75 public function getDescription(): string {
76 return "base type";
77 }
78
79 /**
80 * Returns the class name for the list object.
81 *
82 * @return class-string class name
83 */
84 public function getListClassName(): string {
85 return lamList::class;
86 }
87
88 /**
89 * Returns the default attribute list for this account type.
90 * This function must be overwritten by the child classes.
91 *
92 * @return string attribute list
93 */
94 public function getDefaultListAttributes(): string {
95 return "dn:DN,objectClass:Object classes";
96 }
97
98 /**
99 * Returns a list of attributes which have a translated description.
100 * This is used for the head row in the list view.
101 *
102 * @return array<string, string> list of descriptions
103 */
104 public function getListAttributeDescriptions(): array {
105 $descriptions = [];
106 foreach ($this->getType()->getModules() as $moduleName) {
107 $module = moduleCache::getModule($moduleName, $this->getScope());
108 if ($module === null) {
109 continue;
110 }
111 $descriptions = array_merge($descriptions, $module->getListAttributeDescriptions($this->getType()));
112 }
113 return $descriptions;
114 }
115
116 /**
117 * Returns if entries of this type may be created via file upload.
118 *
119 * @return bool true, if file upload is supported
120 */
121 public function supportsFileUpload(): bool {
122 return true;
123 }
124
125 /**
126 * Returns the title text for the title bar on the new/edit page.
127 *
128 * @param accountContainer $container account container
129 * @return string|null title text
130 */
131 public function getTitleBarTitle(accountContainer $container): ?string {
132 if ($container->dn_orig == null) {
133 return null;
134 }
135 return htmlspecialchars(getAbstractDN($container->dn_orig));
136 }
137
138 /**
139 * Returns the title text for the title bar on the new/edit page.
140 *
141 * @param accountContainer $container account container
142 * @return string title text
143 */
144 public function getTitleBarSubtitle(accountContainer $container): string {
145 return '';
146 }
147
148 /**
149 * Builds the HTML code for the icon that shows the account status (locked/unlocked).
150 *
151 * @param accountContainer $container account container
152 * @return htmlElement|null status icon
153 */
154 public function buildAccountStatusIcon(accountContainer $container): ?htmlElement {
155 $statusContent = new htmlGroup();
156 $details = [];
157 $lockableOptions = [];
158 $statusSupported = false;
159 foreach ($container->getAccountModules() as $module) {
160 if (!($module instanceof AccountStatusProvider)) {
161 continue;
162 }
163 $statusSupported = true;
164 $dummyAttributes = null;
165 $details = array_merge($details, $module->getAccountStatusDetails($container->get_type(), $dummyAttributes));
166 $lockableOptions = array_merge($lockableOptions, $module->getAccountStatusPossibleLockOptions($container->get_type(), $dummyAttributes));
167 }
168 if (!$statusSupported) {
169 return null;
170 }
171 $accountStatus = new AccountStatus($details);
172 $mainIcon = 'unlocked.svg';
173 $altText = _('Unlocked');
174 if ($accountStatus->isLocked()) {
175 $mainIcon = 'locked.svg';
176 $altText = _('Locked');
177 }
178 elseif ($accountStatus->isPartiallyLocked()) {
179 $mainIcon = 'partiallyLocked.svg';
180 $altText = _('Partially locked');
181 }
182 elseif ($accountStatus->isExpired()) {
183 $mainIcon = 'expired.svg';
184 $altText = _('Expired');
185 }
186 elseif ($accountStatus->isPartiallyExpired()) {
187 $mainIcon = 'expired.svg';
188 $altText = _('Expired');
189 }
190 $statusIcon = new htmlImage('../../graphics/' . $mainIcon, null, null, $altText);
191 $statusIcon->setCSSClasses(['icon']);
192 if (!empty($accountStatus->getDetails()) || !empty($lockableOptions)) {
193 $detailsTable = new htmlTable();
194 foreach ($accountStatus->getDetails() as $detail) {
195 $label = new htmlOutputText($detail->getDetails());
196 $label->setNoWrap(true);
197 $detailsTable->addElement($label);
198 $statusDetailIcon = new htmlImage('../../graphics/' . $detail->getIcon(), '16px', '16px');
199 $statusDetailIcon->setCSSClasses(['fixed-size-16']);
200 $detailsTable->addElement($statusDetailIcon);
201 $detailsTable->addNewLine();
202 }
203 if (empty($accountStatus->getDetails())) {
204 $detailsTable->addElement(new htmlOutputText(_('Unlocked')));
205 $statusDetailIcon = new htmlImage('../../graphics/unlocked.svg', '16px', '16px');
206 $statusDetailIcon->setCSSClasses(['fixed-size-16']);
207 $detailsTable->addElement($statusDetailIcon);
208 $detailsTable->addNewLine();
209 }
210 $detailsTable->addElement(new htmlSpacer(null, '1rem'), true);
211 $hintGroup = new htmlGroup();
212 $hintGroup->colspan = 2;
213 $hintGroup->addElement(new htmlImage('../../graphics/light.svg', '16px', '16px'));
214 $hintGroup->addElement(new htmlOutputText(_('Please click to lock/unlock this account.')));
215 $detailsTable->addElement($hintGroup);
216 $statusIcon->setHelpData(_('Account status'), $detailsTable);
217 if (checkIfWriteAccessIsAllowed($container->get_type()->getScope())) {
218 $statusIcon->setOnClick("window.lam.dialog.showModal('.modal-account-status')");
219 $statusIcon->setCSSClasses(['clickable', 'icon']);
220 }
221 }
222 $statusContent->addElement($statusIcon);
223 $dialogDiv = $this->buildAccountStatusDialogDiv($accountStatus, $lockableOptions);
224 $statusContent->addElement($dialogDiv);
225 return $statusContent;
226 }
227
228 /**
229 * Builds the dialog to (un)lock parts of an account.
230 *
231 * @param AccountStatus $accountStatus account status
232 * @param AccountStatusDetails[] $lockableOptions possible lock options
233 * @return htmlElement dialog div
234 */
235 private function buildAccountStatusDialogDiv(AccountStatus $accountStatus, array $lockableOptions): htmlElement {
236 $hasLockOptions = !empty($lockableOptions);
237 $hasUnlockOptions = $accountStatus->isLocked() || $accountStatus->isPartiallyLocked() || $accountStatus->isPartiallyExpired();
238 $container = new htmlResponsiveRow();
239 $container->add(new htmlTitle(_('Change account status')));
240
241 // show radio buttons for lock/unlock
242 $radioDisabled = true;
243 $selectedRadio = 'unlock';
244 $onchange = '';
245 if ($hasLockOptions && $hasUnlockOptions) {
246 $radioDisabled = false;
247 $onchange = 'window.lam.accountLists.lockUnlockRadioChange();';
248 }
249 if (!$hasUnlockOptions) {
250 $selectedRadio = 'lock';
251 }
252 if (!$radioDisabled) {
253 $radio = new htmlRadio('lam_accountStatusAction', [_('Lock') => 'lock', _('Unlock') => 'unlock'], $selectedRadio);
254 $radio->setOnchangeEvent($onchange);
255 $container->add($radio);
256 }
257 else {
258 $radio = new htmlRadio('lam_accountStatusActionDisabled', [_('Lock') => 'lock', _('Unlock') => 'unlock'], $selectedRadio);
259 $radio->setIsEnabled(false);
260 $container->add($radio);
261 $container->add(new htmlHiddenInput('lam_accountStatusAction', $selectedRadio));
262 }
263 $container->addVerticalSpacer('1rem');
264
265 $container->add(new htmlHiddenInput('lam_accountStatusResult', 'cancel'));
266
267 // locking part
268 if ($hasLockOptions) {
269 $lockContent = new htmlResponsiveRow();
270 foreach ($lockableOptions as $lockableOption) {
271 $preSelected = $lockableOption->isLocked();
272 $lockContentGroup = new htmlGroup();
273 $image = new htmlImage('../../graphics/' . $lockableOption->getIcon(), '16px', '16px');
274 $image->setCSSClasses(['align-middle']);
275 $lockContentGroup->addElement($image);
276 $checkbox = new htmlInputCheckbox('lam_accountStatusLock_' . $lockableOption->getId(), $preSelected);
277 $checkbox->setCSSClasses(['align-middle']);
278 $lockContentGroup->addElement($checkbox);
279 $label = new htmlLabel('lam_accountStatusLock_' . $lockableOption->getId(), $lockableOption->getDetails());
280 $label->setCSSClasses(['align-middle']);
281 $lockContentGroup->addElement($label);
282 $lockContent->add($lockContentGroup);
283 }
284
285 $lockDiv = new htmlDiv('lam_accountStatusDialogLockDiv', $lockContent);
286 if ($hasUnlockOptions) {
287 $lockDiv->setCSSClasses(['hidden']);
288 }
289 $container->add($lockDiv);
290 }
291 // unlocking part
292 if ($hasUnlockOptions) {
293 $unlockContent = new htmlResponsiveRow();
294 foreach ($accountStatus->getDetails() as $detail) {
295 $unlockContentGroup = new htmlGroup();
296 $image = new htmlImage('../../graphics/' . $detail->getIcon(), '16px', '16px');
297 $image->setCSSClasses(['align-middle']);
298 $unlockContentGroup->addElement($image);
299 $checkbox = new htmlInputCheckbox('lam_accountStatusUnlock_' . $detail->getId(), true);
300 $checkbox->setCSSClasses(['align-middle']);
301 $unlockContentGroup->addElement($checkbox);
302 $label = new htmlLabel('lam_accountStatusUnlock_' . $detail->getId(), $detail->getDetails());
303 $label->setCSSClasses(['align-middle']);
304 $unlockContentGroup->addElement($label);
305 $unlockContent->add($unlockContentGroup);
306 }
307 $unlockDiv = new htmlDiv('lam_accountStatusDialogUnlockDiv', $unlockContent);
308 $container->add($unlockDiv);
309 }
310 $container->addVerticalSpacer('1rem');
311
312 $buttonGroup = new htmlGroup();
313 $okButton = new htmlButton('changeStatusOk', _('Ok'));
314 $okButton->setCSSClasses(['lam-primary']);
315 $okButton->setOnClick("document.getElementById('lam_accountStatusResult').value = 'ok'; document.forms['inputForm'].submit();");
316 $buttonGroup->addElement($okButton);
317 $cancelButton = new htmlButton('changeStatusCancel', _('Cancel'));
318 $cancelButton->setOnClick("document.querySelector('.modal-account-status').classList.remove('show-modal'); return false;");
319 $buttonGroup->addElement($cancelButton);
320 $container->add($buttonGroup);
321
322 $innerDiv = new htmlDiv('lam_accountStatusDialogContent', $container);
323 $innerDiv->setCSSClasses(['modal-content']);
324 $div = new htmlDiv('lam_accountStatusDialog', $innerDiv);
325 $div->setCSSClasses(['modal modal-account-status']);
326 return $div;
327 }
328
329 /**
330 * Returns the LDAP filter to find the possible suffixes for this account type.
331 *
332 * @return string LDAP filter
333 */
334 public function getSuffixFilter(): string {
335 return "(|(objectClass=organizationalunit)(objectClass=country)(objectClass=organization)(objectClass=krbRealmContainer)(objectClass=container)(dc=*))";
336 }
337
338 /**
339 * This function is called after the edit page is processed and before the page content is generated.
340 * This can be used to run custom handlers after each page processing.
341 *
342 * @param accountContainer $container account container
343 */
344 public function runEditPagePostAction(accountContainer $container): void {
345 // check if account status should be changed
346 if (isset($_POST['lam_accountStatusResult']) && ($_POST['lam_accountStatusResult'] == 'ok')) {
347 // lock account
348 if ($_POST['lam_accountStatusAction'] == 'lock') {
349 $lockIds = [];
350 foreach ($_POST as $key => $value) {
351 if (($value === 'on') && (str_starts_with($key, 'lam_accountStatusLock_'))) {
352 $lockIds[] = substr($key, strlen('lam_accountStatusLock_'));
353 }
354 }
355 foreach ($container->getAccountModules() as $module) {
356 if (!($module instanceof AccountStatusProvider)) {
357 continue;
358 }
359 $dummyAttributes = null;
360 $module->accountStatusPerformLock($container->get_type(), $dummyAttributes, $lockIds);
361 }
362 }
363 // unlock account
364 elseif ($_POST['lam_accountStatusAction'] == 'unlock') {
365 $unlockIds = [];
366 foreach ($_POST as $key => $value) {
367 if (($value === 'on') && (str_starts_with($key, 'lam_accountStatusUnlock_'))) {
368 $unlockIds[] = substr($key, strlen('lam_accountStatusUnlock_'));
369 }
370 }
371 foreach ($container->getAccountModules() as $module) {
372 if (!($module instanceof AccountStatusProvider)) {
373 continue;
374 }
375 $dummyAttributes = null;
376 $module->accountStatusPerformUnlock($container->get_type(), $dummyAttributes, $unlockIds);
377 }
378 }
379 }
380 }
381
382 /**
383 * Returns a list of configuration options.
384 *
385 * The field names are used as keywords to load and save settings.
386 * We recommend to use the type name as prefix for them (e.g. user_someSetting) to avoid naming conflicts.
387 *
388 * @return htmlElement|null output
389 *
390 * @see htmlElement
391 */
392 public function get_configOptions(): ?htmlElement {
393 return null;
394 }
395
396 /**
397 * Checks input values of config settings.
398 * <br>
399 * If the input data is invalid the return value is an array that contains subarrays to build StatusMessages ('message type', 'message head', 'message text').
400 * <br>If no errors occurred the function returns an empty array.
401 *
402 * @param array<string, string> $options hash array (option name => value) that contains the input. The option values are all arrays containing one or more elements.
403 * @return array<string|string[]>[] list of error messages
404 */
405 public function check_configOptions(array &$options): array {
406 return [];
407 }
408
409 /**
410 * Returns if this account type supports multiple configurations.
411 *
412 * @return bool multiple configs supported
413 */
414 public function supportsMultipleConfigs(): bool {
415 return true;
416 }
417
418 /**
419 * Returns the type configuration.
420 *
421 * @return ConfiguredType type configuration
422 */
423 protected function getType() {
424 return $this->type;
425 }
426
427 /**
428 * Returns the file name of the type icon.
429 * It needs to be 16x16px and located in graphics folder.
430 * By default, this is "{type name}.png"
431 *
432 * @return string file name
433 */
434 public function getIcon(): string {
435 return static::class . '.png';
436 }
437
438 /**
439 * Returns the scope name.
440 *
441 * @return string scope
442 */
443 public function getScope() {
444 return static::class;
445 }
446
447 }