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";
276 $query =
"SELECT * FROM ldap_server_settings ORDER BY name";
293 $query =
"SELECT server_id FROM ldap_server_settings " .
294 "WHERE active = " .
$ilDB->quote(1,
'integer') .
" " .
295 "AND authentication = " .
$ilDB->quote(0,
'integer') .
" " .
296 "AND ( authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
297 "OR authentication_type = " .
$ilDB->quote(0,
'integer') .
")";
300 $server_ids = array();
302 $server_ids[] = $row->server_id;
316 $query =
"SELECT server_id FROM ldap_server_settings " .
317 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
318 "AND authentication = " .
$ilDB->quote(0,
'integer');
332 $query =
"SELECT server_id FROM ldap_server_settings " .
333 "WHERE authentication_type = " .
$ilDB->quote($a_auth_mode,
'integer') .
" ";
336 return $row->server_id;
350 $query =
'UPDATE ldap_server_settings ' .
351 'SET authentication_type = ' .
$ilDB->quote(0,
'integer') .
' ' .
352 'WHERE authentication_type = ' .
$ilDB->quote($a_authmode,
'integer');
363 public static function toggleDataSource(
int $a_ldap_server_id,
int $a_auth_mode,
int $a_status): bool
372 $query =
"UPDATE ldap_server_settings " .
373 'SET authentication_type = ' .
$ilDB->quote($a_auth_mode,
'integer') .
" " .
374 'WHERE server_id = ' .
$ilDB->quote($a_ldap_server_id,
'integer');
387 $logger =
$DIC->logger()->auth();
389 if ($a_auth_mode ===
'') {
390 $logger->error(__METHOD__ .
': No auth mode given..............');
394 $auth_arr = explode(
'_', $a_auth_mode);
404 if (self::isAuthModeLDAP($a_auth_mode)) {
405 $auth_arr = explode(
'_', $a_auth_mode);
406 return (
int) $auth_arr[1];
417 $auth_arr = explode(
'_', $a_auth_key);
418 if (count($auth_arr) > 1) {
419 return 'ldap_' . $auth_arr[1];
430 $auth_arr = explode(
'_', $a_auth_mode);
431 if (count($auth_arr) > 1) {
448 $this->enabled_authentication = $a_status;
464 $this->authentication_mapping = $a_map;
489 $this->active = $a_status;
499 public function setUrl(
string $a_url): void
501 $this->url_string = $a_url;
504 $urls = explode(
',', $a_url);
507 foreach ($urls as
$url) {
512 $this->fallback_urls[] =
$url;
530 foreach (array_merge(array(0 => $this->url), $this->fallback_urls) as
$url) {
540 ilLoggerFactory::getLogger(
'auth')->error(
'Cannot connect to LDAP server: ' . $url .
' ' . $exc->getCode() .
' ' . $exc->getMessage());
554 $this->name = $a_name;
562 $this->version = $a_version;
570 $this->base_dn = $a_base_dn;
578 $this->referrals = $a_status;
586 $this->tls = $a_status;
594 if ($a_type === self::LDAP_BIND_USER) {
606 $this->bind_user = $a_user;
614 $this->bind_password = $a_password;
622 $this->search_base = $a_search_base;
630 $this->user_attribute = $a_user_attr;
638 $this->
filter = $a_filter;
646 $this->group_dn = $a_value;
654 $this->group_filter = $a_value;
662 $this->group_member = $a_value;
670 $this->group_name = $a_value;
681 if (!is_array($names)) {
685 return array_filter(array_map(
'trim', $names));
695 $this->group_attribute = $a_value;
699 $this->group_optional = $a_status;
707 $this->group_user_filter = $a_filter;
720 $this->memberisdn = $a_value;
724 $this->group_scope = $a_value;
732 $this->user_scope = $a_value;
744 $this->sync_on_login = $a_value;
752 $this->sync_per_cron = $a_value;
756 $this->global_role = $a_role;
764 $this->role_bind_dn = $a_value;
772 $this->role_bind_pass = $a_value;
780 $this->role_sync_active = $a_value;
789 $this->username_filter = $a_value;
794 $this->escape_dn = $a_value;
807 $this->account_migration = $a_status;
824 $this->ilErr->setMessage(
'');
829 $this->ilErr->setMessage($this->
lng->txt(
'fill_out_all_required_fields'));
834 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_bind_user'));
838 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_missing_role_assignment'));
841 $this->ilErr->appendMessage($this->
lng->txt(
'ldap_tls_conflict'));
844 return $this->ilErr->getMessage() ===
'';
849 $next_id = $this->db->nextId(
'ldap_server_settings');
851 $query =
'INSERT INTO ldap_server_settings (server_id,active,name,url,version,base_dn,referrals,tls,bind_type,bind_user,bind_pass,' .
852 'search_base,user_scope,user_attribute,filter,group_dn,group_scope,group_filter,group_member,group_memberisdn,group_name,' .
853 'group_attribute,group_optional,group_user_filter,sync_on_login,sync_per_cron,role_sync_active,role_bind_dn,role_bind_pass,migration, ' .
854 'authentication,authentication_type,username_filter, escape_dn) ' .
855 '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)';
859 'integer',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
'text',
'text',
'text',
'integer',
860 'text',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'text',
'integer',
'text',
'integer',
'integer',
'integer',
861 'text',
'text',
'integer',
'integer',
'integer',
"text",
'integer'),
900 $this->server_id = $next_id;
906 $query =
"UPDATE ldap_server_settings SET " .
907 "active = " . $this->db->quote($this->
isActive(),
'integer') .
", " .
908 "name = " . $this->db->quote($this->
getName(),
'text') .
", " .
909 "url = " . $this->db->quote($this->
getUrlString(),
'text') .
", " .
910 "version = " . $this->db->quote($this->
getVersion(),
'integer') .
", " .
911 "base_dn = " . $this->db->quote($this->
getBaseDN(),
'text') .
", " .
912 "referrals = " . $this->db->quote($this->
isActiveReferrer(),
'integer') .
", " .
913 "tls = " . $this->db->quote($this->
isActiveTLS(),
'integer') .
", " .
914 "bind_type = " . $this->db->quote($this->
getBindingType(),
'integer') .
", " .
915 "bind_user = " . $this->db->quote($this->
getBindUser(),
'text') .
", " .
916 "bind_pass = " . $this->db->quote($this->
getBindPassword(),
'text') .
", " .
917 "search_base = " . $this->db->quote($this->
getSearchBase(),
'text') .
", " .
918 "user_scope = " . $this->db->quote($this->
getUserScope(),
'integer') .
", " .
919 "user_attribute = " . $this->db->quote($this->
getUserAttribute(),
'text') .
", " .
920 "filter = " . $this->db->quote($this->
getFilter(),
'text') .
", " .
921 "group_dn = " . $this->db->quote($this->
getGroupDN(),
'text') .
", " .
922 "group_scope = " . $this->db->quote($this->
getGroupScope(),
'integer') .
", " .
923 "group_filter = " . $this->db->quote($this->
getGroupFilter(),
'text') .
", " .
924 "group_member = " . $this->db->quote($this->
getGroupMember(),
'text') .
", " .
926 "group_name = " . $this->db->quote($this->
getGroupName(),
'text') .
", " .
927 "group_attribute = " . $this->db->quote($this->
getGroupAttribute(),
'text') .
", " .
929 "group_user_filter = " . $this->db->quote($this->
getGroupUserFilter(),
'text') .
", " .
930 "sync_on_login = " . $this->db->quote(($this->
enabledSyncOnLogin() ? 1 : 0),
'integer') .
", " .
931 "sync_per_cron = " . $this->db->quote(($this->
enabledSyncPerCron() ? 1 : 0),
'integer') .
", " .
933 "role_bind_dn = " . $this->db->quote($this->
getRoleBindDN(),
'text') .
", " .
938 ", username_filter = " . $this->db->quote($this->
getUsernameFilter(),
"text") .
" " .
939 ", escape_dn = " . $this->db->quote($this->
enabledEscapeDN() ? 1 : 0,
'integer') .
" " .
940 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
942 $this->db->manipulate(
$query);
949 public function delete():
void
960 foreach ($rules as $ruleAssigment) {
961 $ruleAssigment->delete();
966 $query =
"DELETE FROM ldap_server_settings " .
967 "WHERE server_id = " . $this->db->quote($this->
getServerId(),
'integer');
968 $this->db->manipulate(
$query);
992 $options[
'userscope'] =
'one';
994 $options[
'userscope'] =
'sub';
998 $options[
'userfilter'] = $this->
getFilter();
1003 $options[
'enableLogging'] =
true;
1007 $options[
'groupscope'] =
'base';
1010 $options[
'groupscope'] =
'one';
1013 $options[
'groupscope'] =
'sub';
1039 if (strpos(
$filter,
'(') !== 0) {
1042 if (substr(
$filter, -1) !==
')') {
1057 $mapping->getFields(),
1072 if (!$this->server_id) {
1080 $this->
setName($row->name ??
'');
1081 $this->
setUrl($row->url ??
'');
static _getAuthModeName($a_auth_key)
Error Handling & global info handling uses PEAR error class.
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.