19declare(strict_types=1);
52 $this->
lng = $DIC->language();
53 $this->
profile = $DIC[
'user']->getProfile();
55 if (
null === $a_idp_id || 0 === $a_idp_id) {
70 !array_key_exists($this->idp->getUidClaim(), $this->attributes) ||
71 !is_array($this->attributes[$this->idp->getUidClaim()]) ||
72 !array_key_exists(0, $this->attributes[$this->idp->getUidClaim()]) ||
73 $this->attributes[$this->idp->getUidClaim()][0] ===
''
76 'Could not find unique SAML attribute for the configured identifier: %s',
77 print_r($this->idp->getUidClaim(),
true)
81 $this->uid = $this->attributes[$this->idp->getUidClaim()][0];
86 if ([] === $this->attributes) {
87 $this->
getLogger()->warning(
'Could not parse any attributes from SAML response.');
97 }
catch (Exception
$e) {
98 $this->
getLogger()->warning($e->getMessage());
107 $update_auth_mode =
false;
110 'Login observer called for SAML authentication request of ext_account "%s" and auth_mode "%s".',
116 'Trying to find ext_account "%s" for auth_mode "%s".',
127 if (!is_string($internal_account) || $internal_account ===
'') {
128 $update_auth_mode =
true;
131 'Could not find ext_account "%s" for auth_mode "%s".',
136 $fallback_auth_mode =
'local';
138 'Trying to find ext_account "%s" for auth_mode "%s".',
145 if (
$GLOBALS[
'DIC'][
'ilSetting']->
get(
'auth_mode')) {
146 $defaultAuth =
$GLOBALS[
'DIC'][
'ilSetting']->get(
'auth_mode');
150 (!is_string($internal_account) || $internal_account ===
'') &&
154 'Could not find ext_account "%s" for auth_mode "%s".',
159 $fallback_auth_mode =
'default';
161 'Trying to find ext_account "%s" for auth_mode "%s".',
169 if (is_string($internal_account) && $internal_account !==
'') {
171 'Found user "%s" for ext_account "%s" in ILIAS database.',
176 if ($this->idp->isSynchronizationEnabled()) {
178 'SAML user synchronisation is enabled, so update existing user "%s" with ext_account "%s".',
182 $internal_account = $this->
importUser($internal_account, $this->uid, $this->attributes);
185 if ($update_auth_mode) {
190 'SAML Switched auth_mode of user with login "%s" and ext_account "%s" to "%s".',
197 'SAML Could not switch auth_mode of user with login "%s" and ext_account "%s" to "%s".',
206 'Authentication succeeded: Found internal login "%s for ext_account "%s" and auth_mode "%s".',
220 'Could not find an existing user for ext_account "%s" for any relevant auth_mode.',
223 if ($this->idp->isSynchronizationEnabled()) {
225 'SAML user synchronisation is enabled, so determine action for ext_account "%s" and auth_mode "%s".',
229 if (!$this->force_new_account && $this->idp->isAccountMigrationEnabled()) {
234 'Account migration is enabled, so redirecting ext_account "%s" to account migration screen.',
244 $new_name = $this->
importUser(
null, $this->uid, $this->attributes);
246 'Created new user account with login "%s" and ext_account "%s".',
278 $this->
getLogger()->warning(
'Cannot find user id for external account: ' . $this->
getCredentials()->getUsername());
285 $this->return_to = (string)
ilSession::get(self::SESSION_TMP_RETURN_TO);
287 $this->force_new_account =
true;
294 $this->migration_account = $a_name;
309 return 'saml_' . $this->idp->getIdpId();
312 private function importUser(?
string $a_internal_login,
string $a_external_account, array $a_user_data = []): string
317 $xml_writer->xmlStartTag(
'Users');
318 if (
null === $a_internal_login) {
319 $login = $a_user_data[$this->idp->getLoginClaim()][0];
322 $xml_writer->xmlStartTag(
325 'Action' =>
'Insert',
326 'Language' => $this->
lng->getDefaultLanguage()
329 $xml_writer->xmlElement(
'Login', [], $login);
331 $xml_writer->xmlElement(
'Role', [
332 'Id' => $this->idp->getDefaultRoleId(),
337 $xml_writer->xmlElement(
'Active', [],
"true");
339 $xml_writer->xmlElement(
'TimeLimitUnlimited', [], 1);
340 $xml_writer->xmlElement(
'TimeLimitFrom', [], time());
341 $xml_writer->xmlElement(
'TimeLimitUntil', [], time());
342 $xml_writer->xmlElement(
347 $xml_writer->xmlElement(
'ExternalAccount', [], $a_external_account);
351 $login = $a_internal_login;
354 $xml_writer->xmlStartTag(
'User', [
'Action' =>
'Update',
'Id' => $usr_id]);
356 $loginClaim = $a_user_data[$this->idp->getLoginClaim()][0];
359 $xml_writer->xmlElement(
'Login', [], $login);
365 foreach ($mapping as $rule) {
368 $value = $attributeValueParser->parse();
371 $this->
getLogger()->warning($e->getMessage());
376 $xml_writer->xmlEndTag(
'User');
377 $xml_writer->xmlEndTag(
'Users');
380 'Started import of user "%s" with ext_account "%s" and auth_mode "%s".',
386 $importParser->setXMLContent($xml_writer->xmlDumpMem(
false));
387 $importParser->setRoleAssignment([
388 $this->idp->getDefaultRoleId() => $this->idp->getDefaultRoleId(),
392 $importParser->startParsing();
402 switch (strtolower($rule->getAttribute())) {
404 $gender_attr =
'Gender';
405 match (strtolower($value)) {
406 'n',
'neutral' => $xml_writer->
xmlElement($gender_attr, [],
'n'),
407 'm',
'male' => $xml_writer->
xmlElement($gender_attr, [],
'm'),
409 default => $xml_writer->
xmlElement($gender_attr, [],
'f'),
414 $xml_writer->
xmlElement(
'Firstname', [], $value);
418 $xml_writer->
xmlElement(
'Lastname', [], $value);
426 $xml_writer->
xmlElement(
'SecondEmail', [], $value);
430 $xml_writer->
xmlElement(
'Institution', [], $value);
434 $xml_writer->
xmlElement(
'Department', [], $value);
446 $xml_writer->
xmlElement(
'Street', [], $value);
454 $xml_writer->
xmlElement(
'PostalCode', [], $value);
458 $xml_writer->
xmlElement(
'Country', [], $value);
462 $xml_writer->
xmlElement(
'PhoneOffice', [], $value);
466 $xml_writer->
xmlElement(
'PhoneHome', [], $value);
470 $xml_writer->
xmlElement(
'PhoneMobile', [], $value);
477 case 'referral_comment':
478 $xml_writer->
xmlElement(
'Comment', [], $value);
481 case 'matriculation':
482 $xml_writer->
xmlElement(
'Matriculation', [], $value);
486 $xml_writer->
xmlElement(
'Birthday', [], $value);
495 if (!isset($udf_data[1])) {
500 $field = $this->user_defined_fields[$udf_data[1]] ??
null;
501 if ($field ===
null) {
503 "Invalid/Orphaned UD field mapping detected: %s",
511 [
'Id' => $field->getIdentifier(),
'Name' => $field->getLabel($this->lng)],
520 if ($this->user_defined_fields ===
null) {
521 $this->user_defined_fields = $this->
profile->getAllUserDefinedFields();
createNewAccount(ilAuthStatus $status)
Create new ILIAS account for external_account.
importUser(?string $a_internal_login, string $a_external_account, array $a_user_data=[])
setExternalAccountName(string $a_name)
const string ERR_WRONG_LOGIN
buildUserAttributeXml(ilXmlWriter $xml_writer, ilExternalAuthUserAttributeMappingRule $rule, string $value)
handleSamlAuth(ilAuthStatus $status)
const string LOG_COMPONENT
determineUidFromAttributes()
string $migration_account
__construct(ilAuthCredentials $credentials, ?int $a_idp_id=null)
getExternalAccountName()
Get external account name.
getUserAuthModeName()
Get user auth mode name ldap_1 for ldap account migration with server id 1 apache for apache auth.
migrateAccount(ilAuthStatus $status)
Create new account.
const string SESSION_TMP_RETURN_TO
const string SESSION_TMP_ATTRIBUTES
doAuthentication(ilAuthStatus $status)
readonly Profile $profile
getTriggerAuthMode()
Get auth mode which triggered the account migration 2_1 for ldap account migration with server id 1 1...
array $user_defined_fields
handleAuthenticationFail(ilAuthStatus $status, string $a_reason)
ilAuthCredentials $credentials
const int STATUS_ACCOUNT_MIGRATION_REQUIRED
setAuthenticatedUserId(int $a_id)
setStatus(int $a_status)
Set auth status.
const int STATUS_AUTHENTICATED
static _generateLogin(string $a_login)
generate free login by starting with a default string and adding postfix numbers
Base class for ILIAS Exception handling.
static getLogger(string $a_component_id)
Get component logger.
static _lookupId(string|array $a_user_str)
static _writeAuthMode(int $a_usr_id, string $a_auth_mode)
static _checkExternalAuthAccount(string $a_auth, string $a_account, bool $tryFallback=true)
check whether external account and authentication method matches with a user
static _loginExists(string $a_login, int $a_user_id=0)
static getInstanceByIdpId(int $a_idp_id)
static getFirstActiveIdp()
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
static strToLower(string $a_string)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc