You are here

public function SimpleLdapRole::__construct in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 simple_ldap_role/SimpleLdapRole.class.php \SimpleLdapRole::__construct()

Constructor.

@throw SimpleLdapException

Parameters

string $name: The Drupal role name to search for, and load from LDAP.

File

simple_ldap_role/SimpleLdapRole.class.php, line 30
SimpleLdapRole class file.

Class

SimpleLdapRole
@file SimpleLdapRole class file.

Code

public function __construct($name) {

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

  // Get the LDAP configuration.
  $basedn = simple_ldap_role_variable_get('simple_ldap_role_basedn');
  $scope = simple_ldap_role_variable_get('simple_ldap_role_scope');
  $attribute_name = simple_ldap_role_variable_get('simple_ldap_role_attribute_name');
  $attribute_member = simple_ldap_role_variable_get('simple_ldap_role_attribute_member');
  $safe_name = preg_replace(array(
    '/\\(/',
    '/\\)/',
  ), array(
    '\\\\(',
    '\\\\)',
  ), $name);
  $filter = '(&(' . $attribute_name . '=' . $safe_name . ')' . self::filter() . ')';

  // Attempt to load the role from the LDAP server.
  $attributes = array(
    $attribute_name,
    $attribute_member,
  );
  $result = $this->server
    ->search($basedn, $filter, $scope, $attributes, 0, 1);
  if ($result['count'] == 1) {

    // Found an existing LDAP entry.
    $this->dn = $result[0]['dn'];
    $this->attributes[$attribute_name] = $result[0][$attribute_name];
    if (isset($result[0][$attribute_member])) {
      $this->attributes[$attribute_member] = $result[0][$attribute_member];
    }
    else {
      $this->attributes[$attribute_member] = array(
        'count' => 0,
      );
    }
    $this->exists = TRUE;
  }
  else {

    // Set up a new LDAP entry.
    $this->dn = $attribute_name . '=' . $name . ',' . $basedn;
    $this->attributes[$attribute_name] = array(
      'count' => 1,
      0 => $name,
    );
    $this->attributes[$attribute_member] = array(
      'count' => 0,
    );
    $this->dirty = TRUE;
  }
}