ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
ilLDAPQuery Class Reference
+ Collaboration diagram for ilLDAPQuery:

Public Member Functions

 __construct (ilLDAPServer $a_server, $a_url='')
 Constructur. More...
 
 fetchUser ($a_name)
 Get one user by login name. More...
 
 fetchUsers ()
 Fetch all users. More...
 
 query ($a_search_base, $a_filter, $a_scope, $a_attributes)
 Perform a query. More...
 
 modAdd ($a_dn, $a_attribute)
 Add value to an existing attribute. More...
 
 modDelete ($a_dn, $a_attribute)
 Delete value from an existing attribute. More...
 
 bind ($a_binding_type=IL_LDAP_BIND_DEFAULT, $a_user_dn='', $a_password='')
 Bind to LDAP server. More...
 
 __destruct ()
 Destructor unbind from ldap server. More...
 

Private Member Functions

 readAllUsers ()
 Fetch all users This function splits the query to filters like e.g (uid=a*) (uid=b*)... More...
 
 fetchGroupMembers ($a_name='')
 Fetch group member ids. More...
 
 readUserData ($a_name, $a_check_dn=true, $a_try_group_user_filter=false)
 Read user data. More...
 
 parseAuthMode ()
 Parse authentication mode. More...
 
 queryByScope ($a_scope, $a_base_dn, $a_filter, $a_attributes)
 Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list. More...
 
 connect ()
 Connect to LDAP server. More...
 
 fetchUserProfileFields ()
 fetch required fields of user profile data More...
 
 unbind ()
 Unbind. More...
 

Private Attributes

 $ldap_server_url = null
 
 $settings = null
 
 $log = null
 
 $user_fields = array()
 

Detailed Description

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
$Id$

Definition at line 41 of file class.ilLDAPQuery.php.

Constructor & Destructor Documentation

◆ __construct()

ilLDAPQuery::__construct ( ilLDAPServer  $a_server,
  $a_url = '' 
)

Constructur.

private

Parameters
objectilLDAPServer or subclass
Exceptions
ilLDAPQueryException

Definition at line 57 of file class.ilLDAPQuery.php.

References $ilLog, ilLDAPAttributeMapping\_getInstanceByServerId(), connect(), fetchUserProfileFields(), and ilLDAPServer\getUrl().

58  {
59  global $ilLog;
60 
61  $this->settings = $a_server;
62 
63  if(strlen($a_url))
64  {
65  $this->ldap_server_url = $a_url;
66  }
67  else
68  {
69  $this->ldap_server_url = $this->settings->getUrl();
70  }
71 
72  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->settings->getServerId());
73  $this->log = $ilLog;
74 
75  $this->fetchUserProfileFields();
76  $this->connect();
77  }
static _getInstanceByServerId($a_server_id)
Get instance of class.
connect()
Connect to LDAP server.
fetchUserProfileFields()
fetch required fields of user profile data
+ Here is the call graph for this function:

◆ __destruct()

ilLDAPQuery::__destruct ( )

Destructor unbind from ldap server.

private

Parameters

Definition at line 605 of file class.ilLDAPQuery.php.

606  {
607  if($this->lh)
608  {
609  @ldap_unbind($this->lh);
610  }
611  }

Member Function Documentation

◆ bind()

ilLDAPQuery::bind (   $a_binding_type = IL_LDAP_BIND_DEFAULT,
  $a_user_dn = '',
  $a_password = '' 
)

Bind to LDAP server.

public

Parameters
intbinding_type IL_LDAP_BIND_DEFAULT || IL_LDAP_BIND_ADMIN
Exceptions
ilLDAPQueryExceptionon connection failure.

Definition at line 506 of file class.ilLDAPQuery.php.

References $pass, ilLDAPServer\DEFAULT_NETWORK_TIMEOUT, IL_LDAP_BIND_ADMIN, IL_LDAP_BIND_DEFAULT, IL_LDAP_BIND_TEST, and IL_LDAP_BIND_USER.

507  {
508  switch($a_binding_type)
509  {
510  case IL_LDAP_BIND_TEST:
511  ldap_set_option($this->lh, LDAP_OPT_NETWORK_TIMEOUT, ilLDAPServer::DEFAULT_NETWORK_TIMEOUT);
512  // fall through
514  // Now bind anonymously or as user
515  if(
516  IL_LDAP_BIND_USER == $this->settings->getBindingType() &&
517  strlen($this->settings->getBindUser())
518  )
519  {
520  $user = $this->settings->getBindUser();
521  $pass = $this->settings->getBindPassword();
522 
523  define('IL_LDAP_REBIND_USER',$user);
524  define('IL_LDAP_REBIND_PASS',$pass);
525  $this->log->write(__METHOD__.': Bind as '.$user);
526  }
527  else
528  {
529  $user = $pass = '';
530  $this->log->write(__METHOD__.': Bind anonymous');
531  }
532  break;
533 
534  case IL_LDAP_BIND_ADMIN:
535  $user = $this->settings->getRoleBindDN();
536  $pass = $this->settings->getRoleBindPassword();
537 
538  if(!strlen($user) or !strlen($pass))
539  {
540  $user = $this->settings->getBindUser();
541  $pass = $this->settings->getBindPassword();
542  }
543 
544  define('IL_LDAP_REBIND_USER',$user);
545  define('IL_LDAP_REBIND_PASS',$pass);
546  break;
547 
548  default:
549  throw new ilLDAPQueryException('LDAP: unknown binding type in: '.__METHOD__);
550  }
551 
552  if(!@ldap_bind($this->lh,$user,$pass))
553  {
554  throw new ilLDAPQueryException('LDAP: Cannot bind as '.$user.' with message: '. ldap_err2str(ldap_errno($this->lh)).' Trying fallback...', ldap_errno($this->lh));
555  }
556  else
557  {
558  $this->log->write(__METHOD__.': Bind successful.');
559  }
560  }
const IL_LDAP_BIND_USER
const IL_LDAP_BIND_DEFAULT
const IL_LDAP_BIND_TEST
const DEFAULT_NETWORK_TIMEOUT
const IL_LDAP_BIND_ADMIN

◆ connect()

ilLDAPQuery::connect ( )
private

Connect to LDAP server.

private

Exceptions
ilLDAPQueryException

Definition at line 460 of file class.ilLDAPQuery.php.

Referenced by __construct().

461  {
462  $this->lh = @ldap_connect($this->ldap_server_url);
463 
464  // LDAP Connect
465  if(!$this->lh)
466  {
467  throw new ilLDAPQueryException("LDAP: Cannot connect to LDAP Server: ".$this->settings->getUrl());
468  }
469  // LDAP Version
470  if(!ldap_set_option($this->lh,LDAP_OPT_PROTOCOL_VERSION,$this->settings->getVersion()))
471  {
472  throw new ilLDAPQueryException("LDAP: Cannot set version to: ".$this->settings->getVersion());
473  }
474  // Switch on referrals
475  if($this->settings->isActiveReferrer())
476  {
477  if(!ldap_set_option($this->lh,LDAP_OPT_REFERRALS,true))
478  {
479  throw new ilLDAPQueryException("LDAP: Cannot switch on LDAP referrals");
480  }
481  #@ldap_set_rebind_proc($this->lh,'referralRebind');
482  }
483  else
484  {
485  ldap_set_option($this->lh,LDAP_OPT_REFERRALS,false);
486  $this->log->write(__METHOD__.': Switching referrals to false.');
487  }
488  // Start TLS
489  if($this->settings->isActiveTLS())
490  {
491  if(!ldap_start_tls($this->lh))
492  {
493  throw new ilLDAPQueryException("LDAP: Cannot start LDAP TLS");
494  }
495  }
496  }
+ Here is the caller graph for this function:

◆ fetchGroupMembers()

ilLDAPQuery::fetchGroupMembers (   $a_name = '')
private

Fetch group member ids.

public

Definition at line 273 of file class.ilLDAPQuery.php.

References $data, $res, queryByScope(), and readUserData().

Referenced by fetchUsers().

274  {
275  $group_name = strlen($a_name) ? $a_name : $this->settings->getGroupName();
276 
277  // Build filter
278  $filter = sprintf('(&(%s=%s)%s)',
279  $this->settings->getGroupAttribute(),
280  $group_name,
281  $this->settings->getGroupFilter());
282 
283 
284  // Build search base
285  if(($gdn = $this->settings->getGroupDN()) && substr($gdn,-1) != ',')
286  {
287  $gdn .= ',';
288  }
289  $gdn .= $this->settings->getBaseDN();
290 
291  $this->log->write('LDAP: Using filter '.$filter);
292  $this->log->write('LDAP: Using DN '.$gdn);
293  $res = $this->queryByScope($this->settings->getGroupScope(),
294  $gdn,
295  $filter,
296  array($this->settings->getGroupMember()));
297 
298  $tmp_result = new ilLDAPResult($this->lh,$res);
299  $group_data = $tmp_result->getRows();
300 
301 
302  if(!$tmp_result->numRows())
303  {
304  $this->log->write(__METHOD__.': No group found.');
305  return false;
306  }
307 
308  $attribute_name = strtolower($this->settings->getGroupMember());
309 
310  // All groups
311  foreach($group_data as $data)
312  {
313  $this->log->write(__METHOD__.': found '.count($data[$attribute_name]).' group members for group '.$data['dn']);
314  if(is_array($data[$attribute_name]))
315  {
316  foreach($data[$attribute_name] as $name)
317  {
318  $this->readUserData($name,true,true);
319  }
320  }
321  else
322  {
323  $this->readUserData($data[$attribute_name],true,true);
324  }
325  }
326  unset($tmp_result);
327  return;
328  }
readUserData($a_name, $a_check_dn=true, $a_try_group_user_filter=false)
Read user data.
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
queryByScope($a_scope, $a_base_dn, $a_filter, $a_attributes)
Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetchUser()

ilLDAPQuery::fetchUser (   $a_name)

Get one user by login name.

public

Parameters
stringlogin name
Returns
array of user data

Definition at line 86 of file class.ilLDAPQuery.php.

References readUserData().

87  {
88  $this->user_fields = array_merge(array($this->settings->getUserAttribute()),$this->mapping->getFields());
89 
90  if(!$this->readUserData($a_name))
91  {
92  return array();
93  }
94  else
95  {
96  return $this->users;
97  }
98  }
readUserData($a_name, $a_check_dn=true, $a_try_group_user_filter=false)
Read user data.
+ Here is the call graph for this function:

◆ fetchUserProfileFields()

ilLDAPQuery::fetchUserProfileFields ( )
private

fetch required fields of user profile data

private

Parameters

Definition at line 569 of file class.ilLDAPQuery.php.

References ilLDAPRoleAssignmentRules\getAttributeNames().

Referenced by __construct().

570  {
571  include_once('Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php');
572 
573  $this->user_fields = array_merge(
574  array($this->settings->getUserAttribute()),
575  array('dn'),
576  $this->mapping->getFields(),
578  );
579  }
static getAttributeNames()
get all possible attribute names
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetchUsers()

ilLDAPQuery::fetchUsers ( )

Fetch all users.

public

Returns
array array of user data

Definition at line 107 of file class.ilLDAPQuery.php.

References fetchGroupMembers(), and readAllUsers().

108  {
109  // First of all check if a group restriction is enabled
110  // YES: => fetch all group members
111  // No: => fetch all users
112  if(strlen($this->settings->getGroupName()))
113  {
114  $this->log->write(__METHOD__.': Searching for group members.');
115 
116  $groups = $this->settings->getGroupNames();
117  if(count($groups) <= 1)
118  {
119  $this->fetchGroupMembers();
120  }
121  else
122  {
123  foreach($groups as $group)
124  {
125  $this->fetchGroupMembers($group);
126  }
127  }
128 
129  }
130  if(!strlen($this->settings->getGroupName()) or $this->settings->isMembershipOptional())
131  {
132  $this->log->write(__METHOD__.': Start reading all users...');
133  $this->readAllUsers();
134  #throw new ilLDAPQueryException('LDAP: Called import of users without specifying group restrictions. NOT IMPLEMENTED YET!');
135  }
136  return $this->users ? $this->users : array();
137  }
readAllUsers()
Fetch all users This function splits the query to filters like e.g (uid=a*) (uid=b*)...
fetchGroupMembers($a_name='')
Fetch group member ids.
+ Here is the call graph for this function:

◆ modAdd()

ilLDAPQuery::modAdd (   $a_dn,
  $a_attribute 
)

Add value to an existing attribute.

public

Exceptions
ilLDAPQueryException

Definition at line 170 of file class.ilLDAPQuery.php.

171  {
172  if(@ldap_mod_add($this->lh,$a_dn,$a_attribute))
173  {
174  return true;
175  }
176  throw new ilLDAPQueryException(__METHOD__.' '.ldap_error($this->lh));
177  }

◆ modDelete()

ilLDAPQuery::modDelete (   $a_dn,
  $a_attribute 
)

Delete value from an existing attribute.

public

Exceptions
ilLDAPQueryException

Definition at line 185 of file class.ilLDAPQuery.php.

186  {
187  if(@ldap_mod_del($this->lh,$a_dn,$a_attribute))
188  {
189  return true;
190  }
191  throw new ilLDAPQueryException(__METHOD__.' '.ldap_error($this->lh));
192  }

◆ parseAuthMode()

ilLDAPQuery::parseAuthMode ( )
private

Parse authentication mode.

Returns
string auth mode

Definition at line 408 of file class.ilLDAPQuery.php.

References ilAuthUtils\_getAuthModeName().

Referenced by readUserData().

409  {
410  if($this->settings->isAuthenticationEnabled() or !$this->settings->getAuthenticationMapping())
411  {
412  return 'ldap';
413  }
414  return ilAuthUtils::_getAuthModeName($this->settings->getAuthenticationMapping());
415  }
static _getAuthModeName($a_auth_key)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query()

ilLDAPQuery::query (   $a_search_base,
  $a_filter,
  $a_scope,
  $a_attributes 
)

Perform a query.

public

Parameters
stringsearch base
stringfilter
intscope
arrayattributes
Returns
object ilLDAPResult
Exceptions
ilLDAPQueryException

Definition at line 150 of file class.ilLDAPQuery.php.

References $res, and queryByScope().

151  {
152  $res = $this->queryByScope($a_scope,$a_search_base,$a_filter,$a_attributes);
153  if($res === false)
154  {
155  throw new ilLDAPQueryException(__METHOD__.' '.ldap_error($this->lh).' '.
156  sprintf('DN: %s, Filter: %s, Scope: %s',
157  $a_search_base,
158  $a_filter,
159  $a_scope));
160  }
161  return new ilLDAPResult($this->lh,$res);
162  }
queryByScope($a_scope, $a_base_dn, $a_filter, $a_attributes)
Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.
+ Here is the call graph for this function:

◆ queryByScope()

ilLDAPQuery::queryByScope (   $a_scope,
  $a_base_dn,
  $a_filter,
  $a_attributes 
)
private

Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.

private

Parameters

Definition at line 426 of file class.ilLDAPQuery.php.

References $res, IL_LDAP_SCOPE_BASE, IL_LDAP_SCOPE_ONE, and IL_LDAP_SCOPE_SUB.

Referenced by fetchGroupMembers(), query(), readAllUsers(), and readUserData().

427  {
428  $a_filter = $a_filter ? $a_filter : "(objectclass=*)";
429 
430  switch($a_scope)
431  {
432  case IL_LDAP_SCOPE_SUB:
433  $res = @ldap_search($this->lh,$a_base_dn,$a_filter,$a_attributes);
434  break;
435 
436  case IL_LDAP_SCOPE_ONE:
437  $res = @ldap_list($this->lh,$a_base_dn,$a_filter,$a_attributes);
438  break;
439 
440  case IL_LDAP_SCOPE_BASE:
441 
442  $res = ldap_read($this->lh,$a_base_dn,$a_filter,$a_attributes);
443  break;
444 
445  default:
446  $this->log->write("LDAP: LDAPQuery: Unknown search scope");
447  }
448 
449  return $res;
450 
451  }
const IL_LDAP_SCOPE_SUB
const IL_LDAP_SCOPE_ONE
const IL_LDAP_SCOPE_BASE
+ Here is the caller graph for this function:

◆ readAllUsers()

ilLDAPQuery::readAllUsers ( )
private

Fetch all users This function splits the query to filters like e.g (uid=a*) (uid=b*)...

This avoids AD page_size_limit

public

Definition at line 202 of file class.ilLDAPQuery.php.

References $data, $res, queryByScope(), and readUserData().

Referenced by fetchUsers().

203  {
204  // Build search base
205  if(($dn = $this->settings->getSearchBase()) && substr($dn,-1) != ',')
206  {
207  $dn .= ',';
208  }
209  $dn .= $this->settings->getBaseDN();
210 
211  // page results
212  $filter = $this->settings->getFilter();
213  $page_filter = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-');
214  $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
215 
216  foreach($page_filter as $letter)
217  {
218  $new_filter = '(&';
219  $new_filter .= $filter;
220 
221  switch($letter)
222  {
223  case '-':
224  $new_filter .= ('(!(|');
225  foreach($chars as $char)
226  {
227  $new_filter .= ('('.$this->settings->getUserAttribute().'='.$char.'*)');
228  }
229  $new_filter .= ')))';
230  break;
231 
232  default:
233  $new_filter .= ('('.$this->settings->getUserAttribute().'='.$letter.'*))');
234  break;
235  }
236 
237  $this->log->write(__METHOD__.': Searching with ldap search and filter '.$new_filter.' in '.$dn);
238  $res = $this->queryByScope($this->settings->getUserScope(),
239  $dn,
240  $new_filter,
241  array($this->settings->getUserAttribute()));
242 
243  $tmp_result = new ilLDAPResult($this->lh,$res);
244  if(!$tmp_result->numRows())
245  {
246  $this->log->write(__METHOD__.': No users found. Aborting.');
247  continue;
248  }
249  $this->log->write(__METHOD__.': Found '.$tmp_result->numRows().' users.');
250  foreach($tmp_result->getRows() as $data)
251  {
252  if(isset($data[$this->settings->getUserAttribute()]))
253  {
254  $this->readUserData($data[$this->settings->getUserAttribute()],false,false);
255  }
256  else
257  {
258  $this->log->write(__METHOD__.': Unknown error. No user attribute found.');
259  }
260  }
261  unset($tmp_result);
262  }
263  return true;
264  }
readUserData($a_name, $a_check_dn=true, $a_try_group_user_filter=false)
Read user data.
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
queryByScope($a_scope, $a_base_dn, $a_filter, $a_attributes)
Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readUserData()

ilLDAPQuery::readUserData (   $a_name,
  $a_check_dn = true,
  $a_try_group_user_filter = false 
)
private

Read user data.

Parameters
boolcheck dn
booluse group filter private

Definition at line 336 of file class.ilLDAPQuery.php.

References $res, ilObjUser\_checkExternalAuthAccount(), IL_LDAP_SCOPE_BASE, parseAuthMode(), and queryByScope().

Referenced by fetchGroupMembers(), fetchUser(), and readAllUsers().

337  {
338  $filter = $this->settings->getFilter();
339  if($a_try_group_user_filter)
340  {
341  if($this->settings->isMembershipOptional())
342  {
343  $filter = $this->settings->getGroupUserFilter();
344  }
345  }
346 
347  // Build filter
348  if($this->settings->enabledGroupMemberIsDN() and $a_check_dn)
349  {
350  $dn = $a_name;
351  #$res = $this->queryByScope(IL_LDAP_SCOPE_BASE,$dn,$filter,$this->user_fields);
352 
353  $fields = array_merge($this->user_fields,array('useraccountcontrol'));
354  $res = $this->queryByScope(IL_LDAP_SCOPE_BASE,strtolower($dn),$filter,$fields);
355  }
356  else
357  {
358  $filter = sprintf('(&(%s=%s)%s)',
359  $this->settings->getUserAttribute(),
360  $a_name,
361  $filter);
362 
363  // Build search base
364  if(($dn = $this->settings->getSearchBase()) && substr($dn,-1) != ',')
365  {
366  $dn .= ',';
367  }
368  $dn .= $this->settings->getBaseDN();
369  $fields = array_merge($this->user_fields,array('useraccountcontrol'));
370  $res = $this->queryByScope($this->settings->getUserScope(),strtolower($dn),$filter,$fields);
371  }
372 
373 
374  $tmp_result = new ilLDAPResult($this->lh,$res);
375  if(!$tmp_result->numRows())
376  {
377  $this->log->write('LDAP: No user data found for: '.$a_name);
378  unset($tmp_result);
379  return false;
380  }
381 
382  if($user_data = $tmp_result->get())
383  {
384  if(isset($user_data['useraccountcontrol']))
385  {
386  if(($user_data['useraccountcontrol'] & 0x02))
387  {
388  $this->log->write(__METHOD__.': '.$a_name.' account disabled.');
389  return;
390  }
391  }
392 
393  $user_ext = $user_data[strtolower($this->settings->getUserAttribute())];
394 
395  // auth mode depends on ldap server settings
396  $auth_mode = $this->parseAuthMode();
397 
398  $user_data['ilInternalAccount'] = ilObjUser::_checkExternalAuthAccount($auth_mode,$user_ext);
399  $this->users[$user_ext] = $user_data;
400  }
401  return true;
402  }
parseAuthMode()
Parse authentication mode.
static _checkExternalAuthAccount($a_auth, $a_account)
check whether external account and authentication method matches with a user
const IL_LDAP_SCOPE_BASE
queryByScope($a_scope, $a_base_dn, $a_filter, $a_attributes)
Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unbind()

ilLDAPQuery::unbind ( )
private

Unbind.

private

Parameters

Definition at line 589 of file class.ilLDAPQuery.php.

590  {
591  if($this->lh)
592  {
593  @ldap_unbind($this->lh);
594  }
595  }

Field Documentation

◆ $ldap_server_url

ilLDAPQuery::$ldap_server_url = null
private

Definition at line 43 of file class.ilLDAPQuery.php.

◆ $log

ilLDAPQuery::$log = null
private

Definition at line 45 of file class.ilLDAPQuery.php.

◆ $settings

ilLDAPQuery::$settings = null
private

Definition at line 44 of file class.ilLDAPQuery.php.

◆ $user_fields

ilLDAPQuery::$user_fields = array()
private

Definition at line 47 of file class.ilLDAPQuery.php.


The documentation for this class was generated from the following file: