19declare(strict_types=1);
84 $this->db =
$DIC->database();
85 $this->
lng = $DIC->language();
86 $this->ilErr =
$DIC[
'ilErr'];
87 $this->
logger = $DIC->logger()->auth();
89 $this->server_id = $a_server_id;
90 $this->url_list = new \ILIAS\LDAP\Server\ServerUrlList();
100 return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] =
new ilLDAPServer($a_server_id));
108 if ($this->url_list->count() < 2) {
112 $this->url_list = $this->url_list->rotate();
114 $query =
'UPDATE ldap_server_settings SET ' .
115 'url = ' . $this->db->quote($this->url_list->toString(),
'text') .
' ' .
116 'WHERE server_id = ' . $this->db->quote($this->
getServerId(),
'integer');
117 $this->db->manipulate($query);
128 return function_exists(
'ldap_bind');
142 $query =
"SELECT server_id FROM ldap_server_settings " .
143 "WHERE active = 1 AND authentication = 1 " .
149 while ($row =
$ilDB->fetchObject(
$res)) {
150 $server_ids[] = (
int) $row->server_id;
166 $query =
"SELECT server_id FROM ldap_server_settings " .
167 "WHERE active = 1 " .
168 "AND sync_per_cron = 1 " .
175 while ($row =
$ilDB->fetchObject(
$res)) {
176 $server_ids[] = (
int) $row->server_id;
191 $query =
"SELECT server_id FROM ldap_server_settings " .
192 "WHERE active = 1 " .
193 "AND role_sync_active = 1 ";
199 while ($row =
$ilDB->fetchObject(
$res)) {
200 $server_ids[] = (
int) $row->server_id;
213 if (count($servers)) {
230 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
235 while ($row =
$ilDB->fetchObject(
$res)) {
236 $server_ids[] = $row->server_id;
251 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
272 $query =
'SELECT * FROM ldap_server_settings ORDER BY name';
289 $query =
"SELECT server_id FROM ldap_server_settings " .
290 "WHERE active = " .
$ilDB->quote(1,
'integer') .
" " .
291 "AND authentication = " .
$ilDB->quote(0,
'integer') .
" " .
292 "AND ( authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
293 "OR authentication_type = " .
$ilDB->quote(0,
'integer') .
")";
296 $server_ids = array();
298 $server_ids[] = $row->server_id;
312 $query =
"SELECT server_id FROM ldap_server_settings " .
313 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
314 "AND authentication = " .
$ilDB->quote(0,
'integer');
328 $query =
"SELECT server_id FROM ldap_server_settings " .
329 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" ";
332 return $row->server_id;
346 $query =
'UPDATE ldap_server_settings ' .
347 'SET authentication_type = ' .
$ilDB->quote(0,
'integer') .
' ' .
348 'WHERE authentication_type = ' .
$ilDB->quote($a_authmode,
'integer');
349 $ilDB->manipulate($query);
359 public static function toggleDataSource(
int $a_ldap_server_id,
int $a_auth_mode,
int $a_status): bool
368 $query =
"UPDATE ldap_server_settings " .
369 'SET authentication_type = ' .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
370 'WHERE server_id = ' .
$ilDB->quote($a_ldap_server_id,
'integer');
371 $ilDB->manipulate($query);
385 if ($a_auth_mode ===
'') {
386 $logger->
error(__METHOD__ .
': No auth mode given..............');
390 $auth_arr = explode(
'_', $a_auth_mode);
400 if (self::isAuthModeLDAP($a_auth_mode)) {
401 $auth_arr = explode(
'_', $a_auth_mode);
402 return (
int) $auth_arr[1];
413 $auth_arr = explode(
'_', $a_auth_key);
414 if (count($auth_arr) > 1) {
415 return 'ldap_' . $auth_arr[1];
426 $auth_arr = explode(
'_', $a_auth_mode);
427 if (count($auth_arr) > 1) {
444 $this->enabled_authentication = $a_status;
460 $this->authentication_mapping = $a_map;
485 $this->active = $a_status;
493 return $this->url_list->getConnectionStringAtIndex(0);
499 public function setUrl(
string $a_url): void
506 return $this->url_list->toString();
515 $connection_failures = [];
518 $valid_urls = $this->url_list->validUrls();
519 foreach ($valid_urls as $index => $uri) {
520 $url_string = (string) $uri;
523 $this->
logger->debug(
'Attempting LDAP connection to: {url}', [
'url' => $url_string]);
528 $this->url_list = $this->url_list->withPrimaryAt($index);
530 if ($connection_failures !== []) {
532 'Successfully connected to LDAP server: {url} after {failures} failed attempts',
534 'url' => $url_string,
535 'failures' => count($connection_failures)
542 $connection_failures[] = $url_string;
544 $this->
logger->error(
'LDAP connection failed for server: {url} - {message}', [
545 'url' => $url_string,
546 'message' => $exc->getMessage(),
550 if (!$prevent_persisted_rotation) {
556 $this->
logger->warning(
'No valid LDAP server found. Tried {count} server(s)', [
557 'count' => count($connection_failures),
558 'urls' => implode(
', ', $connection_failures)
571 $this->name = $a_name;
579 $this->version = $a_version;
587 $this->base_dn = $a_base_dn;
595 $this->referrals = $a_status;
603 $this->tls = $a_status;
611 if ($a_type === self::LDAP_BIND_USER) {
623 $this->bind_user = $a_user;
631 $this->bind_password = $a_password;
639 $this->search_base = $a_search_base;
647 $this->user_attribute = $a_user_attr;
655 $this->
filter = $a_filter;
663 $this->group_dn = $a_value;
671 $this->group_filter = $a_value;
679 $this->group_member = $a_value;
687 $this->group_name = $a_value;
698 if (!is_array($names)) {
702 return array_filter(array_map(
'trim', $names));
712 $this->group_attribute = $a_value;
716 $this->group_optional = $a_status;
724 $this->group_user_filter = $a_filter;
737 $this->memberisdn = $a_value;
741 $this->group_scope = $a_value;
749 $this->user_scope = $a_value;
761 $this->sync_on_login = $a_value;
769 $this->sync_per_cron = $a_value;
773 $this->global_role = $a_role;
781 $this->role_bind_dn = $a_value;
789 $this->role_bind_pass = $a_value;
797 $this->role_sync_active = $a_value;
806 $this->username_filter = $a_value;
811 $this->escape_dn = $a_value;
824 $this->account_migration = $a_status;
841 $this->ilErr->setMessage(
'');
845 $this->url_list->count() === 0) {
846 $this->ilErr->setMessage($this->
lng->txt(
'fill_out_all_required_fields'));
850 if (!empty($this->url_list->getInvalidParts())) {
851 $this->ilErr->setMessage($this->
lng->txt(
'form_input_not_valid'));
857 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_bind_user'));
862 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_role_assignment'));
867 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_tls_conflict'));
880 if ($this->url_list->count() === 0) {
881 return $this->
lng->txt(
'ldap_server_url_required');
884 $invalid_parts = $this->url_list->getInvalidParts();
885 if (!empty($invalid_parts)) {
886 return $this->
lng->txt(
'ldap_server_url_invalid_uris') .
' ' . implode(
', ', $invalid_parts);
894 $next_id = $this->db->nextId(
'ldap_server_settings');
896 $query =
'INSERT INTO ldap_server_settings (server_id,active,name,url,version,base_dn,referrals,tls,bind_type,bind_user,bind_pass,' .
897 'search_base,user_scope,user_attribute,filter,group_dn,group_scope,group_filter,group_member,group_memberisdn,group_name,' .
898 'group_attribute,group_optional,group_user_filter,sync_on_login,sync_per_cron,role_sync_active,role_bind_dn,role_bind_pass,migration, ' .
899 'authentication,authentication_type,username_filter, escape_dn) ' .
900 'VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)';
904 'integer',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
'text',
'text',
'text',
'integer',
905 'text',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
906 'text',
'text',
'integer',
'integer',
'integer',
"text",
'integer'),
945 $this->server_id = $next_id;
951 $query =
"UPDATE ldap_server_settings SET " .
952 "active = " . $this->db->quote($this->
isActive(),
'integer') .
", " .
953 "name = " . $this->db->quote($this->
getName(),
'text') .
", " .
954 "url = " . $this->db->quote($this->
getUrlString(),
'text') .
", " .
955 "version = " . $this->db->quote($this->
getVersion(),
'integer') .
", " .
956 "base_dn = " . $this->db->quote($this->
getBaseDN(),
'text') .
", " .
957 "referrals = " . $this->db->quote($this->
isActiveReferrer(),
'integer') .
", " .
958 "tls = " . $this->db->quote($this->
isActiveTLS(),
'integer') .
", " .
959 "bind_type = " . $this->db->quote($this->
getBindingType(),
'integer') .
", " .
960 "bind_user = " . $this->db->quote($this->
getBindUser(),
'text') .
", " .
961 "bind_pass = " . $this->db->quote($this->
getBindPassword(),
'text') .
", " .
962 "search_base = " . $this->db->quote($this->
getSearchBase(),
'text') .
", " .
963 "user_scope = " . $this->db->quote($this->
getUserScope(),
'integer') .
", " .
964 "user_attribute = " . $this->db->quote($this->
getUserAttribute(),
'text') .
", " .
965 "filter = " . $this->db->quote($this->
getFilter(),
'text') .
", " .
966 "group_dn = " . $this->db->quote($this->
getGroupDN(),
'text') .
", " .
967 "group_scope = " . $this->db->quote($this->
getGroupScope(),
'integer') .
", " .
968 "group_filter = " . $this->db->quote($this->
getGroupFilter(),
'text') .
", " .
969 "group_member = " . $this->db->quote($this->
getGroupMember(),
'text') .
", " .
971 "group_name = " . $this->db->quote($this->
getGroupName(),
'text') .
", " .
972 "group_attribute = " . $this->db->quote($this->
getGroupAttribute(),
'text') .
", " .
974 "group_user_filter = " . $this->db->quote($this->
getGroupUserFilter(),
'text') .
", " .
975 "sync_on_login = " . $this->db->quote(($this->
enabledSyncOnLogin() ? 1 : 0),
'integer') .
", " .
976 "sync_per_cron = " . $this->db->quote(($this->
enabledSyncPerCron() ? 1 : 0),
'integer') .
", " .
978 "role_bind_dn = " . $this->db->quote($this->
getRoleBindDN(),
'text') .
", " .
983 ", username_filter = " . $this->db->quote($this->
getUsernameFilter(),
"text") .
" " .
984 ", escape_dn = " . $this->db->quote($this->
enabledEscapeDN() ? 1 : 0,
'integer') .
" " .
985 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
987 $this->db->manipulate($query);
994 public function delete():
void
1005 foreach ($rules as $ruleAssigment) {
1006 $ruleAssigment->delete();
1011 $query =
"DELETE FROM ldap_server_settings " .
1012 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
1013 $this->db->manipulate($query);
1025 'url' => $this->
getUrl(),
1084 if (strpos(
$filter,
'(') !== 0) {
1087 if (substr(
$filter, -1) !==
')') {
1102 $mapping->getFields(),
1117 if (!$this->server_id) {
1120 $query =
"SELECT * FROM ldap_server_settings WHERE server_id = " . $this->db->quote($this->server_id,
ilDBConstants::T_INTEGER);
1122 $res = $this->db->query($query);
1125 $this->
setName($row->name ??
'');
1126 $this->
setUrl($row->url ??
'');
static fromString(string $stored)
Create from string representation (comma-separated, as stored in DB or form).
static _getAuthModeName($a_auth_key)
Error Handling & global info handling.
static _delete(int $a_server_id)
static _getInstanceByServerId(int $a_server_id)
static _getRules($a_server_id)
Get all rules.
static getAttributeNames($a_server_id)
get all possible attribute names
static _deleteByServerId(int $a_server_id)
setGlobalRole(int $a_role)
int $authentication_mapping
enableRoleSynchronization(bool $a_value)
setGroupFilter(string $a_value)
static _getServerList()
Get list of all configured servers.
setFilter(string $a_filter)
isAuthenticationEnabled()
Check if authentication is enabled.
enableEscapeDN(bool $a_value)
toggleTLS(bool $a_status)
enableGroupMemberIsDN(bool $a_value)
read()
Read server settings.
enableSyncOnLogin(bool $a_value)
const LDAP_BIND_ANONYMOUS
static getAuthModeByKey(string $a_auth_key)
get auth mode by key
__construct(int $a_server_id=0)
static _getCronServerIds()
Get list of acticve servers with option 'SyncCron'.
static checkLDAPLib()
Check if ldap module is installed.
static disableDataSourceForAuthMode(int $a_authmode)
Disable data source.
getAuthenticationMapping()
Get authentication mode that is mapped.
prepareFilter(string $a_filter)
Create brackets for filters if they do not exist.
setGroupDN(string $a_value)
setGroupScope(int $a_value)
static _getActiveServerList()
Get active server list.
setRoleBindDN(string $a_value)
string $group_user_filter
setGroupUserFilter(string $a_filter)
static isDataSourceActive(int $a_auth_mode)
Check if a data source is active for a specific auth mode.
enableAuthentication(bool $a_status)
Enable authentication for this ldap server.
setUrl(string $a_url)
Set server URL(s).
setGroupAttribute(string $a_value)
toPearAuthArray()
Creates an array of options compatible to PEAR Auth.
validate()
Validate user input.
static _getAllServer()
Get list of all configured servers.
setUsernameFilter(string $a_value)
setBindPassword(string $a_password)
const DEFAULT_NETWORK_TIMEOUT
setUserAttribute(string $a_user_attr)
setGroupName(string $a_value)
getAuthenticationMappingKey()
Get authentication mapping key Default is ldap.
getGroupNames()
Get group names as array.
setBaseDN(string $a_base_dn)
enableSyncPerCron(bool $a_value)
getPearAtributeArray()
Get attribute array for pear auth data.
isAccountMigrationEnabled()
enabled account migration
rotateFallbacks()
Rotate fallback urls in case of connect timeouts (move first to end and persist).
static toggleDataSource(int $a_ldap_server_id, int $a_auth_mode, int $a_status)
Toggle Data Source.
enableAccountMigration(bool $a_status)
Enable account migration.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getDataSource(int $a_auth_mode)
setGroupMember(string $a_value)
setBindUser(string $a_user)
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static isAuthModeLDAP(string $a_auth_mode)
Check if user auth mode is LDAP.
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
setUserScope(int $a_value)
setBindingType(int $a_type)
static _getRoleSyncServerIds()
Check whether there if there is an active server with option role_sync_active.
bool $enabled_authentication
toggleMembershipOptional(bool $a_status)
toggleReferrer(bool $a_status)
setAuthenticationMapping(int $a_map)
Set mapped authentication mapping.
toggleActive(bool $a_status)
static _getFirstActiveServer()
Get first active server.
setSearchBase(string $a_search_base)
static getAvailableDataSources(int $a_auth_mode)
doConnectionCheck(bool $prevent_persisted_rotation=false)
Check ldap connection and do a fallback to the next server if no connection is possible.
getUrlValidationError()
Return URL validation error message if URL list is invalid, null otherwise.
static getServerIds()
Get all server ids.
setVersion(int $a_version)
enabledRoleSynchronization()
setRoleBindPassword(string $a_value)
ILIAS LDAP Server ServerUrlList $url_list
Component logger with individual log levels by component id.
error(string $message, array $context=[])
filter(string $filter_id, array $class_path, string $cmd, bool $activated=true, bool $expanded=true)