You are here

public function SimpleLdapUser::__construct in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/SimpleLdapUser.class.php \SimpleLdapUser::__construct()

Constructor.

@throw SimpleLdapException

Parameters

string $name: The drupal user name or email address to search for, and load from LDAP.

File

simple_ldap_user/SimpleLdapUser.class.php, line 27
Class defining a simple LDAP user.

Class

SimpleLdapUser
@file Class defining a simple LDAP user.

Code

public function __construct($name) {

  // Load the LDAP server object.
  $this->server = SimpleLdapServer::singleton();

  // Get the LDAP configuration.
  $base_dn = simple_ldap_user_variable_get('simple_ldap_user_basedn');
  $scope = simple_ldap_user_variable_get('simple_ldap_user_scope');
  $attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
  $attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');
  $puid_attr = simple_ldap_user_variable_get('simple_ldap_user_unique_attribute');
  $safe_name = preg_replace(array(
    '/\\(/',
    '/\\)/',
  ), array(
    '\\\\(',
    '\\\\)',
  ), $name);

  // Search first for the user by name, then by email and finally by PUID.
  // Ensures that if someone has a username that is an email address, we find only
  // one record.
  $filter_list = array();
  $filter_list[] = '(&(' . $attribute_name . '=' . $safe_name . ')' . self::filter() . ')';
  $filter_list[] = '(&(' . $attribute_mail . '=' . $safe_name . ')' . self::filter() . ')';
  if ($puid_attr) {
    $filter_list[] = '(&(' . $puid_attr . '=' . $safe_name . ')' . self::filter() . ')';
  }

  // List of attributes to fetch from the LDAP server.
  // Using key => value autmatically dedups the list.
  $attributes = array(
    $attribute_name => $attribute_name,
    $attribute_mail => $attribute_mail,
  );
  $attribute_map = simple_ldap_user_variable_get('simple_ldap_user_attribute_map');

  // Collect all the attributes to load
  $attributes = array_keys($attribute_map);
  $config_extra_attributes = array_values(simple_ldap_user_variable_get('simple_ldap_user_extra_attrs'));
  $hook_extra_attributes = array_values(module_invoke_all('simple_ldap_user_extra_attributes', $this->server));

  // Merge them into a single array.
  $attributes = array_merge($attributes, $config_extra_attributes, $hook_extra_attributes);

  // Add the unique attribute, if it is set.
  if ($puid_attr) {
    $attributes[] = $puid_attr;
  }

  // filter to keep ldap_search happy
  $attributes = array_unique(array_map('strtolower', array_values($attributes)));

  // Include the userAccountControl attribute for Active Directory.
  try {
    if ($this->server->type == 'Active Directory') {
      $attributes['useraccountcontrol'] = 'useraccountcontrol';
    }
  } catch (SimpleLdapException $e) {
  }
  foreach ($filter_list as $filter) {

    // Attempt to load the user from the LDAP server.
    try {
      $result = $this->server
        ->search($base_dn, $filter, $scope, array_values($attributes), 0, 1);
    } catch (SimpleLdapException $e) {
      if ($e
        ->getCode() == -1) {
        $result = array(
          'count' => 0,
        );
      }
      else {
        throw $e;
      }
    }
    if ($result['count'] == 1) {
      break;
    }
  }

  // Populate the attribute array.
  if ($result['count'] == 1) {
    $this->dn = $result[0]['dn'];
    foreach ($attributes as $attribute) {
      $attribute = strtolower($attribute);

      // Search for the attribute in the LDAP schema.
      $schema_attribute = $this->server->schema
        ->get('attributeTypes', $attribute);
      $schema_attribute_name = strtolower($schema_attribute['name']);

      // Check whether the attribute or any of its aliases are present in the
      // LDAP user.
      $found = FALSE;
      if (isset($result[0][$schema_attribute_name])) {
        $found = $schema_attribute_name;
      }
      if (!$found) {
        foreach ($schema_attribute['aliases'] as $alias) {
          $alias = strtolower($alias);
          if (isset($result[0][$alias])) {
            $found = $alias;
            break;
          }
        }
      }

      // Assign the attribute value to the SimpleLdapUser object.
      if ($found) {
        $this->attributes[$attribute] = $result[0][$found];
      }
    }
    $this->exists = TRUE;
  }
  else {
    $this->attributes[$attribute_name] = array(
      'count' => 1,
      0 => $name,
    );
  }
}