ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilRegistrationSettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
36 {
37  public const CODE_TYPE_REGISTRATION = 1;
38  public const CODE_TYPE_EXTENSION = 2;
39 
43  public int $ref_id;
44 
50  protected ilLanguage $lng;
52  protected ilTabsGUI $tabs;
54  protected readonly ILIAS\HTTP\Services $http;
55  protected readonly ILIAS\Refinery\Factory $refinery;
56  protected readonly Factory $ui_factory;
57  protected readonly UIRenderer $ui_renderer;
58  protected readonly ilUIService $ui_service;
59  protected readonly ilObjUser $user;
66 
68 
69  public function __construct()
70  {
71  global $DIC;
72 
73  $this->tpl = $DIC->ui()->mainTemplate();
74  $this->ctrl = $DIC->ctrl();
75  $this->access = $DIC->access();
76  $this->rbacsystem = $DIC->rbac()->system();
77  $this->rbacreview = $DIC->rbac()->review();
78  $this->error = $DIC['ilErr'];
79  $this->tabs = $DIC->tabs();
80  $this->toolbar = $DIC->toolbar();
81 
82  $this->lng = $DIC->language();
83  $this->lng->loadLanguageModule('administration');
84  $this->lng->loadLanguageModule('registration');
85  $this->lng->loadLanguageModule('user');
86  $this->registration_settings = new ilRegistrationSettings();
87 
88  $this->http = $DIC->http();
89  $this->refinery = $DIC->refinery();
90  $this->ref_id = $this->initRefIdFromQuery();
91  $this->ui_factory = $DIC->ui()->factory();
92  $this->ui_renderer = $DIC->ui()->renderer();
93  $this->ui_service = $DIC->uiService();
94  $this->user = $DIC->user();
95  $this->code_repository = new RegistrationCodeRepository($DIC->database());
96  }
97 
98  protected function initRefIdFromQuery(): int
99  {
100  if ($this->http->wrapper()->query()->has('ref_id')) {
101  return $this->http->wrapper()->query()->retrieve(
102  'ref_id',
103  $this->refinery->kindlyTo()->int()
104  );
105  }
106  return 0;
107  }
108 
109  public function executeCommand(): void
110  {
111  $next_class = $this->ctrl->getNextClass($this);
112  $cmd = $this->ctrl->getCmd();
113 
114  if ($this->http->wrapper()->query()->has('registration_codes_table_action')) {
115  $cmd = $this->http->wrapper()->query()->retrieve(
116  'registration_codes_table_action',
117  $this->refinery->kindlyTo()->string()
118  );
119  }
120 
121  switch ($next_class) {
122  default:
123  if (!$cmd) {
124  $cmd = 'view';
125  }
126  $this->$cmd();
127  break;
128  }
129  }
130 
131  protected function checkAccess(string $a_permission): void
132  {
133  if (!$this->checkAccessBool($a_permission)) {
134  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->WARNING);
135  }
136  }
137 
138  protected function checkAccessBool(string $a_permission): bool
139  {
140  return $this->access->checkAccess($a_permission, '', $this->ref_id);
141  }
142 
143  public function setSubTabs(string $activeTab = 'registration_settings'): void
144  {
145  $this->tabs->addSubTab(
146  "registration_settings",
147  $this->lng->txt("registration_tab_settings"),
148  $this->ctrl->getLinkTarget($this, 'view')
149  );
150  $this->tabs->addSubTab(
151  "registration_codes",
152  $this->lng->txt("registration_tab_codes"),
153  $this->ctrl->getLinkTarget($this, 'listCodes')
154  );
155  $this->tabs->activateSubTab($activeTab);
156  }
157 
158  public function initForm(): ilPropertyFormGUI
159  {
160  $form_gui = new ilPropertyFormGUI();
161  $form_gui->setFormAction($this->ctrl->getFormAction($this, 'save'));
162  $form_gui->setTitle($this->lng->txt('reg_settings_header'));
163 
164  $reg_type = new ilRadioGroupInputGUI($this->lng->txt('reg_type'), 'reg_type');
165  $reg_type->addOption(new ilRadioOption(
166  $this->lng->txt('reg_disabled'),
168  ));
169  $option = new ilRadioOption($this->lng->txt('reg_direct'), (string) ilRegistrationSettings::IL_REG_DIRECT);
170  $option->setInfo($this->lng->txt('reg_direct_info'));
171  $cd = new ilCheckboxInputGUI(
172  $this->lng->txt('reg_allow_codes'),
174  );
175  $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
176  $option->addSubItem($cd);
177  $reg_type->addOption($option);
178  $option = new ilRadioOption($this->lng->txt('reg_approve'), (string) ilRegistrationSettings::IL_REG_APPROVE);
179  $option->setInfo($this->lng->txt('reg_approve_info'));
180  $cd = new ilCheckboxInputGUI(
181  $this->lng->txt('reg_allow_codes'),
183  );
184  $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
185  $option->addSubItem($cd);
186  $reg_type->addOption($option);
187  $option = new ilRadioOption(
188  $this->lng->txt('reg_type_confirmation'),
190  );
191  $option->setInfo($this->lng->txt('reg_type_confirmation_info'));
192  $lt = new ilNumberInputGUI($this->lng->txt('reg_confirmation_hash_life_time'), 'reg_hash_life_time');
193  $lt->setSize(6); // #8511
194  $lt->setMaxLength(6);
196  $lt->setRequired(true);
197  $lt->setInfo($this->lng->txt('reg_confirmation_hash_life_time_info'));
198  $lt->setSuffix($this->lng->txt('seconds'));
199  $option->addSubItem($lt);
200  $cd = new ilCheckboxInputGUI(
201  $this->lng->txt('reg_allow_codes'),
203  );
204  $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
205  $option->addSubItem($cd);
206  $reg_type->addOption($option);
207  $option = new ilRadioOption(
208  $this->lng->txt('registration_reg_type_codes'),
210  );
211  $option->setInfo($this->lng->txt('registration_reg_type_codes_info'));
212  $reg_type->addOption($option);
213  $form_gui->addItem($reg_type);
214 
215  $pwd_gen = new ilCheckboxInputGUI($this->lng->txt('passwd_generation'), 'reg_pwd');
216  $pwd_gen->setValue('1');
217  $pwd_gen->setInfo($this->lng->txt('reg_info_pwd'));
218  $form_gui->addItem($pwd_gen);
219 
220  $approver = new ilTextInputGUI($this->lng->txt('reg_notification'), 'reg_approver');
221  $approver->setSize(32);
222  $approver->setMaxLength(50);
223  $approver->setInfo($this->lng->txt('reg_notification_info'));
224  $form_gui->addItem($approver);
225 
226  $roles = new ilRadioGroupInputGUI($this->lng->txt('reg_role_assignment'), 'reg_role_type');
227  $option = new ilRadioOption($this->lng->txt('reg_fixed'), (string) ilRegistrationSettings::IL_REG_ROLES_FIXED);
228  $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
229  $edit = $this->ctrl->getLinkTarget($this, 'editRoles');
230  $list->setHtml($this->parseRoleList($this->prepareRoleList(), $edit));
231  $option->addSubItem($list);
232  $roles->addOption($option);
233  $option = new ilRadioOption($this->lng->txt('reg_email'), (string) ilRegistrationSettings::IL_REG_ROLES_EMAIL);
234  $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
235  $edit = $this->ctrl->getLinkTarget($this, 'editEmailAssignments');
236  $list->setHtml($this->parseRoleList($this->prepareAutomaticRoleList(), $edit));
237  $option->addSubItem($list);
238  $roles->addOption($option);
239  $roles->setInfo($this->lng->txt('registration_codes_override_global_info'));
240  $form_gui->addItem($roles);
241 
242  $limit = new ilCheckboxInputGUI($this->lng->txt('reg_access_limitations'), 'reg_access_limitation');
243  $limit->setValue('1');
244  $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
245  $edit = $this->ctrl->getLinkTarget($this, 'editRoleAccessLimitations');
246  $list->setHtml($this->parseRoleList($this->prepareAccessLimitationRoleList(), $edit));
247  $list->setInfo($this->lng->txt('registration_codes_override_global_info'));
248  $limit->addSubItem($list);
249  $form_gui->addItem($limit);
250 
251  $domains = new ilTextInputGUI($this->lng->txt('reg_allowed_domains'), 'reg_allowed_domains');
252  $domains->setInfo($this->lng->txt('reg_allowed_domains_info'));
253  $form_gui->addItem($domains);
254 
255  if ($this->rbacsystem->checkAccess("write", $this->ref_id)) {
256  $form_gui->addCommandButton('save', $this->lng->txt('save'));
257  }
258  return $form_gui;
259  }
260 
261  public function initFormValues(ilPropertyFormGUI $formGUI): void
262  {
264  if ($this->registration_settings->roleSelectionEnabled()) {
266  } elseif ($this->registration_settings->automaticRoleAssignmentEnabled()) {
268  }
269 
271  $values = [
272  'reg_type' => $this->registration_settings->getRegistrationType(),
273  'reg_hash_life_time' => $this->registration_settings->getRegistrationHashLifetime(),
274  'reg_pwd' => $this->registration_settings->passwordGenerationEnabled(),
275  'reg_approver' => $this->registration_settings->getApproveRecipientLogins(),
276  'reg_role_type' => $role_type,
277  'reg_access_limitation' => $this->registration_settings->getAccessLimitation(),
278  'reg_allowed_domains' => implode(';', $this->registration_settings->getAllowedDomains())
279  ];
280 
281  $allow_codes = $this->registration_settings->getAllowCodes();
282  $reg_type = $this->registration_settings->getRegistrationType();
283  if ($allow_codes && in_array($reg_type, [
287  ], true)) {
288  $values['reg_codes_' . $reg_type] = true;
289  }
290 
291  $formGUI->setValuesByArray($values);
292  }
293 
294  public function view(): void
295  {
296  $this->checkAccess('visible,read');
297  $this->setSubTabs();
298 
299  // edit new accout mail
300  $this->ctrl->setParameterByClass("ilobjuserfoldergui", "ref_id", USER_FOLDER_ID);
301  if ($this->checkAccessBool('write')) {
302  $this->toolbar->addButton(
303  $this->lng->txt('registration_user_new_account_mail'),
304  $this->ctrl->getLinkTargetByClass(
305  [
306  ilAdministrationGUI::class,
307  ilObjUserFolderGUI::class
308  ],
309  'newAccountMail'
310  )
311  );
312  $this->ctrl->setParameterByClass(ilObjUserFolderGUI::class, 'ref_id', $_GET['ref_id']);
313  }
314 
315  $form = $this->initForm();
316  $this->initFormValues($form);
317  $this->tpl->setContent($form->getHTML());
318  }
319 
320  public function save(): bool
321  {
322  $this->checkAccess('write');
323 
324  $form = $this->initForm();
325  $res = $form->checkInput();
326  $this->registration_settings->setRegistrationType((int) $form->getInput('reg_type'));
327  $this->registration_settings->setPasswordGenerationStatus((bool) $form->getInput('reg_pwd'));
328  $this->registration_settings->setApproveRecipientLogins($form->getInput('reg_approver'));
329  $this->registration_settings->setRoleType((int) $form->getInput('reg_role_type'));
330  $this->registration_settings->setAccessLimitation((bool) $form->getInput('reg_access_limitation'));
331  $this->registration_settings->setAllowedDomains((string) $form->getInput('reg_allowed_domains'));
332 
333  $allow_codes = false;
334  $reg_type = (int) $form->getInput('reg_type');
335  if (in_array($reg_type, [
339  ], true)) {
340  $allow_codes = (bool) $form->getInput('reg_codes_' . $reg_type);
341  }
342  $this->registration_settings->setAllowCodes($allow_codes);
343 
344  $hash_life_time = $form->getInput('reg_hash_life_time');
345  if (!preg_match('/^([0]|([1-9][0-9]*))([\.,][0-9]+)?$/', (string) $hash_life_time)) {
346  $this->registration_settings->setRegistrationHashLifetime(ilRegistrationSettings::REG_HASH_LIFETIME_MIN_VALUE);
347  } else {
348  $this->registration_settings->setRegistrationHashLifetime(max(
349  (int) $hash_life_time,
351  ));
352  }
353 
354  if ($error_code = $this->registration_settings->validate()) {
355  switch ($error_code) {
357  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('reg_unknown_recipients') . ' ' . $this->registration_settings->getUnknown());
358  $this->view();
359  return false;
360 
362  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('reg_approve_needs_recipient') . ' ' . $this->registration_settings->getUnknown());
363  $this->view();
364  return false;
365  }
366  }
367 
368  $this->registration_settings->save();
369  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
370  $this->view();
371  return true;
372  }
373 
374  protected function initRolesForm(): ilPropertyFormGUI
375  {
376  $role_form = new ilPropertyFormGUI();
377  $role_form->setFormAction($this->ctrl->getFormAction($this, 'save'));
378  $role_form->setTitle($this->lng->txt('reg_selectable_roles'));
379 
380  $roles = new ilCheckboxGroupInputGUI($this->lng->txt('reg_available_roles'), 'roles');
381  $allowed_roles = [];
382  foreach ($this->rbacreview->getGlobalRoles() as $role) {
383  if ($role === SYSTEM_ROLE_ID || $role === ANONYMOUS_ROLE_ID) {
384  continue;
385  }
386  $role_option = new ilCheckboxOption(ilObjRole::_lookupTitle($role));
387  $role_option->setValue((string) $role);
388  $roles->addOption($role_option);
389  $allowed_roles[$role] = ilObjRole::_lookupAllowRegister($role);
390  }
391 
392  $roles->setUseValuesAsKeys(true);
393  $roles->setValue($allowed_roles);
394  $role_form->addItem($roles);
395 
396  if ($this->checkAccessBool('write')) {
397  $role_form->addCommandButton("updateRoles", $this->lng->txt("save"));
398  }
399  $role_form->addCommandButton("view", $this->lng->txt("cancel"));
400  return $role_form;
401  }
402 
403  public function editRoles(?ilPropertyFormGUI $form = null): void
404  {
405  $this->checkAccess('write');
406  $this->tabs->clearTargets();
407  $this->tabs->setBackTarget(
408  $this->lng->txt("registration_settings"),
409  $this->ctrl->getLinkTarget($this, "view")
410  );
411  if (!$form instanceof ilPropertyFormGUI) {
412  $form = $this->initRolesForm();
413  }
414  $this->tpl->setContent($form->getHTML());
415  }
416 
417  public function updateRoles(): bool
418  {
419  $this->checkAccess('write');
420  $form = $this->initRolesForm();
421  if ($form->checkInput()) {
422  $roles = (array) $form->getInput('roles');
423  if (count($roles) < 1) {
424  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_last_role_for_registration'));
425  $this->editRoles();
426  return false;
427  }
428  foreach ($this->rbacreview->getGlobalRoles() as $role) {
429  if ($role_obj = ilObjectFactory::getInstanceByObjId($role, false)) {
430  $role_obj->setAllowRegister(isset($roles[$role]) && (int) $roles[$role] === 1);
431  $role_obj->update();
432  }
433  }
434  }
435  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
436  $this->view();
437  return true;
438  }
439 
440  public function editEmailAssignments(?ilPropertyFormGUI $form = null): void
441  {
442  $this->checkAccess('write');
443  $this->tabs->clearTargets();
444  $this->tabs->setBackTarget(
445  $this->lng->txt("registration_settings"),
446  $this->ctrl->getLinkTarget($this, "view")
447  );
448 
449  $this->initRoleAssignments();
450  $form = $form ?? $this->initEmailAssignmentForm();
451  $this->tpl->setContent($form->getHTML());
452  }
453 
455  {
456  $role_assignment_form = new ilPropertyFormGUI();
457  $role_assignment_form->setFormAction($this->ctrl->getFormAction($this));
458  $role_assignment_form->setTitle($this->lng->txt('reg_email_role_assignment'));
459 
460  $global_roles = ["" => $this->lng->txt("links_select_one")];
461  foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
462  if ($role_id === ANONYMOUS_ROLE_ID) {
463  continue;
464  }
465 
466  $global_roles[$role_id] = ilObjRole::_lookupTitle($role_id);
467  $role_assignments = new ilCheckboxInputGUI(ilObjRole::_lookupTitle($role_id), "role_assigned_$role_id");
468 
469  $domains = $this->assignments_obj->getDomainsByRole($role_id);
470 
471  $domain = new ilTextInputGUI($this->lng->txt('reg_domain'), "domain_$role_id");
472  $domain->setMulti(true);
473  $domain->setValidationRegexp("/^@.*\.[a-zA-Z]{1,4}$/");
474  if (!empty($domains)) {
475  $domain->setValue($domains[0]);
476  $domain->setMultiValues($domains);
477  $role_assignments->setChecked(true);
478  }
479 
480  $role_assignments->addSubItem($domain);
481  $role_assignment_form->addItem($role_assignments);
482  }
483 
484  $default_role = new ilSelectInputGUI($this->lng->txt('reg_default'));
485  $default_role->setPostVar("default_role");
486  $default_role->setOptions($global_roles);
487  $default_role->setValue($this->assignments_obj->getDefaultRole());
488  $default_role->setRequired(true);
489  $role_assignment_form->addItem($default_role);
490 
491  $role_assignment_form->addCommandButton("saveAssignment", $this->lng->txt("save"));
492  $role_assignment_form->addCommandButton("view", $this->lng->txt("cancel"));
493 
494  return $role_assignment_form;
495  }
496 
497  public function editRoleAccessLimitations(?ilPropertyFormGUI $form = null): void
498  {
499  global $DIC;
500 
501  $this->checkAccess('write');
502  $this->tabs->clearTargets();
503  $this->tabs->setBackTarget(
504  $this->lng->txt("registration_settings"),
505  $this->ctrl->getLinkTarget($this, "view")
506  );
507  $this->initRoleAccessLimitations();
508  if (null === $form) {
509  $form = $this->initRoleAccessForm();
510  }
511  $this->tpl->setContent($form->getHTML());
512  }
513 
515  {
516  $form = new ilPropertyFormGUI();
517  $form->setFormAction($this->ctrl->getFormAction($this));
518  $form->setTitle($this->lng->txt('reg_role_access_limitations'));
519  foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
520  $role_access = new ilRadioGroupInputGUI($role['title'], "role_access_" . $role['id']);
521 
522  $op_unlimited = new ilRadioOption($this->lng->txt('reg_access_limitation_mode_unlimited'), "unlimited");
523 
524  $op_absolute = new ilRadioOption($this->lng->txt('reg_access_limitation_mode_absolute'), "absolute");
525  $absolute_date = new ilDateTime(
526  date("d.m.Y", $this->access_limitations_obj->getAbsolute($role['id'])),
528  );
529  $date = new ilDateTimeInputGUI("", "absolute_date_" . $role['id']);
530  $date->setDate($absolute_date);
531  $op_absolute->addSubItem($date);
532 
533  $op_relative = new ilRadioOption($this->lng->txt('reg_access_limitation_mode_relative'), "relative");
534  $duration = new ilDurationInputGUI("", "duration_" . $role['id']);
535  $duration->setShowMinutes(false);
536  $duration->setShowHours(false);
537  $duration->setShowDays(true);
538  $duration->setShowMonths(true);
539  $duration->setDays($this->access_limitations_obj->getRelative($role['id'], 'd'));
540  $duration->setMonths($this->access_limitations_obj->getRelative($role['id'], 'm'));
541  $op_relative->addSubItem($duration);
542 
543  $role_access->addOption($op_unlimited);
544  $role_access->addOption($op_absolute);
545  $role_access->addOption($op_relative);
546  $role_access->setValue(
547  $this->access_limitations_obj->getMode(
548  $role['id']
549  ) === 'null' ? 'unlimited' : $this->access_limitations_obj->getMode($role['id'])
550  );
551 
552  $form->addItem($role_access);
553  }
554 
555  $form->addCommandButton("saveRoleAccessLimitations", $this->lng->txt("save"));
556  $form->addCommandButton("view", $this->lng->txt("cancel"));
557 
558  return $form;
559  }
560 
561  public function saveAssignment(): bool
562  {
563  $this->checkAccess('write');
564  $this->initRoleAssignments();
565  $form = $this->initEmailAssignmentForm();
566  $is_valid = $form->checkInput();
567  $form->setValuesByPost();
568  if (!$is_valid) {
569  $this->editEmailAssignments($form);
570  return false;
571  }
572 
573  $assignments_by_domain = [];
574  $problems_domains_by_field_id = [];
575  foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
576  if ($role_id === ANONYMOUS_ROLE_ID) {
577  continue;
578  }
579 
580  $role_assigned_input = $form->getInput("role_assigned_$role_id");
581  if (!$role_assigned_input) {
582  continue;
583  }
584 
585  $domain_input = $form->getInput("domain_$role_id");
586  foreach ($domain_input as $domain) {
587  if (!is_string($domain) || $domain === '') {
588  continue;
589  }
590 
591  if (isset($assignments_by_domain[$domain])) {
592  if (!isset($problems_domains_by_field_id["role_assigned_$role_id"])) {
593  $problems_domains_by_field_id["role_assigned_$role_id"] = [];
594  }
595 
596  $problems_domains_by_field_id["domain_$role_id"][$domain] = $domain;
597  continue;
598  }
599 
600  $assignments_by_domain[$domain] = $role_id;
601  }
602  }
603 
604  if ($problems_domains_by_field_id !== []) {
605  foreach ($problems_domains_by_field_id as $field_id => $domains) {
606  $domain_string = implode(', ', $domains);
607  $alert = sprintf($this->lng->txt('reg_domain_already_assigned_p'), $domain_string);
608  if (count($domains) === 1) {
609  $alert = sprintf($this->lng->txt('reg_domain_already_assigned_s'), $domain_string);
610  }
611  $form->getItemByPostVar($field_id)->setAlert($alert);
612  }
613 
614  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
615  $this->editEmailAssignments($form);
616  return false;
617  }
618 
619  $this->assignments_obj->deleteAll();
620 
621  $counter = 0;
622  foreach ($assignments_by_domain as $domain => $role_id) {
623  $this->assignments_obj->setDomain($counter, $domain);
624  $this->assignments_obj->setRole($counter, $role_id);
625  $counter++;
626  }
627  $default_role = $form->getInput("default_role");
628  $this->assignments_obj->setDefaultRole((int) $default_role);
629  $this->assignments_obj->save();
630  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
631  $this->view();
632  return true;
633  }
634 
635  public function saveRoleAccessLimitations(): bool
636  {
637  $this->checkAccess('write');
638  $this->initRoleAccessLimitations();
639 
640  $form = $this->initRoleAccessForm();
641  if (!$form->checkInput()) {
642  $form->setValuesByPost();
643  $this->editRoleAccessLimitations($form);
644  return false;
645  }
646 
647  $this->access_limitations_obj->resetAccessLimitations();
648  foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
649  $mode = $form->getInput('role_access_' . $role['id']);
650  $this->access_limitations_obj->setMode($mode, $role['id']);
651 
652  if ($mode === 'absolute') {
653  $this->access_limitations_obj->setAbsolute($form->getInput('absolute_date_' . $role['id']), $role['id']);
654  }
655 
656  if ($mode === 'relative') {
657  $this->access_limitations_obj->setRelative($form->getInput('duration_' . $role['id']), $role['id']);
658  }
659  }
660 
661  if ($err = $this->access_limitations_obj->validate()) {
662  switch ($err) {
664  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('reg_access_limitation_missing_mode'));
665  break;
666 
668  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('reg_access_limitation_out_of_date'));
669  break;
670  }
671  $this->editRoleAccessLimitations();
672  return false;
673  }
674 
675  $this->access_limitations_obj->save();
676  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
677  $this->view();
678  return true;
679  }
680 
686  private function parseRoleList(array $roles, string $url): string
687  {
688  $tpl = new ilTemplate('tpl.registration_roles.html', true, true, 'components/ILIAS/Registration');
689 
690  $tpl->setVariable("EDIT", $this->lng->txt("edit"));
691  $tpl->setVariable("LINK_EDIT", $url);
692 
693  if (is_array($roles) && count($roles)) {
694  foreach ($roles as $role) {
695  $tpl->setCurrentBlock("list_item");
696  $tpl->setVariable("LIST_ITEM_ITEM", $role);
697  $tpl->parseCurrentBlock();
698  }
699  } else {
700  $tpl->setVariable("NONE", $this->lng->txt('none'));
701  }
702  return $tpl->get();
703  }
704 
705  /***
706  * @return string[]
707  */
708  private function prepareRoleList(): array
709  {
710  $all = [];
711  foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
712  $all[] = $role['title'];
713  }
714 
715  return $all;
716  }
717 
721  private function prepareAutomaticRoleList(): array
722  {
723  $this->initRoleAssignments();
724  $all = [];
725  foreach ($this->assignments_obj->getAssignments() as $assignment) {
726  if ($assignment['domain'] !== '' && $assignment['role']) {
727  $all[] = $assignment['domain'] . ' -> ' . ilObjRole::_lookupTitle((int) $assignment['role']);
728  }
729  }
730 
731  if ((string) $this->assignments_obj->getDefaultRole() !== '') {
732  $all[] = $this->lng->txt('reg_default') . ' -> ' . ilObjRole::_lookupTitle($this->assignments_obj->getDefaultRole());
733  }
734 
735  return $all;
736  }
737 
738  private function prepareAccessLimitationRoleList(): array
739  {
740  $this->initRoleAccessLimitations();
741  $all = [];
742  foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
743  switch ($this->access_limitations_obj->getMode((int) $role['id'])) {
744  case 'absolute':
745  $txt_access_value = $this->lng->txt('reg_access_limitation_limited_until');
746  $txt_access_value .= " " . ilDatePresentation::formatDate(
747  new ilDateTime($this->access_limitations_obj->getAbsolute((int) $role['id']), IL_CAL_UNIX)
748  );
749  break;
750 
751  case 'relative':
752  $months = $this->access_limitations_obj->getRelative($role['id'], 'm');
753  $days = $this->access_limitations_obj->getRelative($role['id'], 'd');
754 
755  $txt_access_value = $this->lng->txt('reg_access_limitation_limited_time') . " ";
756 
757  if ($months) {
758  if ($days) {
759  $txt_access_value .= ", ";
760  } else {
761  $txt_access_value .= " " . $this->lng->txt('and') . " ";
762  }
763  } elseif ($days) {
764  $txt_access_value .= " " . $this->lng->txt('and') . " ";
765  }
766 
767  if ($months) {
768  $txt_access_value .= $months . " ";
769  $txt_access_value .= ($months === 1) ? $this->lng->txt('month') : $this->lng->txt('months');
770 
771  if ($days) {
772  $txt_access_value .= " " . $this->lng->txt('and') . " ";
773  }
774  }
775 
776  if ($days) {
777  $txt_access_value .= $days . " ";
778  $txt_access_value .= ($days === 1) ? $this->lng->txt('day') : $this->lng->txt('days');
779  }
780  break;
781 
782  default:
783  $txt_access_value = $this->lng->txt('reg_access_limitation_none');
784  break;
785  }
786 
787  $all[] = $role['title'] . ' (' . $txt_access_value . ')';
788  }
789 
790  return $all;
791  }
792 
793  private function initRoleAssignments(): void
794  {
795  if (!$this->assignments_obj instanceof ilRegistrationRoleAssignments) {
796  $this->assignments_obj = new ilRegistrationRoleAssignments();
797  }
798  }
799 
800  private function initRoleAccessLimitations(): void
801  {
802  if (!$this->access_limitations_obj instanceof ilRegistrationRoleAccessLimitations) {
803  $this->access_limitations_obj = new ilRegistrationRoleAccessLimitations();
804  }
805  }
806 
807  public function listCodes(): void
808  {
809  $this->checkAccess('visible,read');
810  $this->setSubTabs('registration_codes');
811  if ($this->checkAccessBool('write')) {
812  $this->toolbar->addButton(
813  $this->lng->txt('registration_codes_add'),
814  $this->ctrl->getLinkTarget($this, 'addCodes')
815  );
816  }
817  $this->tpl->setContent($this->ui_renderer->render([
818  $this->getRegistrationFilter()->getFilterComponent(),
819  $this->getRegistrationCodeTable()->getTableComponent($this->getRegistrationFilter())
820  ]));
821  }
822 
824  {
825  if (!isset($this->registration_codes_table)) {
826  $this->registration_codes_table = new RegistrationCodesTable(
827  $this->http->request(),
828  $this->lng,
830  new DataFactory(),
832  $this->ctrl->getLinkTarget($this, 'listCodes', '', true),
833  $this->user,
835  $this->checkAccessBool('write'),
836  );
837  }
838 
840  }
841 
843  {
844  if (!isset($this->registration_code_filter)) {
845  $this->registration_code_filter = new RegistrationFilterComponent(
846  $this->ctrl->getLinkTarget($this, 'listCodes', '', true),
849  $this->lng,
852  );
853  }
854 
856  }
857 
859  {
860  $this->form_gui = new ilPropertyFormGUI();
861  $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'createCodes'));
862  $this->form_gui->setTitle($this->lng->txt('registration_codes_edit_header'));
863 
864  $count = new ilNumberInputGUI($this->lng->txt('registration_codes_number'), 'reg_codes_number');
865  $count->setSize(4);
866  $count->setMaxLength(4);
867  $count->setMinValue(1);
868  $count->setMaxValue(1000);
869  $count->setRequired(true);
870  $this->form_gui->addItem($count);
871 
872  // type
873  $code_type = new ilCheckboxGroupInputGUI($this->lng->txt('registration_codes_type'), 'code_type');
874  $code_type->setRequired(true);
875 
876  $code_type->addOption(
877  new ilCheckboxOption(
878  $this->lng->txt('registration_codes_type_reg'),
879  (string) self::CODE_TYPE_REGISTRATION,
880  $this->lng->txt('registration_codes_type_reg_info')
881  )
882  );
883  $code_type->addOption(
884  new ilCheckboxOption(
885  $this->lng->txt('registration_codes_type_ext'),
886  (string) self::CODE_TYPE_EXTENSION,
887  $this->lng->txt('registration_codes_type_ext_info')
888  )
889  );
890  $this->form_gui->addItem($code_type);
891 
892  $sec = new ilFormSectionHeaderGUI();
893  $sec->setTitle($this->lng->txt('registration_codes_roles_title'));
894  $this->form_gui->addItem($sec);
895 
896  $options = ["" => $this->lng->txt('registration_codes_no_assigned_role')];
897  foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
898  if (!in_array($role_id, [SYSTEM_ROLE_ID, ANONYMOUS_ROLE_ID], true)) {
899  $options[$role_id] = ilObject::_lookupTitle($role_id);
900  }
901  }
902  $roles = new ilSelectInputGUI($this->lng->txt("registration_codes_roles"), "reg_codes_role");
903  $roles->setInfo($this->lng->txt("registration_codes_override_info"));
904  $roles->setOptions($options);
905  $this->form_gui->addItem($roles);
906 
907  $local = new ilTextInputGUI($this->lng->txt("registration_codes_roles_local"), "reg_codes_local");
908  $local->setMulti(true);
909  $local->setDataSource($this->ctrl->getLinkTarget($this, "getLocalRoleAutoComplete", "", true));
910  $this->form_gui->addItem($local);
911 
912  $sec = new ilFormSectionHeaderGUI();
913  $sec->setTitle($this->lng->txt('reg_access_limitations'));
914  $this->form_gui->addItem($sec);
915 
916  $limit = new ilRadioGroupInputGUI($this->lng->txt("reg_access_limitation_mode"), "reg_limit");
917  $limit->setInfo($this->lng->txt("registration_codes_override_info"));
918  $this->form_gui->addItem($limit);
919 
920  $opt = new ilRadioOption($this->lng->txt("registration_codes_roles_limitation_none"), "none");
921  $limit->addOption($opt);
922 
923  $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_none"), "unlimited");
924  $limit->addOption($opt);
925 
926  $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_mode_absolute"), "absolute");
927  $limit->addOption($opt);
928 
929  $dt = new ilDateTimeInputGUI($this->lng->txt("reg_access_limitation_mode_absolute_target"), "abs_date");
930  $dt->setRequired(true);
931  $opt->addSubItem($dt);
932 
933  $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_mode_relative"), "relative");
934  $limit->addOption($opt);
935 
936  $dur = new ilDurationInputGUI($this->lng->txt("reg_access_limitation_mode_relative_target"), "rel_date");
937  $dur->setRequired(true);
938  $dur->setShowMonths(true);
939  $dur->setShowDays(true);
940  $dur->setShowHours(false);
941  $dur->setShowMinutes(false);
942  $opt->addSubItem($dur);
943 
944  $this->form_gui->addCommandButton('createCodes', $this->lng->txt('create'));
945  $this->form_gui->addCommandButton('listCodes', $this->lng->txt('cancel'));
946  return $this->form_gui;
947  }
948 
949  // see ilRoleAutoCompleteInputGUI
950  public function getLocalRoleAutoComplete(): void
951  {
952  $q = $_REQUEST["term"];
954  echo $list;
955  exit;
956  }
957 
958  public function addCodes(): void
959  {
960  $this->checkAccess('write');
961  $this->setSubTabs('registration_codes');
962  $this->initAddCodesForm();
963 
964  // default
965  $limit = $this->form_gui->getItemByPostVar("reg_limit");
966  $limit->setValue("none");
967  $this->tpl->setContent($this->form_gui->getHTML());
968  }
969 
970  public function createCodes(): void
971  {
972  $this->checkAccess('write');
973  $this->setSubTabs('registration_codes');
974 
975  $this->initAddCodesForm();
976  $valid = $this->form_gui->checkInput();
977  if ($valid) {
978  $number = $this->form_gui->getInput('reg_codes_number');
979  $role = (int) $this->form_gui->getInput('reg_codes_role');
980  $local = $this->form_gui->getInput("reg_codes_local");
981 
982  if (is_array($local)) {
983  $role_ids = [];
984  foreach (array_unique($local) as $item) {
985  if (trim($item)) {
986  $role_id = $this->rbacreview->roleExists($item);
987  if ($role_id) {
988  $role_ids[] = $role_id;
989  }
990  }
991  }
992  if (count($role_ids)) {
993  $local = $role_ids;
994  }
995  }
996 
997  $date = null;
998  $limit = $this->form_gui->getInput("reg_limit");
999  switch ($limit) {
1000  case "absolute":
1001  $date_input = $this->form_gui->getItemByPostVar("abs_date");
1002  $date = $date_input->getDate()->get(IL_CAL_DATE);
1003  if ($date < date("Y-m-d")) {
1004  $date_input->setAlert($this->lng->txt("form_msg_wrong_date"));
1005  $valid = false;
1006  }
1007  break;
1008 
1009  case "relative":
1010  $date = $this->form_gui->getInput("rel_date");
1011  if (!array_sum($date)) {
1012  $valid = false;
1013  } else {
1014  $date = serialize([
1015  "d" => $date["dd"],
1016  "m" => $date["MM"] % 12,
1017  "y" => (int) floor($date["MM"] / 12)
1018  ]);
1019  }
1020  break;
1021 
1022  case "none":
1023  $limit = null;
1024  break;
1025  }
1026  }
1027 
1028  if ($valid) {
1029  $stamp = time();
1030  for ($loop = 1; $loop <= $number; $loop++) {
1031  $code_types = (array) $this->form_gui->getInput('code_type');
1032 
1034  $role,
1035  $stamp,
1036  $local,
1037  $limit,
1038  $date,
1039  in_array(self::CODE_TYPE_REGISTRATION, $code_types) ? true : false,
1040  in_array(self::CODE_TYPE_EXTENSION, $code_types) ? true : false
1041  );
1042  }
1043 
1044  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
1045  $this->ctrl->redirect($this, "listCodes");
1046  } else {
1047  $this->form_gui->setValuesByPost();
1048  $this->tpl->setContent($this->form_gui->getHTML());
1049  }
1050  }
1051 
1052  public function deleteCodes(): void
1053  {
1054  $this->checkAccess("write");
1055 
1056  $ids = [];
1057  if ($this->http->wrapper()->post()->has('id')) {
1058  $ids = $this->http->wrapper()->post()->retrieve(
1059  'id',
1060  $this->refinery->kindlyTo()->listOf(
1061  $this->refinery->kindlyTo()->int()
1062  )
1063  );
1064  }
1065  $this->code_repository->deleteCodes($ids);
1066  $this->tpl->setOnScreenMessage('success', $this->lng->txt('info_deleted'), true);
1067  $this->ctrl->redirect($this, "listCodes");
1068  }
1069 
1070  public function deleteConfirmation(): void
1071  {
1072  $this->checkAccess('write');
1073  $codes = [];
1074  if ($this->http->wrapper()->query()->has('registration_codes_code_ids')) {
1075  $ids = $this->http->wrapper()->query()->retrieve(
1076  'registration_codes_code_ids',
1077  $this->refinery->kindlyTo()->listOf(
1078  $this->refinery->kindlyTo()->string()
1079  )
1080  );
1081  if ($ids === ['ALL_OBJECTS']) {
1082  $codes = $this->code_repository->getCodesByFilter($this->getRegistrationFilter()->getFilterData());
1083  } else {
1084  $ids = $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())->transform($ids);
1085  $codes = $this->code_repository->loadCodesByIds($ids);
1086  }
1087  }
1088  if (!count($codes)) {
1089  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_select_one'), true);
1090  $this->ctrl->redirect($this, 'listCodes');
1091  }
1092  $this->setSubTabs('registration_codes');
1093 
1094  $gui = new ilConfirmationGUI();
1095  $gui->setHeaderText($this->lng->txt('info_delete_sure'));
1096  $gui->setCancel($this->lng->txt('cancel'), 'listCodes');
1097  $gui->setConfirm($this->lng->txt('confirm'), 'deleteCodes');
1098  $gui->setFormAction($this->ctrl->getFormAction($this, 'deleteCodes'));
1099 
1100  foreach ($codes as $code) {
1101  $gui->addItem('id[]', (string) $code['code_id'], $code['code']);
1102  }
1103  $this->tpl->setContent($gui->getHTML());
1104  }
1105 
1106  public function exportCodes(): void
1107  {
1108  $codes = [];
1109  if ($this->http->wrapper()->query()->has('registration_codes_code_ids')) {
1110  $ids = $this->http->wrapper()->query()->retrieve(
1111  'registration_codes_code_ids',
1112  $this->refinery->kindlyTo()->listOf(
1113  $this->refinery->kindlyTo()->string()
1114  )
1115  );
1116  if ($ids === ['ALL_OBJECTS']) {
1117  $codes = $this->code_repository->getCodesByFilter($this->getRegistrationFilter()->getFilterData());
1118  } else {
1119  $ids = $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())->transform($ids);
1120  $codes = $this->code_repository->loadCodesByIds($ids);
1121  }
1122 
1123  $codes = array_filter(array_map(
1124  static fn($code) => (string) ($code['code'] ?? ''),
1125  $codes
1126  ));
1127  }
1128 
1129  if (count($codes)) {
1131  implode("\r\n", $codes),
1132  'ilias_registration_codes_' . date('d-m-Y') . '.txt',
1133  'text/plain'
1134  );
1135  } else {
1136  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('registration_export_codes_no_data'));
1137  $this->listCodes();
1138  }
1139  }
1140 }
This class represents a duration (typical hh:mm:ss) property in a property form.
This class represents an option in a radio group.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
$res
Definition: ltiservices.php:66
$_GET["client_id"]
Definition: webdav.php:30
This class represents an option in a checkbox group.
const USER_FOLDER_ID
Definition: constants.php:33
This class represents a selection list property in a property form.
parseRoleList(array $roles, string $url)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly RegistrationCodeRepository $code_repository
static _lookupRegisterAllowed()
get all roles that are activated in user registration
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
ilRegistrationRoleAccessLimitations $access_limitations_obj
const SYSTEM_ROLE_ID
Definition: constants.php:29
editRoles(?ilPropertyFormGUI $form=null)
$valid
Class class.ilregistrationEmailRoleAssignments.
addOption(ilRadioOption $a_option)
readonly RegistrationFilterComponent $registration_code_filter
$url
Definition: shib_logout.php:68
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
const IL_CAL_UNIX
setVariable(string $variable, $value='')
Sets the given variable to the given value.
$duration
initFormValues(ilPropertyFormGUI $formGUI)
static getList(string $a_str)
Get completion list.
This class represents a date/time property in a property form.
readonly RegistrationCodesTable $registration_codes_table
setPostVar(string $a_postvar)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
editRoleAccessLimitations(?ilPropertyFormGUI $form=null)
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
static _lookupTitle(int $obj_id)
setFormAction(string $a_formaction)
This is how the factory for UI elements looks.
Definition: Factory.php:37
This class represents a number property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
setSubTabs(string $activeTab='registration_settings')
Class ilRegistrationSettingsGUI.
This class represents a property in a property form.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
setRequired(bool $a_required)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
exit
Class class.ilRegistrationAccessLimitation.
readonly ILIAS HTTP Services $http
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilObjAuthSettingsGUI.
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
const IL_CAL_DATE
static create(int $role, int $stamp, array $local_roles, ?string $limit, ?string $limit_date, bool $reg_type, bool $ext_type)
editEmailAssignments(?ilPropertyFormGUI $form=null)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
$q
Definition: shib_logout.php:23
readonly ILIAS Refinery Factory $refinery
static _lookupAllowRegister(int $a_role_id)
check whether role is allowed in user registration or not
ilRegistrationRoleAssignments $assignments_obj