ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ApacheAuthSettingsForm.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilAuthUtils;
24 use ilCtrlInterface;
31 use ilLanguage;
32 use ilLDAPServer;
33 use ilObject;
34 use ilRbacReview;
35 use ilRbacSystem;
36 
37 readonly class ApacheAuthSettingsForm
38 {
40  private ilLanguage $lng;
45 
49  public function __construct(
50  private int $ref_id,
51  private object $parentObject,
52  private string $show_command,
53  private string $save_command,
54  private array $values = [],
55  ?UIFactory $ui_factory = null,
56  ?ilLanguage $lng = null,
57  ?ilRbacSystem $rbac_system = null,
58  ?ilRbacReview $rbac_review = null,
59  ?ilCtrlInterface $ctrl = null
60  ) {
61  global $DIC;
62 
63  $this->ui_factory = $ui_factory ?? $DIC->ui()->factory();
64  $this->lng = $lng ?? $DIC->language();
65  $this->rbac_system = $rbac_system ?? $DIC->rbac()->system();
66  $this->rbac_review = $rbac_review ?? $DIC->rbac()->review();
67  $this->ctrl = $ctrl ?? $DIC->ctrl();
68 
69  $this->ui_field = $this->ui_factory->input()->field();
70 
71  $this->lng->loadLanguageModule('auth');
72  }
73 
74  public function buildForm(): StandardForm
75  {
76  $access = $this->rbac_system->checkAccess('write', $this->ref_id);
77 
78  $form = $this->ui_factory->input()->container()->form()->standard(
79  $this->ctrl->getFormAction($this->parentObject, $access ? $this->save_command : $this->show_command),
80  [
81  'apache_enable_auth' => $this->buildEnableAuthInput(),
82  'apache_enable_local' => $this->ui_field->checkbox($this->lng->txt('apache_enable_local'))
83  ->withValue((bool) ($this->values['apache_enable_local'] ?? true)),
84  'apache_enable_ldap' => $this->buildLdapEnableInput(),
85  'apache_auth_indicator_name' => $this->ui_field->text($this->lng->txt('apache_auth_indicator_name'))
86  ->withRequired(true)
87  ->withValue($this->values['apache_auth_indicator_name'] ?? ''),
88  'apache_auth_indicator_value' => $this->ui_field->text($this->lng->txt('apache_auth_indicator_value'))
89  ->withRequired(true)
90  ->withValue($this->values['apache_auth_indicator_value'] ?? ''),
91  'apache_auth_enable_override_login_page' => $this->buildAuthEnableOverrideLoginPageInput(),
92  'apache_auth_authenticate_on_login_page' => $this->ui_field->checkbox($this->lng->txt('apache_auth_authenticate_on_login_page'))
93  ->withValue((bool) ($this->values['apache_auth_authenticate_on_login_page'] ?? true)),
94  'apache_auth_username_config' => $this->ui_field->section([
95  'apache_auth_username_config_type' => $this->buildAuthUsernameConfigTypeInput()
96  ], $this->lng->txt('apache_auth_username_config')),
97  'apache_auth_security' => $this->ui_field->section([
98  'apache_auth_domains' => $this->ui_field->textarea(
99  $this->lng->txt('apache_auth_domains'),
100  $this->lng->txt('apache_auth_domains_description')
101  )->withValue($this->values['apache_auth_domains'] ?? '')
102  ], $this->lng->txt('apache_auth_security'))
103  ]
104  );
105 
106  if (!$access) {
107  $form = $form->withSubmitLabel($this->lng->txt('refresh'));
108  }
109 
110  return $form;
111  }
112 
114  {
115  $roleOptions = [];
116  foreach ($this->rbac_review->getGlobalRolesArray() as $role) {
117  $roleOptions[$role['obj_id']] = ilObject::_lookupTitle($role['obj_id']);
118  }
119 
120  $apache_default_role = $this->ui_field->select(
121  $this->lng->txt('apache_default_role'),
122  $roleOptions
123  );
124 
125  $apache_local_autocreate = $this->ui_field->optionalGroup([
126  'apache_default_role' => $apache_default_role
127  ], $this->lng->txt('apache_autocreate'));
128 
129  return $this->ui_field->optionalGroup([
130  'apache_local_autocreate' => $apache_local_autocreate
131  ], $this->lng->txt('apache_enable_auth'))
132  ->withValue(
133  $this->checkGroupEnabled('apache_enable_auth')
134  ? [
135  'apache_local_autocreate' => $this->checkGroupEnabled('apache_local_autocreate')
136  ? [
137  'apache_default_role' => $this->values['apache_default_role'] ?? 4
138  ]
139  : null
140  ]
141  : null
142  );
143  }
144 
145  private function buildLdapEnableInput(): Checkbox|OptionalGroup
146  {
147  $servers = ilLDAPServer::getServerIds();
148 
149  if ($servers !== []) {
150  $options[0] = $this->lng->txt('select_one');
151  foreach ($servers as $server_id) {
152  $ldap_server = new ilLDAPServer($server_id);
153  $options[$server_id] = $ldap_server->getName();
154  }
155 
156  $apache_enable_ldap = $this->ui_field->optionalGroup([
157  'apache_ldap_sid' => $this->ui_field->select($this->lng->txt('auth_ldap_server_ds'), $options)
158  ->withRequired(true)
159  ], $this->lng->txt('apache_enable_ldap'), $this->lng->txt('apache_ldap_hint_ldap_must_be_configured'))
160  ->withValue(
161  $this->checkGroupEnabled('apache_enable_ldap')
162  ? [
163  'apache_ldap_sid' => $this->values['apache_ldap_sid'] ?? ilLDAPServer::getDataSource(ilAuthUtils::AUTH_APACHE)
164  ]
165  : null
166  );
167  } else {
168  $apache_enable_ldap = $this->ui_field->checkbox(
169  $this->lng->txt('apache_enable_ldap'),
170  $this->lng->txt('apache_ldap_hint_ldap_must_be_configured')
171  )->withValue((bool) ($this->values['apache_enable_ldap'] ?? true));
172  }
173 
174  return $apache_enable_ldap;
175  }
176 
178  {
179  return $this->ui_field->optionalGroup([
180  'apache_auth_target_override_login_page' => $this->ui_field->text($this->lng->txt('apache_auth_target_override_login'))
181  ->withRequired(true)
182  ], $this->lng->txt('apache_auth_enable_override_login'))
183  ->withValue(
184  $this->checkGroupEnabled('apache_auth_enable_override_login_page')
185  ? [
186  'apache_auth_target_override_login_page' => $this->values['apache_auth_target_override_login_page'] ?? ''
187  ]
188  : null
189  );
190  }
191 
193  {
194  return $this->ui_field->switchableGroup([
195  '1' => $this->ui_field->group([
196  'apache_auth_username_direct_mapping_fieldname' => $this->ui_field->text(
197  $this->lng->txt('apache_auth_username_direct_mapping_fieldname')
198  )->withValue($this->values['apache_auth_username_direct_mapping_fieldname'] ?? '')
199  ], $this->lng->txt('apache_auth_username_direct_mapping')),
200  '2' => $this->ui_field->group([], $this->lng->txt('apache_auth_username_extended_mapping'))->withDisabled(true),
201  '3' => $this->ui_field->group([], $this->lng->txt('apache_auth_username_by_function')),
202  ], $this->lng->txt('apache_auth_username_config_type'))
203  ->withValue($this->values['apache_auth_username_config_type'] ?? '1');
204  }
205 
206  private function checkGroupEnabled(string $post_var): bool
207  {
208  return isset($this->values[$post_var]) && $this->values[$post_var];
209  }
210 }
__construct(private int $ref_id, private object $parentObject, private string $show_command, private string $save_command, private array $values=[], ?UIFactory $ui_factory=null, ?ilLanguage $lng=null, ?ilRbacSystem $rbac_system=null, ?ilRbacReview $rbac_review=null, ?ilCtrlInterface $ctrl=null)
static getDataSource(int $a_auth_mode)
const int AUTH_APACHE
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static getServerIds()
Get all server ids.
static _lookupTitle(int $obj_id)
global $DIC
Definition: shib_login.php:26
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61