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