ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilShibbolethSettingsForm.php
Go to the documentation of this file.
1 <?php
2 
21 
23 
30 {
31  private $refinery;
32 
33  protected ilCtrl $ctrl;
34  protected \ILIAS\UI\Factory $ui;
35  protected \ILIAS\UI\Renderer $renderer;
37  protected string $action;
38  protected \Psr\Http\Message\RequestInterface $request;
39  protected ilLanguage $lng;
41 
42  public function __construct(ilShibbolethSettings $settings, string $action)
43  {
47  global $DIC;
48  $this->ui = $DIC->ui()->factory();
49  $this->renderer = $DIC->ui()->renderer();
50  $this->request = $DIC->http()->request();
51  $this->ctrl = $DIC->ctrl();
52  $this->lng = $DIC->language();
53  $this->settings = $settings;
54  $this->action = $action;
55  $this->refinery = $DIC->refinery();
56  $this->initForm();
57  }
58 
59  protected function txt(string $var): string
60  {
61  return $this->lng->txt($var);
62  }
63 
64  protected function infoTxt(string $var): string
65  {
66  return $this->txt($var . '_info');
67  }
68 
69  public function getHTML(): string
70  {
71  return $this->renderer->render($this->form);
72  }
73 
74  public function initForm(): void
75  {
76  $field = $this->ui->input()->field();
77  $custom_trafo = fn (callable $c) => $this->refinery->custom()->transformation($c);
79  $read_me_link = "./Services/AuthShibboleth/README.SHIBBOLETH.txt";
80  $active = $field->checkbox($this->txt('shib_active'), $this->lng->txt("auth_shib_instructions"))
81  ->withValue($this->settings->isActive())
82  ->withAdditionalTransformation($custom_trafo(function ($v): void {
83  $this->settings->setActive((bool) $v);
84  }));
85 
86  $auth_allow_local = $field->checkbox($this->txt('auth_allow_local'))
87  ->withValue($this->settings->isLocalAuthAllowed())
88  ->withAdditionalTransformation($custom_trafo(function ($v): void {
89  $this->settings->setAllowLocalAuth((bool) $v);
90  }));
91 
92  $admin_must_activate = $field->checkbox(
93  $this->txt('shib_activate_new'),
94  $this->lng->txt("shib_activate_new_info")
95  )->withValue($this->settings->adminMustActivate())
97  $custom_trafo(function ($v): void {
98  $this->settings->setAdminMustActivate((bool) $v);
99  })
100  );
101 
102  $default_user_role = $field->select($this->txt('shib_user_default_role'), $this->getRoles())
103  ->withRequired(true)
104  ->withValue($this->settings->getDefaultRole())
105  ->withAdditionalTransformation($custom_trafo(function ($v): void {
106  $this->settings->setDefaultRole((int) $v);
107  }));
108 
109  $basic_section = $field->section([
110  $active,
111  $auth_allow_local,
112  $admin_must_activate,
113  $default_user_role,
114  ], $this->txt('shib'));
115 
116  // Federation
117  $federation_name = $field->text($this->txt('shib_federation_name'))
118  ->withRequired(true)
119  ->withValue($this->settings->getFederationName())
120  ->withAdditionalTransformation($custom_trafo(function ($v): void {
121  $this->settings->setFederationName((string) $v);
122  }));
123 
124  $login_type = $field->switchableGroup([
125  'internal_wayf' => $field->group([
126  $field->textarea('', $this->txt('shib_idp_list'))
127  ->withValue($this->settings->getIdPList())
128  ->withAdditionalTransformation($custom_trafo(function ($v): void {
129  $this->settings->setIdPList((string) $v);
130  }))
131  ], $this->txt('shib_login_internal_wayf')),
132  'external_wayf' => $field->group([
133  $field->text('', $this->txt('shib_login_button'))
134  ->withValue($this->settings->getLoginButton())
135  ->withAdditionalTransformation($custom_trafo(function ($v): void {
136  $this->settings->setLoginButton((string) $v);
137  }))
138  ], $this->txt('shib_login_external_wayf')),
139  'embedded_wayf' => $field->group([], $this->txt('shib_login_embedded_wayf'))
140  ->withByline($this->txt('shib_login_embedded_wayf_description')),
141  ], $this->txt('shib_login_type'))
142  ->withRequired(true)
143  ->withValue($this->settings->getOrganisationSelectionType())
144  ->withAdditionalTransformation($custom_trafo(function ($v): void {
145  $this->settings->setOrganisationSelectionType($v[0]);
146  }));
147 
148  $instructions = $field->textarea($this->txt('auth_login_instructions'))
149  ->withValue($this->settings->get('login_instructions', ''))
150  ->withAdditionalTransformation($custom_trafo(function ($v): void {
151  $this->settings->set('login_instructions', htmlspecialchars_decode($v));
152  }));
153 
154  $data_manipulation = $field->text($this->txt('shib_data_conv'))
155  ->withValue($this->settings->get('data_conv'))
156  ->withAdditionalTransformation($custom_trafo(function ($v): void {
157  $this->settings->set('data_conv', (string) $v);
158  }));
159 
160  $federation_section = $field->section([
161  $federation_name,
162  $login_type,
163  $instructions,
164  $data_manipulation
165  ], '');
166 
167  // User Fields
168  $fields = [];
169  $fields[] = $field->text($this->txt('shib_login'))
170  ->withValue($this->settings->get('shib_login'))
171  ->withRequired(true)
172  ->withAdditionalTransformation($custom_trafo(function ($v): void {
173  $this->settings->set('shib_login', (string) $v);
174  }));
175  foreach ($this->settings->getUserFields() as $field_name => $required) {
176  $fields[] = $field->text($this->txt($field_name))
177  ->withValue($this->settings->get($field_name))
178  ->withRequired($required)
179  ->withAdditionalTransformation($custom_trafo(function ($v) use ($field_name): void {
180  $this->settings->set($field_name, (string) $v);
181  }));
182  $fields[] = $field->checkbox($this->txt('shib_update'))
183  ->withValue((bool) $this->settings->get('update_' . $field_name))
184  ->withAdditionalTransformation($custom_trafo(function ($v) use ($field_name): void {
185  $this->settings->set('update_' . $field_name, (string) $v);
186  }));
187  }
188 
189  $user_fields = $field->section(
190  $fields,
191  ''
192  );
193 
194  // COMPLETE FORM
195 
196  $this->form = $this->ui->input()->container()->form()->standard(
197  $this->action,
198  [
199  $basic_section,
200  $federation_section,
201  $user_fields
202  ]
203  )->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
204  return true;
205  }));
206  }
207 
208  public function setValuesByPost(): void
209  {
210  $request = $this->request->withParsedBody(array_map('htmlspecialchars', $this->request->getParsedBody()));
211  $this->form = $this->form->withRequest($request);
212  }
213 
214  protected function fillObject(): bool
215  {
216  return $this->form->getData() === true;
217  }
218 
219  public function saveObject(): bool
220  {
221  if (!$this->fillObject()) {
222  return false;
223  }
224  $this->settings->store();
225  return true;
226  }
227 
231  protected function getRoles(int $filter = ilRbacReview::FILTER_ALL_GLOBAL): array
232  {
233  global $DIC;
234  $opt = [];
235  foreach ($DIC->rbac()->review()->getRolesByFilter($filter) as $role) {
236  $opt[$role['obj_id']] = $role['title'] . ' (' . $role['obj_id'] . ')';
237  }
238 
239  return $opt;
240  }
241 }
Psr Http Message RequestInterface $request
Class ilShibbolethSettingsForm.
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
This describes a standard form.
Definition: Standard.php:26
form( $class_path, string $cmd, string $submit_caption="")
getRoles(int $filter=ilRbacReview::FILTER_ALL_GLOBAL)
ILIAS UI Component Input Container Form Standard $form
Class ilShibbolethSettings.