ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilAuthLogoutBehaviourGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
35 {
36  private ilCtrl $ctrl;
37  private ilLanguage $lng;
38  private HttpService $http;
45 
46  public function __construct()
47  {
48  global $DIC;
49  $this->ctrl = $DIC->ctrl();
50  $this->http = $DIC->http();
51  $this->lng = $DIC->language();
52  $this->refinery = $DIC->refinery();
53  $this->tpl = $DIC->ui()->mainTemplate();
54  $this->ui_factory = $DIC->ui()->factory();
55  $this->ui_renderer = $DIC->ui()->renderer();
56  $this->lng->loadLanguageModule('auth');
57  $this->settings = new ilSetting('auth');
58  $this->configurable_logout_target = new ConfigurableLogoutTarget(
59  $this->ctrl,
60  $this->settings,
61  $DIC->access()
62  );
63  }
64 
65  public function executeCommand(): void
66  {
67  $cmd = $this->ctrl->getCmd();
68  if (method_exists($this, $cmd)) {
69  $this->$cmd();
70  }
71  $this->showForm();
72  }
73 
74  public function getForm(
75  ?ServerRequestInterface $request = null,
76  array $errors = []
77  ): StandardForm {
78  $logout_group = $this->ui_factory->input()->field()
79  ->group(
80  [],
81  $this->lng->txt('destination_logout_screen')
82  );
83 
84  $login_group = $this->ui_factory->input()->field()
85  ->group(
86  [],
87  $this->lng->txt('destination_login_screen'),
88  $this->lng->txt('destination_login_screen_info')
89  );
90 
91  $ref_id = $this->ui_factory->input()->field()
92  ->numeric($this->lng->txt('destination_internal_ressource_ref_id'))
94  $this->refinery->custom()->constraint(
95  fn($value) => $this->configurable_logout_target->isInRepository($value),
96  fn(callable $txt, $value) => $txt('logout_behaviour_invalid_ref_id', $value)
97  )
98  )
99  ->withAdditionalTransformation(
100  $this->refinery->custom()->constraint(
101  fn($value) => $this->configurable_logout_target->isAnonymousAccessible(
102  $value
103  ),
104  fn(callable $txt, $value) => $txt(
105  'logout_behaviour_ref_id_no_access',
106  $value
107  )
108  )
109  )
110  ->withValue($this->settings->get('logout_behaviour_ref_id', ''));
111  if (isset($errors['ref_id'])) {
112  $ref_id = $ref_id->withError($errors['ref_id']);
113  }
114 
115  $internal_group = $this->ui_factory->input()->field()
116  ->group(
117  ['ref_id' => $ref_id],
118  $this->lng->txt('destination_internal_ressource')
119  );
120 
121  $url = $this->settings->get('logout_behaviour_url', '');
122  $html = $this->ui_factory->input()->field()
123  ->url($this->lng->txt('destination_external_ressource_url'))
125  $this->refinery->custom()->constraint(
126  fn($value) => $this->configurable_logout_target->isValidExternalResource(
127  (string) $value
128  ),
129  fn(callable $txt, $value) => $txt('logout_behaviour_invalid_url', $value)
130  )
131  )
132  ->withValue($url);
133  if (isset($errors['url'])) {
134  $html = $html->withError($errors['url']);
135  }
136 
137  $external_group = $this->ui_factory->input()->field()
138  ->group(
139  ['url' => $html],
140  $this->lng->txt('destination_external_ressource')
141  );
142 
143  $logout_behaviour_switchable_group = $this->ui_factory->input()->field()
144  ->switchableGroup(
145  [
146  LogoutDestinations::LOGOUT_SCREEN->value => $logout_group,
147  LogoutDestinations::LOGIN_SCREEN->value => $login_group,
148  ConfigurableLogoutTarget::INTERNAL_RESSOURCE => $internal_group,
149  ConfigurableLogoutTarget::EXTERNAL_RESSOURCE => $external_group
150  ],
151  $this->lng->txt('destination_after_logout')
152  )
153  ->withValue(
154  $this->settings->get(
155  'logout_behaviour',
156  LogoutDestinations::LOGOUT_SCREEN->value
157  )
158  );
159 
160  $section = $this->ui_factory->input()->field()
161  ->section(
162  ['logout_behaviour_settings' => $logout_behaviour_switchable_group],
163  $this->lng->txt('logout_behaviour_settings')
164  );
165 
166  $form = $this->ui_factory->input()->container()->form()
167  ->standard(
168  $this->ctrl->getFormAction($this, 'saveForm'),
169  ['logout_behaviour' => $section]
170  );
171  if ($request) {
172  $form = $form->withRequest($request);
173  }
174 
175  return $form;
176  }
177 
178  public function showForm(): void
179  {
180  $mode = $this->settings->get('logout_behaviour', LogoutDestinations::LOGOUT_SCREEN->value);
181  $ref_id = (int) $this->settings->get('logout_behaviour_ref_id', '');
182  $content = '';
183  if ($mode === ConfigurableLogoutTarget::INTERNAL_RESSOURCE &&
184  !$this->configurable_logout_target->isValidInternalResource($ref_id)) {
185  $content .= $this->ui_renderer->render(
186  $this->ui_factory->messageBox()->failure(
187  $this->lng->txt('logout_behaviour_ref_id_valid_status_changed')
188  )
189  );
190  }
191  $content .= $this->ui_renderer->render($this->getForm());
192 
193  $this->tpl->setContent($content);
194  }
195 
196  public function saveForm(): void
197  {
198  $form = $this->getForm();
199  $form = $form->withRequest($this->http->request());
200  $section = $form->getInputs()['logout_behaviour'];
201  $group = $section->getInputs()['logout_behaviour_settings'];
202  $ref_id = $group->getInputs()[ConfigurableLogoutTarget::INTERNAL_RESSOURCE]->getInputs()['ref_id'];
203  $url = $group->getInputs()[ConfigurableLogoutTarget::EXTERNAL_RESSOURCE]->getInputs()['url'];
204 
205  $data = $form->getData();
206  if (!$data || $form->getError() || $ref_id->getError() || $url->getError()) {
207  $errors = [];
208  if ($ref_id->getError()) {
209  $errors['ref_id'] = $ref_id->getError();
210  }
211  if ($url->getError()) {
212  $errors['url'] = $url->getError();
213  }
214  $this->tpl->setContent($this->ui_renderer->render($this->getForm($this->http->request(), $errors)));
215  $this->tpl->printToStdout();
216 
217  $this->http->close();
218  }
219  if (isset($data['logout_behaviour']['logout_behaviour_settings'][0])) {
220  $mode = $data['logout_behaviour']['logout_behaviour_settings'][0];
221 
222  switch ($mode) {
223  case LogoutDestinations::LOGIN_SCREEN->value:
224  case LogoutDestinations::LOGOUT_SCREEN->value:
225  break;
226 
227  case ConfigurableLogoutTarget::INTERNAL_RESSOURCE:
228  $this->settings->set(
229  'logout_behaviour_ref_id',
230  (string) ($data['logout_behaviour']['logout_behaviour_settings'][1]['ref_id'] ?? '')
231  );
232  break;
233  case ConfigurableLogoutTarget::EXTERNAL_RESSOURCE:
234 
235  $url = $data['logout_behaviour']['logout_behaviour_settings'][1]['url'] ?? '';
236  $this->settings->set('logout_behaviour_url', (string) $url);
237  break;
238  }
239  $this->settings->set('logout_behaviour', $mode);
240  }
241  $this->ctrl->redirect($this, 'showForm');
242  }
243 }
ConfigurableLogoutTarget $configurable_logout_target
$url
Definition: shib_logout.php:68
ilAuthLogoutBehaviourGUI: ilObjAuthSettingsGUI ilAuthLogoutBehaviourGUI: ilLoginPageGUI ...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$txt
Definition: error.php:31
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
getForm(?ServerRequestInterface $request=null, array $errors=[])