ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilAuthLogoutBehaviourGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\UI\Factory as UIFactory;
22use ILIAS\UI\Renderer as UIRenderer;
23use ILIAS\HTTP\Services as HttpService;
24use ILIAS\Refinery\Factory as Refinery;
25use Psr\Http\Message\ServerRequestInterface;
29
35{
36 private ilCtrl $ctrl;
38 private HttpService $http;
39 private Refinery $refinery;
41 private UIFactory $ui_factory;
43 private UIRenderer $ui_renderer;
47
48 public function __construct(private readonly int $ref_id)
49 {
50 global $DIC;
51 $this->ctrl = $DIC->ctrl();
52 $this->http = $DIC->http();
53 $this->lng = $DIC->language();
54 $this->refinery = $DIC->refinery();
55 $this->tpl = $DIC->ui()->mainTemplate();
56 $this->ui_factory = $DIC->ui()->factory();
57 $this->rbac_system = $DIC->rbac()->system();
58 $this->ui_renderer = $DIC->ui()->renderer();
59 $this->ilErr = $DIC->error();
60 $this->lng->loadLanguageModule('auth');
61 $this->settings = new ilSetting('auth');
62 $this->configurable_logout_target = new ConfigurableLogoutTarget(
63 $this->ctrl,
64 $this->settings,
65 $DIC->access()
66 );
67 }
68
69 public function executeCommand(): void
70 {
71 $cmd = $this->ctrl->getCmd();
72 if (method_exists($this, $cmd)) {
73 $this->$cmd();
74 }
75 $this->showForm();
76 }
77
78 public function getForm(
79 ?ServerRequestInterface $request = null,
80 array $errors = []
81 ): StandardForm {
82 $logout_group = $this->ui_factory->input()->field()
83 ->group(
84 [],
85 $this->lng->txt('destination_logout_screen')
86 );
87
88 $login_group = $this->ui_factory->input()->field()
89 ->group(
90 [],
91 $this->lng->txt('destination_login_screen'),
92 $this->lng->txt('destination_login_screen_info')
93 );
94
95 $ref_id = $this->ui_factory->input()->field()
96 ->numeric($this->lng->txt('destination_internal_ressource_ref_id'))
97 ->withAdditionalTransformation(
98 $this->refinery->custom()->constraint(
99 fn($value) => $this->configurable_logout_target->isInRepository($value),
100 fn(callable $txt, $value) => $txt('logout_behaviour_invalid_ref_id', $value)
101 )
102 )
103 ->withAdditionalTransformation(
104 $this->refinery->custom()->constraint(
105 fn($value) => $this->configurable_logout_target->isAnonymousAccessible(
106 $value
107 ),
108 fn(callable $txt, $value) => $txt(
109 'logout_behaviour_ref_id_no_access',
110 $value
111 )
112 )
113 )
114 ->withValue($this->settings->get('logout_behaviour_ref_id', ''));
115 if (isset($errors['ref_id'])) {
116 $ref_id = $ref_id->withError($errors['ref_id']);
117 }
118
119 $internal_group = $this->ui_factory->input()->field()
120 ->group(
121 ['ref_id' => $ref_id],
122 $this->lng->txt('destination_internal_ressource')
123 );
124
125 $url = $this->settings->get('logout_behaviour_url', '');
126 $html = $this->ui_factory->input()->field()
127 ->url($this->lng->txt('destination_external_ressource_url'))
128 ->withAdditionalTransformation(
129 $this->refinery->custom()->constraint(
130 fn($value) => $this->configurable_logout_target->isValidExternalResource(
131 (string) $value
132 ),
133 fn(callable $txt, $value) => $txt('logout_behaviour_invalid_url', $value)
134 )
135 )
136 ->withValue($url);
137 if (isset($errors['url'])) {
138 $html = $html->withError($errors['url']);
139 }
140
141 $external_group = $this->ui_factory->input()->field()
142 ->group(
143 ['url' => $html],
144 $this->lng->txt('destination_external_ressource')
145 );
146
147 $logout_behaviour_switchable_group = $this->ui_factory->input()->field()
148 ->switchableGroup(
149 [
150 LogoutDestinations::LOGOUT_SCREEN->value => $logout_group,
151 LogoutDestinations::LOGIN_SCREEN->value => $login_group,
152 ConfigurableLogoutTarget::INTERNAL_RESSOURCE => $internal_group,
153 ConfigurableLogoutTarget::EXTERNAL_RESSOURCE => $external_group
154 ],
155 $this->lng->txt('destination_after_logout')
156 )
157 ->withValue(
158 $this->settings->get(
159 'logout_behaviour',
160 LogoutDestinations::LOGOUT_SCREEN->value
161 )
162 );
163
164 $access = $this->rbac_system->checkAccess('write', $this->ref_id);
165 if (!$access) {
166 $logout_behaviour_switchable_group = $logout_behaviour_switchable_group->withDisabled(true);
167 }
168
169 $section = $this->ui_factory->input()->field()
170 ->section(
171 ['logout_behaviour_settings' => $logout_behaviour_switchable_group],
172 $this->lng->txt('logout_behaviour_settings')
173 );
174
175 $form = $this->ui_factory->input()->container()->form()
176 ->standard(
177 $access ?
178 $this->ctrl->getFormAction($this, 'saveForm') :
179 $this->ctrl->getFormAction($this, 'showForm'),
180 ['logout_behaviour' => $section]
181 );
182 if ($request) {
183 $form = $form->withRequest($request);
184 }
185
186 if (!$access) {
187 $form = $form->withSubmitLabel($this->lng->txt('refresh'));
188 }
189
190 return $form;
191 }
192
193 public function showForm(): void
194 {
195 $mode = $this->settings->get('logout_behaviour', LogoutDestinations::LOGOUT_SCREEN->value);
196 $ref_id = (int) $this->settings->get('logout_behaviour_ref_id', '');
197 $content = '';
198 if ($mode === ConfigurableLogoutTarget::INTERNAL_RESSOURCE &&
199 !$this->configurable_logout_target->isValidInternalResource($ref_id)) {
200 $content .= $this->ui_renderer->render(
201 $this->ui_factory->messageBox()->failure(
202 $this->lng->txt('logout_behaviour_ref_id_valid_status_changed')
203 )
204 );
205 }
206 $content .= $this->ui_renderer->render($this->getForm());
207
208 $this->tpl->setContent($content);
209 }
210
211 public function saveForm(): void
212 {
213 if (!$this->rbac_system->checkAccess('write', $this->ref_id)) {
214 $this->ilErr->raiseError($this->lng->txt('permission_denied'), $this->ilErr->WARNING);
215 return;
216 }
217
218 $form = $this->getForm();
219 $form = $form->withRequest($this->http->request());
220 $section = $form->getInputs()['logout_behaviour'];
221 $group = $section->getInputs()['logout_behaviour_settings'];
222 $ref_id = $group->getInputs()[ConfigurableLogoutTarget::INTERNAL_RESSOURCE]->getInputs()['ref_id'];
223 $url = $group->getInputs()[ConfigurableLogoutTarget::EXTERNAL_RESSOURCE]->getInputs()['url'];
224
225 $data = $form->getData();
226 if (!$data || $form->getError() || $ref_id->getError() || $url->getError()) {
227 $errors = [];
228 if ($ref_id->getError()) {
229 $errors['ref_id'] = $ref_id->getError();
230 }
231 if ($url->getError()) {
232 $errors['url'] = $url->getError();
233 }
234 $this->tpl->setContent($this->ui_renderer->render($this->getForm($this->http->request(), $errors)));
235 $this->tpl->printToStdout();
236
237 $this->http->close();
238 }
239 if (isset($data['logout_behaviour']['logout_behaviour_settings'][0])) {
240 $mode = $data['logout_behaviour']['logout_behaviour_settings'][0];
241
242 switch ($mode) {
243 case LogoutDestinations::LOGIN_SCREEN->value:
244 case LogoutDestinations::LOGOUT_SCREEN->value:
245 break;
246
247 case ConfigurableLogoutTarget::INTERNAL_RESSOURCE:
248 $this->settings->set(
249 'logout_behaviour_ref_id',
250 (string) ($data['logout_behaviour']['logout_behaviour_settings'][1]['ref_id'] ?? '')
251 );
252 break;
253 case ConfigurableLogoutTarget::EXTERNAL_RESSOURCE:
254
255 $url = $data['logout_behaviour']['logout_behaviour_settings'][1]['url'] ?? '';
256 $this->settings->set('logout_behaviour_url', (string) $url);
257 break;
258 }
259 $this->settings->set('logout_behaviour', $mode);
260 }
261 $this->ctrl->redirect($this, 'showForm');
262 }
263}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
@ilCtrl_isCalledBy ilAuthLogoutBehaviourGUI: ilObjAuthSettingsGUI @ilCtrl_Calls ilAuthLogoutBehaviour...
getForm(?ServerRequestInterface $request=null, array $errors=[])
__construct(private readonly int $ref_id)
ConfigurableLogoutTarget $configurable_logout_target
Class ilCtrl provides processing control methods.
Error Handling & global info handling.
language handling
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
ILIAS Setting Class.
This describes a standard form.
Definition: Standard.php:30
An entity that renders components to a string output.
Definition: Renderer.php:31
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:70