ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSamlIdp.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 final class ilSamlIdp
26 {
27  private const PROP_IDP_ID = 'idp_id';
28  private const PROP_IS_ACTIVE = 'is_active';
29  private const PROP_DEFAULT_ROLE_ID = 'default_role_id';
30  private const PROP_UID_CLAIM = 'uid_claim';
31  private const PROP_LOGIN_CLAIM = 'login_claim';
32  private const PROP_ENTITY_ID = 'entity_id';
33  private const PROP_SYNC_STATUS = 'sync_status';
34  private const PROP_ALLOW_LOCAL_AUTH = 'allow_local_auth';
35  private const PROP_ACCOUNT_MIGR_STATUS = 'account_migr_status';
36 
37  private readonly ilDBInterface $db;
39  private static array $instances = [];
40  private bool $is_active = false;
41  private bool $allow_local_auth = false;
42  private int $default_role_id = 0;
43  private string $uid_claim = '';
44  private string $login_claim = '';
45  private bool $sync_status = false;
46  private string $entity_id = '';
47  private bool $account_migration_status = false;
48 
49  public function __construct(protected int $idp_id = 0)
50  {
51  $this->db = $GLOBALS['DIC']->database();
52 
53  if ($this->idp_id > 0) {
54  $this->read();
55  }
56  }
57 
58  public static function getFirstActiveIdp(): self
59  {
60  $idps = self::getActiveIdpList();
61  if ($idps !== []) {
62  return current($idps);
63  }
64 
65  throw new ilSamlException('No active SAML IDP found');
66  }
67 
68  public static function getInstanceByIdpId(int $a_idp_id): self
69  {
70  if (!isset(self::$instances[$a_idp_id]) || !(self::$instances[$a_idp_id] instanceof self)) {
71  self::$instances[$a_idp_id] = new self($a_idp_id);
72  }
73 
74  return self::$instances[$a_idp_id];
75  }
76 
77  private function read(): void
78  {
79  $query = 'SELECT * FROM saml_idp_settings WHERE idp_id = ' . $this->db->quote($this->idp_id, ilDBConstants::T_INTEGER);
80  $res = $this->db->query($query);
81  while ($record = $this->db->fetchAssoc($res)) {
82  $this->bindDbRecord($record);
83  return;
84  }
85 
86  throw new ilException('Could not find idp');
87  }
88 
89  public function persist(): void
90  {
91  if ($this->idp_id === 0) {
92  $this->setIdpId($this->db->nextId('saml_idp_settings'));
93  }
94 
95  $this->db->replace(
96  'saml_idp_settings',
97  [
98  self::PROP_IDP_ID => [ilDBConstants::T_INTEGER, $this->idp_id]
99  ],
100  [
101  self::PROP_IS_ACTIVE => [ilDBConstants::T_INTEGER, (int) $this->is_active],
102  self::PROP_DEFAULT_ROLE_ID => [ilDBConstants::T_INTEGER, $this->default_role_id],
103  self::PROP_UID_CLAIM => [ilDBConstants::T_TEXT, $this->uid_claim],
104  self::PROP_LOGIN_CLAIM => [ilDBConstants::T_TEXT, $this->login_claim],
105  self::PROP_ENTITY_ID => [ilDBConstants::T_TEXT, $this->entity_id],
106  self::PROP_SYNC_STATUS => [ilDBConstants::T_INTEGER, (int) $this->sync_status],
107  self::PROP_ALLOW_LOCAL_AUTH => [ilDBConstants::T_INTEGER, (int) $this->allow_local_auth],
108  self::PROP_ACCOUNT_MIGR_STATUS => [ilDBConstants::T_INTEGER, (int) $this->account_migration_status]
109  ]
110  );
111  }
112 
117  public function delete(): void
118  {
119  $mapping = new ilExternalAuthUserAttributeMapping('saml', $this->idp_id);
120  $mapping->delete();
121 
122  $this->db->manipulateF(
123  'UPDATE usr_data SET auth_mode = %s WHERE auth_mode = %s',
125  ['default', ilAuthUtils::AUTH_SAML . '_' . $this->idp_id]
126  );
127 
128  $this->db->manipulate('DELETE FROM saml_idp_settings WHERE idp_id = ' . $this->db->quote(
129  $this->idp_id,
131  ));
132  }
133 
137  public function toArray(): array
138  {
139  return [
140  self::PROP_IDP_ID => $this->idp_id,
141  self::PROP_IS_ACTIVE => $this->is_active,
142  self::PROP_DEFAULT_ROLE_ID => $this->default_role_id,
143  self::PROP_UID_CLAIM => $this->uid_claim,
144  self::PROP_LOGIN_CLAIM => $this->login_claim,
145  self::PROP_SYNC_STATUS => $this->sync_status,
146  self::PROP_ACCOUNT_MIGR_STATUS => $this->account_migration_status,
147  self::PROP_ALLOW_LOCAL_AUTH => $this->allow_local_auth,
148  self::PROP_ENTITY_ID => $this->entity_id
149  ];
150  }
151 
155  public function bindDbRecord(array $record): void
156  {
157  $this->setIdpId((int) $record[self::PROP_IDP_ID]);
158  $this->setActive((bool) $record[self::PROP_IS_ACTIVE]);
159  $this->setDefaultRoleId((int) $record[self::PROP_DEFAULT_ROLE_ID]);
160  $this->setUidClaim((string) $record[self::PROP_UID_CLAIM]);
161  $this->setLoginClaim((string) $record[self::PROP_LOGIN_CLAIM]);
162  $this->setSynchronizationStatus((bool) $record[self::PROP_SYNC_STATUS]);
163  $this->setAccountMigrationStatus((bool) $record[self::PROP_ACCOUNT_MIGR_STATUS]);
164  $this->setLocalLocalAuthenticationStatus((bool) $record[self::PROP_ALLOW_LOCAL_AUTH]);
165  $this->setEntityId((string) $record[self::PROP_ENTITY_ID]);
166  }
167 
168  public function bindForm(ilPropertyFormGUI $form): void
169  {
170  $this->setDefaultRoleId((int) $form->getInput(self::PROP_DEFAULT_ROLE_ID));
171  $this->setUidClaim((string) $form->getInput(self::PROP_UID_CLAIM));
172  $this->setLoginClaim((string) $form->getInput(self::PROP_LOGIN_CLAIM));
173  $this->setSynchronizationStatus((bool) $form->getInput(self::PROP_SYNC_STATUS));
174  $this->setLocalLocalAuthenticationStatus((bool) $form->getInput(self::PROP_ALLOW_LOCAL_AUTH));
175  $this->setAccountMigrationStatus((bool) $form->getInput(self::PROP_ACCOUNT_MIGR_STATUS));
176 
178  $metadata = $form->getItemByPostVar('metadata');
179  $this->setEntityId($metadata->getValue());
180  }
181 
182  public static function isAuthModeSaml(string $a_auth_mode): bool
183  {
184  if ('' === $a_auth_mode) {
185  return false;
186  }
187 
188  $auth_arr = explode('_', $a_auth_mode);
189  return (
190  count($auth_arr) === 2 &&
191  (int) $auth_arr[0] === ilAuthUtils::AUTH_SAML &&
192  is_string($auth_arr[1]) && $auth_arr[1] !== ''
193  );
194  }
195 
196  public static function getIdpIdByAuthMode(string $a_auth_mode): ?int
197  {
198  if (self::isAuthModeSaml($a_auth_mode)) {
199  $auth_arr = explode('_', $a_auth_mode);
200  return (int) $auth_arr[1];
201  }
202 
203  return null;
204  }
205 
206  public static function geIdpIdByEntityId(string $entityId): int
207  {
208  foreach (self::getAllIdps() as $idp) {
209  if ($idp->isActive() && $idp->getEntityId() === $entityId) {
210  return $idp->getIdpId();
211  }
212  }
213 
214  return 0;
215  }
216 
220  public static function getActiveIdpList(): array
221  {
222  $idps = [];
223  foreach (self::getAllIdps() as $idp) {
224  if ($idp->isActive()) {
225  $idps[] = $idp;
226  }
227  }
228 
229  return $idps;
230  }
231 
235  public static function getAllIdps(): array
236  {
237  global $DIC;
238 
239  $res = $DIC->database()->query('SELECT * FROM saml_idp_settings');
240 
241  $idps = [];
242  while ($row = $DIC->database()->fetchAssoc($res)) {
243  $idp = new self();
244  $idp->bindDbRecord($row);
245 
246  $idps[] = $idp;
247  }
248 
249  return $idps;
250  }
251 
252  public static function getAuthModeByKey(string $a_auth_key): string
253  {
254  $auth_arr = explode('_', $a_auth_key);
255  if (count($auth_arr) > 1) {
256  return 'saml_' . $auth_arr[1];
257  }
258 
259  return 'saml';
260  }
261 
262  public static function getKeyByAuthMode(string $a_auth_mode): string
263  {
264  $auth_arr = explode('_', $a_auth_mode);
265  if (count($auth_arr) > 1) {
266  return ilAuthUtils::AUTH_SAML . '_' . $auth_arr[1];
267  }
268 
269  return (string) ilAuthUtils::AUTH_SAML;
270  }
271 
272  public function getEntityId(): string
273  {
274  return $this->entity_id;
275  }
276 
277  public function setEntityId(string $entity_id): void
278  {
279  $this->entity_id = $entity_id;
280  }
281 
282  public function isActive(): bool
283  {
284  return $this->is_active;
285  }
286 
287  public function setActive(bool $is_active): void
288  {
289  $this->is_active = $is_active;
290  }
291 
292  public function getIdpId(): int
293  {
294  return $this->idp_id;
295  }
296 
297  public function setIdpId(int $idp_id): void
298  {
299  $this->idp_id = $idp_id;
300  }
301 
302  public function allowLocalAuthentication(): bool
303  {
305  }
306 
307  public function setLocalLocalAuthenticationStatus(bool $status): void
308  {
309  $this->allow_local_auth = $status;
310  }
311 
312  public function getDefaultRoleId(): int
313  {
314  return $this->default_role_id;
315  }
316 
317  public function setDefaultRoleId(int $role_id): void
318  {
319  $this->default_role_id = $role_id;
320  }
321 
322  public function setUidClaim(string $claim): void
323  {
324  $this->uid_claim = $claim;
325  }
326 
327  public function getUidClaim(): string
328  {
329  return $this->uid_claim;
330  }
331 
332  public function setLoginClaim(string $claim): void
333  {
334  $this->login_claim = $claim;
335  }
336 
337  public function getLoginClaim(): string
338  {
339  return $this->login_claim;
340  }
341 
342  public function isSynchronizationEnabled(): bool
343  {
344  return $this->sync_status;
345  }
346 
347  public function setSynchronizationStatus(bool $sync): void
348  {
349  $this->sync_status = $sync;
350  }
351 
352  public function isAccountMigrationEnabled(): bool
353  {
355  }
356 
357  public function setAccountMigrationStatus(bool $status): void
358  {
359  $this->account_migration_status = $status;
360  }
361 }
bindDbRecord(array $record)
$res
Definition: ltiservices.php:69
static isAuthModeSaml(string $a_auth_mode)
Class ilSamlException.
getItemByPostVar(string $a_post_var)
setActive(bool $is_active)
const PROP_ACCOUNT_MIGR_STATUS
const PROP_SYNC_STATUS
setDefaultRoleId(int $role_id)
readonly ilDBInterface $db
static getInstanceByIdpId(int $a_idp_id)
static getIdpIdByAuthMode(string $a_auth_mode)
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
setEntityId(string $entity_id)
setLocalLocalAuthenticationStatus(bool $status)
setUidClaim(string $claim)
Class ilExternalAuthUserAttributeMapping.
setLoginClaim(string $claim)
const PROP_LOGIN_CLAIM
const PROP_IS_ACTIVE
$GLOBALS["DIC"]
Definition: wac.php:30
const PROP_ALLOW_LOCAL_AUTH
static array $instances
const PROP_UID_CLAIM
global $DIC
Definition: shib_login.php:25
__construct(protected int $idp_id=0)
int $default_role_id
static getAllIdps()
string $login_claim
static getKeyByAuthMode(string $a_auth_mode)
string $uid_claim
Class ilSamlIdp.
setAccountMigrationStatus(bool $status)
const PROP_DEFAULT_ROLE_ID
static getAuthModeByKey(string $a_auth_key)
string $entity_id
isAccountMigrationEnabled()
setIdpId(int $idp_id)
static geIdpIdByEntityId(string $entityId)
bool $allow_local_auth
const PROP_IDP_ID
allowLocalAuthentication()
static getActiveIdpList()
isSynchronizationEnabled()
const PROP_ENTITY_ID
setSynchronizationStatus(bool $sync)
bool $account_migration_status
static getFirstActiveIdp()