ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSamlSettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
31 {
32  private const VIEW_MODE_GLOBAL = 1;
33  private const VIEW_MODE_SINGLE = 2;
34 
35  public const DEFAULT_CMD = 'listIdps';
36 
37  private const PERMISSION_WRITE = 'write';
38 
39  private const REQUEST_PARAM_SAML_IDP_ID = 'saml_idp_id';
40  private const REQUEST_PARAM_SAML_IDP_IDS = 'saml_idp_ids';
41 
42  private const MESSAGE_TYPE_FAILURE = 'failure';
43  private const MESSAGE_TYPE_SUCCESS = 'success';
44 
45  private const LNG_SAVED_SUCCESSFULLY = 'saved_successfully';
46  private const LNG_AUTH_SAML_USER_MAPPING = 'auth_saml_user_mapping';
47  private const LNG_LOGIN_FORM = 'login_form';
48  private const LNG_CANCEL = 'cancel';
49 
50  private const CMD_SAVE_NEW_IDP = 'saveNewIdp';
51  private const CMD_SAVE_SETTINGS = 'saveSettings';
52  private const CMD_SHOW_IDP_SETTINGS = 'showIdpSettings';
53  private const CMT_SAVE_IDP_SETTINGS = 'saveIdpSettings';
54  private const CMD_SAVE = 'save';
55  private const CMD_SAVE_USER_ATTRIBUTE_MAPPING = 'saveUserAttributeMapping';
56 
57  private const PROP_UPDATE_SUFFIX = '_update';
58 
59  private const METADATA_STORAGE_KEY = 'metadata';
60 
64  private const GLOBAL_COMMANDS = [
65  self::DEFAULT_CMD,
66  'showAddIdpForm',
67  'showSettings',
68  'saveSettings',
69  'showNewIdpForm',
70  'saveNewIdp',
71  ];
72 
76  private const GLOBAL_ENTITY_COMMANDS = [
77  'deactivateIdp',
78  'activateIdp',
79  'confirmDeleteIdp',
80  'deleteIdp',
81  ];
82 
86  private const IGNORED_USER_FIELDS = [
87  'mail_incoming_mail',
88  'preferences',
89  'hide_own_online_status',
90  'show_users_online',
91  'roles',
92  'upload',
93  'password',
94  'username',
95  'language',
96  'skin_style',
97  'interests_general',
98  'interests_help_offered',
99  'interests_help_looking',
100  'bs_allow_to_contact_me',
101  'chat_osc_accept_msg',
102  'chat_broadcast_typing',
103  ];
104 
105  private readonly ilCtrlInterface $ctrl;
106  private readonly ilLanguage $lng;
107  private readonly ilGlobalTemplateInterface $tpl;
108  private readonly ilAccessHandler $access;
109  private readonly RBACServices $rbac;
110  private readonly ilErrorHandling $error_handler;
111  private readonly ilTabsGUI $tabs;
112  private readonly ilToolbarGUI $toolbar;
113  private readonly GlobalHttpState $httpState;
114  private readonly Refinery $refinery;
115  private readonly ilHelpGUI $help;
117  private ?ilSamlIdp $idp = null;
118  private ?ilSamlAuth $samlAuth = null;
119  private readonly \ILIAS\UI\Factory $ui_factory;
120  private readonly \ILIAS\UI\Renderer $ui_renderer;
121 
122  public function __construct(private readonly int $ref_id)
123  {
124  global $DIC;
125 
126  $this->ctrl = $DIC->ctrl();
127  $this->tpl = $DIC->ui()->mainTemplate();
128  $this->lng = $DIC->language();
129  $this->access = $DIC->access();
130  $this->rbac = $DIC->rbac();
131  $this->error_handler = $DIC['ilErr'];
132  $this->tabs = $DIC->tabs();
133  $this->toolbar = $DIC['ilToolbar'];
134  $this->help = $DIC['ilHelp'];
135  $this->httpState = $DIC->http();
136  $this->refinery = $DIC->refinery();
137  $this->ui_factory = $DIC->ui()->factory();
138  $this->ui_renderer = $DIC->ui()->renderer();
139 
140  $this->lng->loadLanguageModule('auth');
141  }
142 
143  private function ensureAccess(string $operation): void
144  {
145  if (!$this->rbac->system()->checkAccess($operation, $this->ref_id)) {
146  $this->error_handler->raiseError($this->lng->txt('msg_no_perm_read'), $this->error_handler->WARNING);
147  }
148  }
149 
150  public function getUnsafeGetCommands(): array
151  {
152  return [
153  'handleTableActions'
154  ];
155  }
156 
157  public function getSafePostCommands(): array
158  {
159  return [];
160  }
161 
162  private function ensureWriteAccess(): void
163  {
164  $this->ensureAccess(self::PERMISSION_WRITE);
165  }
166 
167  private function ensureReadAccess(): void
168  {
169  $this->ensureAccess('read');
170  }
171 
172  public function getRefId(): int
173  {
174  return $this->ref_id;
175  }
176 
177  private function getIdpIdOrZero(): int
178  {
179  $idpId = 0;
180  if ($this->httpState->wrapper()->query()->has(self::REQUEST_PARAM_SAML_IDP_ID)) {
181  $idpId = $this->httpState->wrapper()->query()->retrieve(
182  self::REQUEST_PARAM_SAML_IDP_ID,
183  $this->refinery->kindlyTo()->int()
184  );
185  } elseif ($this->httpState->wrapper()->post()->has(self::REQUEST_PARAM_SAML_IDP_ID)) {
186  $idpId = $this->httpState->wrapper()->post()->retrieve(
187  self::REQUEST_PARAM_SAML_IDP_ID,
188  $this->refinery->kindlyTo()->int()
189  );
190  }
191 
192  if ($this->httpState->wrapper()->query()->has('saml_idps_table_action')) {
193  if ($this->httpState->wrapper()->query()->has('saml_idps_idp_id')) {
194  $idpIds = $this->httpState->wrapper()->query()->retrieve(
195  'saml_idps_idp_id',
196  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
197  );
198  if (count($idpIds) === 1) {
199  $idpId = current($idpIds);
200  }
201  }
202  }
203 
204  if ($this->httpState->wrapper()->post()->has(self::REQUEST_PARAM_SAML_IDP_IDS)) {
205  $idpIds = $this->httpState->wrapper()->post()->retrieve(
206  self::REQUEST_PARAM_SAML_IDP_IDS,
207  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
208  );
209  if (count($idpIds) === 1) {
210  $idpId = current($idpIds);
211  }
212  }
213 
214  return $idpId;
215  }
216 
217  private function initIdp(): void
218  {
219  try {
220  $this->idp = ilSamlIdp::getInstanceByIdpId($this->getIdpIdOrZero());
221  } catch (Exception) {
222  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_FAILURE, $this->lng->txt('auth_saml_unknow_idp'), true);
223  $this->ctrl->setParameter($this, self::REQUEST_PARAM_SAML_IDP_ID, null);
224  $this->ctrl->redirect($this, self::DEFAULT_CMD);
225  }
226  }
227 
228  public function executeCommand(): void
229  {
230  $this->ensureReadAccess();
231 
232  try {
233  $factory = new ilSamlAuthFactory();
234  $this->samlAuth = $factory->auth();
235  } catch (Throwable $e) {
236  if ('Database error: could not find driver' === $e->getMessage()) {
237  $this->tpl->setOnScreenMessage(
238  self::MESSAGE_TYPE_FAILURE,
239  $this->lng->txt('auth_saml_err_sqlite_driver')
240  );
241  } else {
242  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_FAILURE, $e->getMessage());
243  }
244  }
245 
246  $this->help->setScreenIdComponent('auth');
247  $cmd = $this->ctrl->getCmd();
248  if ($cmd === null || $cmd === '' || !method_exists($this, $cmd)) {
249  $cmd = self::DEFAULT_CMD;
250  }
251  $ipdId = $this->getIdpIdOrZero();
252  if ($ipdId > 0) {
253  $this->ctrl->setParameter($this, self::REQUEST_PARAM_SAML_IDP_ID, $ipdId);
254  }
255  if (!in_array(strtolower($cmd), array_map('strtolower', self::GLOBAL_COMMANDS), true)) {
256  if (0 === $ipdId) {
257  $this->ctrl->redirect($this, self::DEFAULT_CMD);
258  }
259 
260  $this->initIdp();
261  $this->initUserAttributeMapping();
262  }
263  if (
264  in_array(strtolower($cmd), array_map('strtolower', self::GLOBAL_COMMANDS), true) ||
265  in_array(strtolower($cmd), array_map('strtolower', self::GLOBAL_ENTITY_COMMANDS), true)
266  ) {
267  $this->setSubTabs(self::VIEW_MODE_GLOBAL);
268  } else {
269  $this->setSubTabs(self::VIEW_MODE_SINGLE);
270  }
271  $this->$cmd();
272  }
273 
274  private function listIdps(): void
275  {
276  if ($this->samlAuth && $this->rbac->system()->checkAccess(self::PERMISSION_WRITE, $this->ref_id)) {
277  $this->toolbar->addStickyItem(
278  $this->ui_factory->button()->standard(
279  $this->lng->txt('auth_saml_add_idp_btn'),
280  $this->ctrl->getLinkTarget($this, 'showNewIdpForm')
281  )
282  );
283  }
284 
285  $federationMdUrl = rtrim(
286  ILIAS_HTTP_PATH,
287  '/'
288  ) . '/metadata.php?client_id=' . CLIENT_ID;
289  $info = $this->ui_factory->messageBox()->info(
290  sprintf(
291  $this->lng->txt('auth_saml_idps_info'),
292  'auth/saml/config/config.php',
293  'auth/saml/config/authsources.php',
294  $this->ui_renderer->render(
295  $this->ui_factory->link()->standard(
296  'https://simplesamlphp.org/docs/stable/simplesamlphp-sp',
297  'https://simplesamlphp.org/docs/stable/simplesamlphp-sp'
298  )
299  ),
300  $this->ui_renderer->render($this->ui_factory->link()->standard($federationMdUrl, $federationMdUrl))
301  )
302  );
303 
304  $table = new ilSamlIdpTableGUI(
305  $this,
306  $this->ui_factory,
307  $this->ui_renderer,
308  $this->lng,
309  $this->ctrl,
310  $this->httpState->request(),
311  new Factory(),
312  'handleTableActions',
313  $this->rbac->system()->checkAccess(self::PERMISSION_WRITE, $this->ref_id)
314  );
315  $this->tpl->setContent($this->ui_renderer->render([$info, $table->get()]));
316  }
317 
318  private function handleTableActions(): void
319  {
320  $action = $this->httpState->wrapper()->query()->retrieve(
321  'saml_idps_table_action',
322  $this->refinery->byTrying([
323  $this->refinery->kindlyTo()->string(),
324  $this->refinery->always('')
325  ])
326  );
327  match ($action) {
328  'showIdpSettings' => $this->showIdpSettings(),
329  'activateIdp' => $this->activateIdp(),
330  'deactivateIdp' => $this->deactivateIdp(),
331  'confirmDeleteIdp' => $this->confirmDeleteIdp(),
332  default => $this->ctrl->redirect($this, self::DEFAULT_CMD),
333  };
334  }
335 
336  private function deactivateIdp(): void
337  {
338  $this->ensureWriteAccess();
339 
340  $this->idp->setActive(false);
341  $this->idp->persist();
342 
343  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY), true);
344  $this->ctrl->redirect($this, self::DEFAULT_CMD);
345  }
346 
347  private function activateIdp(): void
348  {
349  $this->ensureWriteAccess();
350 
351  $this->idp->setActive(true);
352  $this->idp->persist();
353 
354  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY), true);
355  $this->ctrl->redirect($this, self::DEFAULT_CMD);
356  }
357 
358  private function setSubTabs(int $a_view_mode): void
359  {
360  switch ($a_view_mode) {
361  case self::VIEW_MODE_GLOBAL:
362  $this->tabs->addSubTabTarget(
363  'auth_saml_idps',
364  $this->ctrl->getLinkTarget($this, self::DEFAULT_CMD),
365  array_merge(
366  self::GLOBAL_ENTITY_COMMANDS,
367  [self::DEFAULT_CMD, 'showNewIdpForm', self::CMD_SAVE_NEW_IDP]
368  ),
369  self::class
370  );
371 
372  $this->tabs->addSubTabTarget(
373  'settings',
374  $this->ctrl->getLinkTarget($this, 'showSettings'),
375  ['showSettings', self::CMD_SAVE_SETTINGS],
376  self::class
377  );
378  break;
379 
380  case self::VIEW_MODE_SINGLE:
381  $this->tabs->clearTargets();
382  $this->tabs->setBackTarget(
383  $this->lng->txt('back'),
384  $this->ctrl->getLinkTarget($this, self::DEFAULT_CMD)
385  );
386 
387  $this->tabs->addSubTabTarget(
388  'auth_saml_idp_settings',
389  $this->ctrl->getLinkTarget($this, self::CMD_SHOW_IDP_SETTINGS),
390  [self::CMD_SHOW_IDP_SETTINGS, self::CMT_SAVE_IDP_SETTINGS],
391  self::class
392  );
393 
394  $this->tabs->addSubTabTarget(
395  self::LNG_AUTH_SAML_USER_MAPPING,
396  $this->ctrl->getLinkTarget($this, 'showUserAttributeMappingForm'),
397  ['showUserAttributeMappingForm', self::CMD_SAVE_USER_ATTRIBUTE_MAPPING],
398  self::class
399  );
400  break;
401  }
402  }
403 
404  private function initUserAttributeMapping(): void
405  {
406  $this->mapping = new ilExternalAuthUserAttributeMapping('saml', $this->idp->getIdpId());
407  }
408 
410  {
411  $form = new ilPropertyFormGUI();
412  $form->setFormAction($this->ctrl->getFormAction($this, self::CMD_SAVE_USER_ATTRIBUTE_MAPPING));
413  $form->setTitle($this->lng->txt(self::LNG_AUTH_SAML_USER_MAPPING));
414 
415  $usr_profile = new ilUserProfile();
416  foreach (array_keys($usr_profile->getStandardFields()) as $id) {
417  if (in_array($id, self::IGNORED_USER_FIELDS, true)) {
418  continue;
419  }
420 
421  $this->addAttributeRuleFieldToForm($form, $this->lng->txt($id), $id);
422  }
423 
424  foreach (ilUserDefinedFields::_getInstance()->getDefinitions() as $definition) {
425  $this->addAttributeRuleFieldToForm($form, $definition['field_name'], 'udf_' . $definition['field_id']);
426  }
427 
428  if (!$this->access->checkAccess(self::PERMISSION_WRITE, '', $this->ref_id)) {
429  foreach ($form->getItems() as $item) {
430  $item->setDisabled(true);
431  }
432  } else {
433  $form->addCommandButton(self::CMD_SAVE_USER_ATTRIBUTE_MAPPING, $this->lng->txt(self::CMD_SAVE));
434  }
435 
436  return $form;
437  }
438 
439  private function addAttributeRuleFieldToForm(
440  ilPropertyFormGUI $form,
441  string $field_label,
442  string $field_name
443  ): void {
444  $field = new ilTextInputGUI($field_label, $field_name);
445  $form->addItem($field);
446 
447  $update_automatically = new ilCheckboxInputGUI('', $field_name . self::PROP_UPDATE_SUFFIX);
448  $update_automatically->setOptionTitle($this->lng->txt('auth_saml_update_field_info'));
449  $update_automatically->setValue('1');
450  $form->addItem($update_automatically);
451  }
452 
453  private function saveUserAttributeMapping(): void
454  {
455  $this->ensureWriteAccess();
456 
457  $form = $this->getUserAttributeMappingForm();
458  if ($form->checkInput()) {
459  $this->mapping->delete();
460 
461  $usr_profile = new ilUserProfile();
462  foreach (array_keys($usr_profile->getStandardFields()) as $id) {
463  if (in_array($id, self::IGNORED_USER_FIELDS, true)) {
464  continue;
465  }
466 
467  $rule = $this->mapping->getEmptyRule();
468  $rule->setAttribute($id);
469  $rule->setExternalAttribute((string) $form->getInput($rule->getAttribute()));
470  $rule->updateAutomatically((bool) $form->getInput($rule->getAttribute() . self::PROP_UPDATE_SUFFIX));
471  $this->mapping[$rule->getAttribute()] = $rule;
472  }
473 
474  foreach (ilUserDefinedFields::_getInstance()->getDefinitions() as $definition) {
475  $rule = $this->mapping->getEmptyRule();
476  $rule->setAttribute('udf_' . $definition['field_id']);
477  $rule->setExternalAttribute((string) $form->getInput($rule->getAttribute()));
478  $rule->updateAutomatically((bool) $form->getInput($rule->getAttribute() . self::PROP_UPDATE_SUFFIX));
479  $this->mapping[$rule->getAttribute()] = $rule;
480  }
481 
482  $this->mapping->save();
483 
484  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY));
485  }
486 
487  $form->setValuesByPost();
488 
489  $this->showUserAttributeMappingForm($form);
490  }
491 
492  private function showUserAttributeMappingForm(ilPropertyFormGUI $form = null): void
493  {
494  $this->tabs->setSubTabActive(self::LNG_AUTH_SAML_USER_MAPPING);
495 
496  if (!($form instanceof ilPropertyFormGUI)) {
497  $form = $this->getUserAttributeMappingForm();
498  $data = [];
499  foreach ($this->mapping as $rule) {
500  $data[$rule->getAttribute()] = $rule->getExternalAttribute();
501  $data[$rule->getAttribute() . self::PROP_UPDATE_SUFFIX] = $rule->isAutomaticallyUpdated();
502  }
503  $form->setValuesByArray($data);
504  }
505 
506  $this->tpl->setContent($form->getHTML());
507  }
508 
509  private function getSettingsForm(): ilPropertyFormGUI
510  {
511  $form = new ilPropertyFormGUI();
512  $form->setFormAction($this->ctrl->getFormAction($this, self::CMD_SAVE_SETTINGS));
513  $form->setTitle($this->lng->txt('auth_saml_configure'));
514 
515  $show_login_form = new ilCheckboxInputGUI($this->lng->txt('auth_saml_login_form'), self::LNG_LOGIN_FORM);
516  $show_login_form->setInfo($this->lng->txt('auth_saml_login_form_info'));
517  $show_login_form->setValue('1');
518  $form->addItem($show_login_form);
519 
520  if (!$this->access->checkAccess(self::PERMISSION_WRITE, '', $this->ref_id)) {
521  foreach ($form->getItems() as $item) {
522  $item->setDisabled(true);
523  }
524  } else {
525  $form->addCommandButton(self::CMD_SAVE_SETTINGS, $this->lng->txt(self::CMD_SAVE));
526  }
527 
528  return $form;
529  }
530 
534  private function prepareRoleSelection(): array
535  {
536  $select = [];
537  $global_roles = array_map(
538  'intval',
540  $this->rbac->review()->getGlobalRoles(),
541  'object_data',
542  'title',
543  'obj_id'
544  )
545  );
546 
547  $select[0] = $this->lng->txt('links_select_one');
548  foreach ($global_roles as $role_id) {
549  $select[$role_id] = ilObject::_lookupTitle($role_id);
550  }
551 
552  return $select;
553  }
554 
555  private function saveSettings(): void
556  {
557  $this->ensureWriteAccess();
558 
559  $form = $this->getSettingsForm();
560  if ($form->checkInput()) {
561  ilSamlSettings::getInstance()->setLoginFormStatus((bool) $form->getInput(self::LNG_LOGIN_FORM));
562  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY));
563  }
564 
565  $form->setValuesByPost();
566 
567  $this->showSettings($form);
568  }
569 
570  private function showSettings(ilPropertyFormGUI $form = null): void
571  {
572  if (!($form instanceof ilPropertyFormGUI)) {
573  $form = $this->getSettingsForm();
574  $form->setValuesByArray([
575  self::LNG_LOGIN_FORM => ilSamlSettings::getInstance()->isDisplayedOnLoginPage(),
576  ]);
577  }
578 
579  $this->tpl->setContent($form->getHTML());
580  }
581 
583  {
584  $form = new ilPropertyFormGUI();
585  $form->setFormAction($this->ctrl->getFormAction($this, self::CMT_SAVE_IDP_SETTINGS));
586  $form->setTitle(sprintf($this->lng->txt('auth_saml_configure_idp'), $this->idp->getEntityId()));
587 
588  $idp = new ilTextInputGUI($this->lng->txt('auth_saml_idp'), 'entity_id');
589  $idp->setDisabled(true);
590  $form->addItem($idp);
591 
592  $this->addMetadataElement($form);
593 
594  $local = new ilCheckboxInputGUI($this->lng->txt('auth_allow_local'), 'allow_local_auth');
595  $local->setValue('1');
596  $local->setInfo($this->lng->txt('auth_allow_local_info'));
597  $form->addItem($local);
598 
599  $uid_claim = new ilTextInputGUI($this->lng->txt('auth_saml_uid_claim'), 'uid_claim');
600  $uid_claim->setInfo($this->lng->txt('auth_saml_uid_claim_info'));
601  $uid_claim->setRequired(true);
602  $form->addItem($uid_claim);
603 
604  $sync = new ilCheckboxInputGUI($this->lng->txt('auth_saml_sync'), 'sync_status');
605  $sync->setInfo($this->lng->txt('auth_saml_sync_info'));
606  $sync->setValue('1');
607 
608  $username_claim = new ilTextInputGUI($this->lng->txt('auth_saml_username_claim'), 'login_claim');
609  $username_claim->setInfo($this->lng->txt('auth_saml_username_claim_info'));
610  $username_claim->setRequired(true);
611  $sync->addSubItem($username_claim);
612 
613  $role = new ilSelectInputGUI($this->lng->txt('auth_saml_role_select'), 'default_role_id');
614  $role->setOptions($this->prepareRoleSelection());
615  $role->setRequired(true);
616  $sync->addSubItem($role);
617 
618  $migr = new ilCheckboxInputGUI($this->lng->txt('auth_saml_migration'), 'account_migr_status');
619  $migr->setInfo($this->lng->txt('auth_saml_migration_info'));
620  $migr->setValue('1');
621  $sync->addSubItem($migr);
622  $form->addItem($sync);
623 
624  if (!$this->access->checkAccess(self::PERMISSION_WRITE, '', $this->ref_id)) {
625  foreach ($form->getItems() as $item) {
626  $item->setDisabled(true);
627  }
628  } else {
629  $form->addCommandButton(self::CMT_SAVE_IDP_SETTINGS, $this->lng->txt(self::CMD_SAVE));
630  }
631  $form->addCommandButton(self::DEFAULT_CMD, $this->lng->txt(self::LNG_CANCEL));
632 
633  return $form;
634  }
635 
636  private function showIdpSettings(ilPropertyFormGUI $form = null): void
637  {
638  $this->tabs->setSubTabActive('auth_saml_idp_settings');
639 
640  if (null === $form) {
641  $form = $this->getIdpSettingsForm();
642  $data = $this->idp->toArray();
643  $this->populateWithMetadata($this->idp, $data);
644  $form->setValuesByArray($data);
645  } else {
646  $form->setValuesByPost();
647  }
648 
649  $this->help->setSubScreenId('edit_idp');
650 
651  $this->tpl->setContent($form->getHTML());
652  }
653 
654  private function saveIdpSettings(): void
655  {
656  $this->ensureWriteAccess();
657 
658  $form = $this->getIdpSettingsForm();
659  if ($form->checkInput()) {
660  $this->idp->bindForm($form);
661  $this->idp->persist();
662  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY));
663 
664  $this->storeMetadata($this->idp, $form->getInput(self::METADATA_STORAGE_KEY));
665  }
666 
667  $this->showIdpSettings($form);
668  }
669 
670  private function getIdpForm(): ilPropertyFormGUI
671  {
672  $form = new ilPropertyFormGUI();
673  $form->setFormAction($this->ctrl->getFormAction($this, self::CMD_SAVE_NEW_IDP));
674  $form->setTitle($this->lng->txt('auth_saml_add_idp_btn'));
675 
676  $this->addMetadataElement($form);
677 
678  $form->addCommandButton(self::CMD_SAVE_NEW_IDP, $this->lng->txt(self::CMD_SAVE));
679  $form->addCommandButton('listIdps', $this->lng->txt(self::LNG_CANCEL));
680 
681  return $form;
682  }
683 
684  private function saveNewIdp(): void
685  {
686  $this->ensureWriteAccess();
687 
688  $form = $this->getIdpForm();
689  if ($form->checkInput()) {
690  $idp = new ilSamlIdp();
691  $idp->bindForm($form);
692  $idp->persist();
693 
694  $this->storeMetadata($idp, $form->getInput(self::METADATA_STORAGE_KEY));
695 
696  $this->tpl->setOnScreenMessage(
697  self::MESSAGE_TYPE_SUCCESS,
698  $this->lng->txt(self::LNG_SAVED_SUCCESSFULLY),
699  true
700  );
701  $this->ctrl->setParameter($this, self::REQUEST_PARAM_SAML_IDP_ID, $idp->getIdpId());
702  $this->ctrl->redirect($this, self::CMD_SHOW_IDP_SETTINGS);
703  }
704 
705  $this->showNewIdpForm($form);
706  }
707 
708  private function showNewIdpForm(ilPropertyFormGUI $form = null): void
709  {
710  $this->ensureWriteAccess();
711 
712  if (null === $form) {
713  $form = $this->getIdpForm();
714  } else {
715  $form->setValuesByPost();
716  }
717 
718  $this->help->setSubScreenId('create_idp');
719 
720  $this->tpl->setContent($form->getHTML());
721  }
722 
723  private function addMetadataElement(ilPropertyFormGUI $form): void
724  {
725  $metadata = new ilSamlIdpMetadataInputGUI(
726  $this->lng->txt('auth_saml_add_idp_md_label'),
727  self::METADATA_STORAGE_KEY,
729  new Factory(),
731  )
732  );
733  $metadata->setInfo($this->lng->txt('auth_saml_add_idp_md_info'));
734  $metadata->setRows(20);
735  $metadata->setRequired(true);
736 
737  $purifier = new ilHtmlPurifierComposite();
738  $purifier->addPurifier(new ilSamlIdpMetadataPurifier());
739 
740  $metadata->setPurifier($purifier);
741  $metadata->usePurifier(true);
742  $form->addItem($metadata);
743  }
744 
745  private function populateWithMetadata(ilSamlIdp $idp, array &$data): void
746  {
747  $idpDisco = $this->samlAuth->getIdpDiscovery();
748 
749  $data[self::METADATA_STORAGE_KEY] = $idpDisco->fetchIdpMetadata($idp->getIdpId());
750  }
751 
752  private function storeMetadata(ilSamlIdp $idp, string $metadata): void
753  {
754  $idpDisco = $this->samlAuth->getIdpDiscovery();
755  $idpDisco->storeIdpMetadata($idp->getIdpId(), $metadata);
756  }
757 
758  private function confirmDeleteIdp(): void
759  {
760  $this->ensureWriteAccess();
761 
762  $confirmation = new ilConfirmationGUI();
763  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'deleteIdp'));
764  $confirmation->setConfirm($this->lng->txt('confirm'), 'deleteIdp');
765  $confirmation->setCancel($this->lng->txt(self::LNG_CANCEL), self::DEFAULT_CMD);
766  $confirmation->setHeaderText($this->lng->txt('auth_saml_sure_delete_idp'));
767  $confirmation->addItem(self::REQUEST_PARAM_SAML_IDP_IDS, (string) $this->idp->getIdpId(), $this->idp->getEntityId());
768 
769  $this->tpl->setContent($confirmation->getHTML());
770  }
771 
772  private function deleteIdp(): void
773  {
774  $this->ensureWriteAccess();
775 
776  $idpDisco = $this->samlAuth->getIdpDiscovery();
777  $idpDisco->deleteIdpMetadata($this->idp->getIdpId());
778 
779  $this->idp->delete();
780 
781  $this->tpl->setOnScreenMessage(self::MESSAGE_TYPE_SUCCESS, $this->lng->txt('auth_saml_deleted_idp'), true);
782 
783  $this->ctrl->setParameter($this, self::REQUEST_PARAM_SAML_IDP_ID, null);
784  $this->ctrl->redirect($this, self::DEFAULT_CMD);
785  }
786 }
showNewIdpForm(ilPropertyFormGUI $form=null)
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
storeMetadata(ilSamlIdp $idp, string $metadata)
readonly ilErrorHandling $error_handler
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly GlobalHttpState $httpState
readonly ilHelpGUI $help
setSubTabs(int $a_view_mode)
Help GUI class.
Composite for nesting multiple purifiers.
showSettings(ilPropertyFormGUI $form=null)
Class ilUserProfile.
setOptions(array $a_options)
populateWithMetadata(ilSamlIdp $idp, array &$data)
static getInstanceByIdpId(int $a_idp_id)
$ref_id
Definition: ltiauth.php:66
getSafePostCommands()
This method must return a list of safe POST commands.
Class ilExternalAuthUserAttributeMapping.
showIdpSettings(ilPropertyFormGUI $form=null)
readonly RBACServices $rbac
static _lookupTitle(int $obj_id)
readonly ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly ilToolbarGUI $toolbar
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:25
Provides fluid interface to RBAC services.
readonly ILIAS UI Renderer $ui_renderer
Builds data types.
Definition: Factory.php:35
ilExternalAuthUserAttributeMapping $mapping
ensureAccess(string $operation)
__construct(private readonly int $ref_id)
static _sortIds(array $a_ids, string $a_table, string $a_field, string $a_id_name)
Function that sorts ids by a given table field using WHERE IN E.g: __sort(array(6,7),&#39;usr_data&#39;,&#39;lastname&#39;,&#39;usr_id&#39;) => sorts by lastname.
readonly ilCtrlInterface $ctrl
readonly Refinery $refinery
readonly ILIAS UI Factory $ui_factory
Class ilSamlIdp.
readonly ilLanguage $lng
Class ilSamlSettingsGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addAttributeRuleFieldToForm(ilPropertyFormGUI $form, string $field_label, string $field_name)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
readonly ilTabsGUI $tabs
showUserAttributeMappingForm(ilPropertyFormGUI $form=null)
readonly ilAccessHandler $access
Class ilSamlIdpMetadataPurifier.
Interface ilCtrlSecurityInterface provides ilCtrl security information.
addMetadataElement(ilPropertyFormGUI $form)