19declare(strict_types=1);
89 $this->db =
$DIC->database();
90 $this->
lng = $DIC->language();
91 $this->ilErr =
$DIC[
'ilErr'];
93 $this->server_id = $a_server_id;
103 return self::$instances[$a_server_id] ?? (self::$instances[$a_server_id] =
new ilLDAPServer($a_server_id));
111 if (!$this->fallback_urls) {
115 $all_urls = array_merge($this->fallback_urls);
116 $all_urls[] = $this->
getUrl();
118 $query =
'UPDATE ldap_server_settings SET ' .
119 'url = ' . $this->db->quote(implode(
',', $all_urls),
'text') .
' ' .
120 'WHERE server_id = ' . $this->db->quote($this->
getServerId(),
'integer');
121 $this->db->manipulate($query);
131 return function_exists(
'ldap_bind');
145 $query =
"SELECT server_id FROM ldap_server_settings " .
146 "WHERE active = 1 AND authentication = 1 " .
152 while ($row =
$ilDB->fetchObject(
$res)) {
153 $server_ids[] = (
int) $row->server_id;
169 $query =
"SELECT server_id FROM ldap_server_settings " .
170 "WHERE active = 1 " .
171 "AND sync_per_cron = 1 " .
178 while ($row =
$ilDB->fetchObject(
$res)) {
179 $server_ids[] = (
int) $row->server_id;
194 $query =
"SELECT server_id FROM ldap_server_settings " .
195 "WHERE active = 1 " .
196 "AND role_sync_active = 1 ";
202 while ($row =
$ilDB->fetchObject(
$res)) {
203 $server_ids[] = (
int) $row->server_id;
216 if (count($servers)) {
233 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
238 while ($row =
$ilDB->fetchObject(
$res)) {
239 $server_ids[] = $row->server_id;
254 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
275 $query =
'SELECT * FROM ldap_server_settings ORDER BY name';
292 $query =
"SELECT server_id FROM ldap_server_settings " .
293 "WHERE active = " .
$ilDB->quote(1,
'integer') .
" " .
294 "AND authentication = " .
$ilDB->quote(0,
'integer') .
" " .
295 "AND ( authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
296 "OR authentication_type = " .
$ilDB->quote(0,
'integer') .
")";
299 $server_ids = array();
301 $server_ids[] = $row->server_id;
315 $query =
"SELECT server_id FROM ldap_server_settings " .
316 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
317 "AND authentication = " .
$ilDB->quote(0,
'integer');
331 $query =
"SELECT server_id FROM ldap_server_settings " .
332 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" ";
335 return $row->server_id;
349 $query =
'UPDATE ldap_server_settings ' .
350 'SET authentication_type = ' .
$ilDB->quote(0,
'integer') .
' ' .
351 'WHERE authentication_type = ' .
$ilDB->quote($a_authmode,
'integer');
352 $ilDB->manipulate($query);
362 public static function toggleDataSource(
int $a_ldap_server_id,
int $a_auth_mode,
int $a_status): bool
371 $query =
"UPDATE ldap_server_settings " .
372 'SET authentication_type = ' .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
373 'WHERE server_id = ' .
$ilDB->quote($a_ldap_server_id,
'integer');
374 $ilDB->manipulate($query);
386 $logger =
$DIC->logger()->auth();
388 if ($a_auth_mode ===
'') {
389 $logger->error(__METHOD__ .
': No auth mode given..............');
393 $auth_arr = explode(
'_', $a_auth_mode);
403 if (self::isAuthModeLDAP($a_auth_mode)) {
404 $auth_arr = explode(
'_', $a_auth_mode);
405 return (
int) $auth_arr[1];
416 $auth_arr = explode(
'_', $a_auth_key);
417 if (count($auth_arr) > 1) {
418 return 'ldap_' . $auth_arr[1];
429 $auth_arr = explode(
'_', $a_auth_mode);
430 if (count($auth_arr) > 1) {
447 $this->enabled_authentication = $a_status;
463 $this->authentication_mapping = $a_map;
488 $this->active = $a_status;
498 public function setUrl(
string $a_url): void
500 $this->url_string = $a_url;
503 $urls = explode(
',', $a_url);
506 foreach ($urls as
$url) {
511 $this->fallback_urls[] =
$url;
529 foreach (array_merge(array(0 => $this->url), $this->fallback_urls) as
$url) {
539 ilLoggerFactory::getLogger(
'auth')->error(
'Cannot connect to LDAP server: ' . $url .
' ' . $exc->getCode() .
' ' . $exc->getMessage());
553 $this->name = $a_name;
561 $this->version = $a_version;
569 $this->base_dn = $a_base_dn;
577 $this->referrals = $a_status;
585 $this->tls = $a_status;
593 if ($a_type === self::LDAP_BIND_USER) {
605 $this->bind_user = $a_user;
613 $this->bind_password = $a_password;
621 $this->search_base = $a_search_base;
629 $this->user_attribute = $a_user_attr;
637 $this->
filter = $a_filter;
645 $this->group_dn = $a_value;
653 $this->group_filter = $a_value;
661 $this->group_member = $a_value;
669 $this->group_name = $a_value;
680 if (!is_array($names)) {
684 return array_filter(array_map(
'trim', $names));
694 $this->group_attribute = $a_value;
698 $this->group_optional = $a_status;
706 $this->group_user_filter = $a_filter;
719 $this->memberisdn = $a_value;
723 $this->group_scope = $a_value;
731 $this->user_scope = $a_value;
743 $this->sync_on_login = $a_value;
751 $this->sync_per_cron = $a_value;
755 $this->global_role = $a_role;
763 $this->role_bind_dn = $a_value;
771 $this->role_bind_pass = $a_value;
779 $this->role_sync_active = $a_value;
788 $this->username_filter = $a_value;
793 $this->escape_dn = $a_value;
806 $this->account_migration = $a_status;
823 $this->ilErr->setMessage(
'');
828 $this->ilErr->setMessage($this->
lng->txt(
'fill_out_all_required_fields'));
833 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_bind_user'));
837 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_role_assignment'));
840 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_tls_conflict'));
843 return $this->ilErr->getMessage() ===
'';
848 $next_id = $this->db->nextId(
'ldap_server_settings');
850 $query =
'INSERT INTO ldap_server_settings (server_id,active,name,url,version,base_dn,referrals,tls,bind_type,bind_user,bind_pass,' .
851 'search_base,user_scope,user_attribute,filter,group_dn,group_scope,group_filter,group_member,group_memberisdn,group_name,' .
852 'group_attribute,group_optional,group_user_filter,sync_on_login,sync_per_cron,role_sync_active,role_bind_dn,role_bind_pass,migration, ' .
853 'authentication,authentication_type,username_filter, escape_dn) ' .
854 '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)';
858 'integer',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
'text',
'text',
'text',
'integer',
859 'text',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
860 'text',
'text',
'integer',
'integer',
'integer',
"text",
'integer'),
899 $this->server_id = $next_id;
905 $query =
"UPDATE ldap_server_settings SET " .
906 "active = " . $this->db->quote($this->
isActive(),
'integer') .
", " .
907 "name = " . $this->db->quote($this->
getName(),
'text') .
", " .
908 "url = " . $this->db->quote($this->
getUrlString(),
'text') .
", " .
909 "version = " . $this->db->quote($this->
getVersion(),
'integer') .
", " .
910 "base_dn = " . $this->db->quote($this->
getBaseDN(),
'text') .
", " .
911 "referrals = " . $this->db->quote($this->
isActiveReferrer(),
'integer') .
", " .
912 "tls = " . $this->db->quote($this->
isActiveTLS(),
'integer') .
", " .
913 "bind_type = " . $this->db->quote($this->
getBindingType(),
'integer') .
", " .
914 "bind_user = " . $this->db->quote($this->
getBindUser(),
'text') .
", " .
915 "bind_pass = " . $this->db->quote($this->
getBindPassword(),
'text') .
", " .
916 "search_base = " . $this->db->quote($this->
getSearchBase(),
'text') .
", " .
917 "user_scope = " . $this->db->quote($this->
getUserScope(),
'integer') .
", " .
918 "user_attribute = " . $this->db->quote($this->
getUserAttribute(),
'text') .
", " .
919 "filter = " . $this->db->quote($this->
getFilter(),
'text') .
", " .
920 "group_dn = " . $this->db->quote($this->
getGroupDN(),
'text') .
", " .
921 "group_scope = " . $this->db->quote($this->
getGroupScope(),
'integer') .
", " .
922 "group_filter = " . $this->db->quote($this->
getGroupFilter(),
'text') .
", " .
923 "group_member = " . $this->db->quote($this->
getGroupMember(),
'text') .
", " .
925 "group_name = " . $this->db->quote($this->
getGroupName(),
'text') .
", " .
926 "group_attribute = " . $this->db->quote($this->
getGroupAttribute(),
'text') .
", " .
928 "group_user_filter = " . $this->db->quote($this->
getGroupUserFilter(),
'text') .
", " .
929 "sync_on_login = " . $this->db->quote(($this->
enabledSyncOnLogin() ? 1 : 0),
'integer') .
", " .
930 "sync_per_cron = " . $this->db->quote(($this->
enabledSyncPerCron() ? 1 : 0),
'integer') .
", " .
932 "role_bind_dn = " . $this->db->quote($this->
getRoleBindDN(),
'text') .
", " .
937 ", username_filter = " . $this->db->quote($this->
getUsernameFilter(),
"text") .
" " .
938 ", escape_dn = " . $this->db->quote($this->
enabledEscapeDN() ? 1 : 0,
'integer') .
" " .
939 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
941 $this->db->manipulate($query);
948 public function delete():
void
959 foreach ($rules as $ruleAssigment) {
960 $ruleAssigment->delete();
965 $query =
"DELETE FROM ldap_server_settings " .
966 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
967 $this->db->manipulate($query);
991 $options[
'userscope'] =
'one';
993 $options[
'userscope'] =
'sub';
997 $options[
'userfilter'] = $this->
getFilter();
1002 $options[
'enableLogging'] =
true;
1006 $options[
'groupscope'] =
'base';
1009 $options[
'groupscope'] =
'one';
1012 $options[
'groupscope'] =
'sub';
1038 if (strpos(
$filter,
'(') !== 0) {
1041 if (substr(
$filter, -1) !==
')') {
1056 $mapping->getFields(),
1071 if (!$this->server_id) {
1074 $query =
"SELECT * FROM ldap_server_settings WHERE server_id = " . $this->db->quote($this->server_id,
ilDBConstants::T_INTEGER);
1076 $res = $this->db->query($query);
1079 $this->
setName($row->name ??
'');
1080 $this->
setUrl($row->url ??
'');
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.
doConnectionCheck()
Check ldap connection and do a fallback to the next server if no connection is possible.
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.
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.
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)
static getServerIds()
Get all server ids.
setVersion(int $a_version)
enabledRoleSynchronization()
setRoleBindPassword(string $a_value)
static getLogger(string $a_component_id)
Get component logger.
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)