19 declare(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');
143 $ilDB = $DIC[
'ilDB'];
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;
167 $ilDB = $DIC[
'ilDB'];
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;
192 $ilDB = $DIC[
'ilDB'];
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;
215 $servers = self::_getActiveServerList();
216 if (count($servers)) {
231 $ilDB = $DIC[
'ilDB'];
233 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
238 while ($row =
$ilDB->fetchObject(
$res)) {
239 $server_ids[] = $row->server_id;
252 $ilDB = $DIC[
'ilDB'];
254 $query =
"SELECT server_id FROM ldap_server_settings ORDER BY name";
273 $ilDB = $DIC[
'ilDB'];
275 $query =
'SELECT * FROM ldap_server_settings ORDER BY name';
290 $ilDB = $DIC[
'ilDB'];
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;
313 $ilDB = $DIC[
'ilDB'];
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');
329 $ilDB = $DIC[
'ilDB'];
331 $query =
"SELECT server_id FROM ldap_server_settings " .
332 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" ";
335 return $row->server_id;
347 $ilDB = $DIC[
'ilDB'];
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
366 $ilDB = $DIC[
'ilDB'];
368 self::disableDataSourceForAuthMode($a_auth_mode);
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) {
594 $this->binding_type = self::LDAP_BIND_USER;
596 $this->binding_type = self::LDAP_BIND_ANONYMOUS;
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;
1005 case self::LDAP_SCOPE_BASE:
1006 $options[
'groupscope'] =
'base';
1008 case self::LDAP_SCOPE_ONE:
1009 $options[
'groupscope'] =
'one';
1012 $options[
'groupscope'] =
'sub';
1032 $filter = trim($a_filter);
1034 if ($filter ===
'') {
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 checkLDAPLib()
Check if ldap module is installed.
static _getFirstActiveServer()
Get first active server.
static disableDataSourceForAuthMode(int $a_authmode)
Disable data source.
toggleMembershipOptional(bool $a_status)
read()
Read server settings.
setBindingType(int $a_type)
static getDataSource(int $a_auth_mode)
static getAvailableDataSources(int $a_auth_mode)
setAuthenticationMapping(int $a_map)
Set mapped authentication mapping.
enabledRoleSynchronization()
static getLogger(string $a_component_id)
Get component logger.
static _getServerList()
Get list of all configured servers.
enableAuthentication(bool $a_status)
Enable authentication for this ldap server.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
enableSyncPerCron(bool $a_value)
static _deleteByServerId(int $a_server_id)
static _getAllServer()
Get list of all configured servers.
validate()
Validate user input.
setFilter(string $a_filter)
static _getActiveServerList()
Get active server list.
getPearAtributeArray()
Get attribute array for pear auth data.
static toggleDataSource(int $a_ldap_server_id, int $a_auth_mode, int $a_status)
Toggle Data Source.
static _getAuthModeName($a_auth_key)
static getAttributeNames($a_server_id)
get all possible attribute names
setGroupMember(string $a_value)
static _getRules($a_server_id)
Get all rules.
setGroupAttribute(string $a_value)
setGroupName(string $a_value)
toggleReferrer(bool $a_status)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static isDataSourceActive(int $a_auth_mode)
Check if a data source is active for a specific auth mode.
const DEFAULT_NETWORK_TIMEOUT
const LDAP_BIND_ANONYMOUS
getGroupNames()
Get group names as array.
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static getServerIds()
Get all server ids.
getAuthenticationMapping()
Get authentication mode that is mapped.
doConnectionCheck()
Check ldap connection and do a fallback to the next server if no connection is possible.
toggleActive(bool $a_status)
enableEscapeDN(bool $a_value)
static _getCronServerIds()
Get list of acticve servers with option 'SyncCron'.
bool $enabled_authentication
enableSyncOnLogin(bool $a_value)
setGroupUserFilter(string $a_filter)
__construct(int $a_server_id=0)
toPearAuthArray()
Creates an array of options compatible to PEAR Auth.
setUserAttribute(string $a_user_attr)
isAuthenticationEnabled()
Check if authentication is enabled.
setVersion(int $a_version)
int $authentication_mapping
enableGroupMemberIsDN(bool $a_value)
enableRoleSynchronization(bool $a_value)
string $group_user_filter
setBindPassword(string $a_password)
toggleTLS(bool $a_status)
setRoleBindDN(string $a_value)
setGroupFilter(string $a_value)
setGlobalRole(int $a_role)
setBindUser(string $a_user)
static isAuthModeLDAP(string $a_auth_mode)
Check if user auth mode is LDAP.
setGroupDN(string $a_value)
setGroupScope(int $a_value)
rotateFallbacks()
Rotate fallback urls in case of connect timeouts.
setUserScope(int $a_value)
setSearchBase(string $a_search_base)
getAuthenticationMappingKey()
Get authentication mapping key Default is ldap.
setUsernameFilter(string $a_value)
static _delete(int $a_server_id)
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
setRoleBindPassword(string $a_value)
static _getInstanceByServerId(int $a_server_id)
setBaseDN(string $a_base_dn)
static _getRoleSyncServerIds()
Check whether there if there is an active server with option role_sync_active.
static getAuthModeByKey(string $a_auth_key)
get auth mode by key
enableAccountMigration(bool $a_status)
Enable account migration.
prepareFilter(string $a_filter)
Create brackets for filters if they do not exist.
isAccountMigrationEnabled()
enabled account migration