ILIAS  release_8 Revision v8.24
class.ilAuthModeDetermination.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 public const TYPE_MANUAL = 0;
27 public const TYPE_AUTOMATIC = 1;
28
29 private static ?ilAuthModeDetermination $instance = null;
30
32
35
36 private int $kind = self::TYPE_MANUAL;
37 private array $position = [];
38
39
46 private function __construct()
47 {
48 global $DIC;
49
50 $this->logger = $DIC->logger()->auth();
51
52 $this->commonSettings = $DIC->settings();
53
54 $this->settings = new ilSetting("auth_mode_determination");
55 $this->read();
56 }
57
61 public static function _getInstance(): ilAuthModeDetermination
62 {
63 if (self::$instance) {
64 return self::$instance;
65 }
66 return self::$instance = new ilAuthModeDetermination();
67 }
68
72 public function isManualSelection(): bool
73 {
74 return $this->kind === self::TYPE_MANUAL;
75 }
76
80 public function getKind(): int
81 {
82 return $this->kind;
83 }
84
91 public function setKind(int $a_kind): void
92 {
93 // TODO check value range
94 $this->kind = $a_kind;
95 }
96
100 public function getAuthModeSequence(string $a_username = ''): array
101 {
102 if ($a_username === '') {
103 return $this->position ?: array();
104 }
105 $sorted = array();
106
107 foreach ($this->position as $auth_key) {
108 $sid = ilLDAPServer::getServerIdByAuthMode((string) $auth_key);
109 if ($sid) {
111 $this->logger->debug('Validating username filter for ' . $server->getName());
112 if ($server->getUsernameFilter() !== '') {
113 //#17731
114 $pattern = str_replace('*', '.*?', $server->getUsernameFilter());
115
116 if (preg_match('/^' . $pattern . '$/', $a_username)) {
117 $this->logger->debug('Filter matches for ' . $a_username);
118 array_unshift($sorted, $auth_key);
119 continue;
120 }
121 $this->logger->debug('Filter matches not for ' . $a_username . ' <-> ' . $server->getUsernameFilter());
122 }
123 }
124 $sorted[] = $auth_key;
125 }
126
127 return $sorted;
128 }
129
133 public function getCountActiveAuthModes(): int
134 {
135 return count($this->position);
136 }
137
144 public function setAuthModeSequence(array $a_pos): void
145 {
146 $this->position = $a_pos;
147 }
148
152 public function save(): void
153 {
154 $this->settings->deleteAll();
155
156 $this->settings->set('kind', (string) $this->getKind());
157
158 $counter = 0;
159 foreach ($this->position as $auth_mode) {
160 $this->settings->set((string) $counter++, (string) $auth_mode);
161 }
162 }
163
164
168 private function read(): void
169 {
170 $this->kind = (int) $this->settings->get('kind', (string) self::TYPE_MANUAL);
171
172 $soap_active = (bool) $this->commonSettings->get('soap_auth_active', "");
173
174 // apache settings
175 $apache_settings = new ilSetting('apache_auth');
176 $apache_active = $apache_settings->get('apache_enable_auth');
177
178 // Check if active
179 $i = 0;
180 while (true) {
181 $auth_mode = $this->settings->get((string) $i++, null);
182 if ($auth_mode === null) {
183 break;
184 }
185 if ($auth_mode) {
186 switch ((int) $auth_mode) {
188 $this->position[] = (int) $auth_mode;
189 break;
191 $auth_id = ilLDAPServer::getServerIdByAuthMode($auth_mode);
192 if ($auth_id === null) {
193 break;
194 }
196
197 if ($server->isActive()) {
198 $this->position[] = $auth_mode;
199 }
200 break;
201
203 if ($soap_active) {
204 $this->position[] = (int) $auth_mode;
205 }
206 break;
207
209 if ($apache_active) {
210 $this->position[] = (int) $auth_mode;
211 }
212 break;
213
214 default:
215 foreach (ilAuthUtils::getAuthPlugins() as $pl) {
216 if ($pl->isAuthActive((int) $auth_mode)) {
217 $this->position[] = $auth_mode;
218 }
219 }
220 break;
221 }
222 }
223 }
224
225 // Append missing active auth modes
226 if (!in_array(ilAuthUtils::AUTH_LOCAL, $this->position, true)) {
227 $this->position[] = ilAuthUtils::AUTH_LOCAL;
228 }
229 // begin-patch ldap_multiple
230 foreach (ilLDAPServer::_getActiveServerList() as $sid) {
232 if ($server->isActive() && !in_array(ilAuthUtils::AUTH_LDAP . '_' . $sid, $this->position, true)) {
233 $this->position[] = (ilAuthUtils::AUTH_LDAP . '_' . $sid);
234 }
235 }
236 // end-patch ldap_multiple
237 if ($soap_active && !in_array(ilAuthUtils::AUTH_SOAP, $this->position, true)) {
238 $this->position[] = ilAuthUtils::AUTH_SOAP;
239 }
240 if ($apache_active && !in_array(ilAuthUtils::AUTH_APACHE, $this->position, true)) {
241 $this->position[] = ilAuthUtils::AUTH_APACHE;
242 }
243 // begin-patch auth_plugin
244 foreach (ilAuthUtils::getAuthPlugins() as $pl) {
245 foreach ($pl->getAuthIds() as $auth_id) {
246 if ($pl->isAuthActive($auth_id) && !in_array($auth_id, $this->position, true)) {
247 $this->position[] = $auth_id;
248 }
249 }
250 }
251 // end-patch auth_plugin
252 }
253}
setAuthModeSequence(array $a_pos)
set auth mode sequence
getCountActiveAuthModes()
get number of auth modes
static ilAuthModeDetermination $instance
getAuthModeSequence(string $a_username='')
get auth mode sequence
__construct()
Constructor (Singleton)
setKind(int $a_kind)
set kind of determination
static getAuthPlugins()
Get active enabled auth plugins.
static _getActiveServerList()
Get active server list.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$server
global $DIC
Definition: feed.php:28
$i
Definition: metadata.php:41