You are here

class SimpleLdapUserController in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/SimpleLdapUserController.class.php \SimpleLdapUserController

Controller class for LDAP users.

Hierarchy

Expanded class hierarchy of SimpleLdapUserController

1 string reference to 'SimpleLdapUserController'
simple_ldap_user_entity_info_alter in simple_ldap_user/simple_ldap_user.module
Implements hook_entity_info_alter().

File

simple_ldap_user/SimpleLdapUserController.class.php, line 10
SimpleLdapUserController class.

View source
class SimpleLdapUserController extends UserController {

  /**
   * Resets the entity cache.
   */
  public function resetCache(array $ids = NULL) {
    if (isset($ids)) {
      foreach ($ids as $id) {
        if (isset($this->entityCache[$id]->name)) {
          SimpleLdapUser::reset($this->entityCache[$id]->name);
        }
      }
    }
    else {
      SimpleLdapUser::reset();
    }
    return parent::resetCache($ids);
  }

  /**
   * Verifies that the user exists in the LDAP directory.
   */
  public function load($ids = array(), $conditions = array()) {
    $users = parent::load($ids, $conditions);

    // Validate users against LDAP directory.
    foreach ($users as $uid => $drupal_user) {

      // Do not validate user/1, anonymous users, or blocked users.
      if ($uid == 1 || $uid == 0 || $drupal_user->status == 0) {
        continue;
      }

      // Try to load the user from LDAP.
      $ldap_user = SimpleLdapUser::singleton($drupal_user->name);

      // Check to see if the user should be kept.
      $result = array_filter(module_invoke_all('simple_ldap_user_should_delete_user', $drupal_user, $ldap_user));
      foreach ($result as $res) {
        if ($res === TRUE) {
          $this
            ->delete_single($drupal_user);
          $users[$uid] = NULL;
          continue;
        }
      }
      if (!$ldap_user->exists) {

        // Block the user if it does not exist in LDAP.
        $this
          ->blockUser($drupal_user);
      }

      // Active Directory uses a bitmask to specify certain flags on an account,
      // including whether it is enabled. http://support.microsoft.com/kb/305144
      if ($ldap_user->server->type == 'Active Directory') {
        if (isset($ldap_user->useraccountcontrol[0]) && (int) $ldap_user->useraccountcontrol[0] & 2) {
          $this
            ->blockUser($drupal_user);
        }
      }
    }
    return $users;
  }

  /**
   * Block a user, setting status to 0. This will store the current status as
   * stored in the database into a separate value, for use in user hooks.
   */
  protected function blockUser(stdClass $account) {
    $account->simple_ldap_user_drupal_status = $account->status;
    $account->simple_ldap_user_ldap_status = 0;
    $account->status = 0;
  }

  /**
   * Delete a user from the system.
   *
   * This is a copy of user_delete() minus the user_load() call.
   */
  private function delete_single(stdClass $account) {
    $uids = array(
      $account->uid,
    );
    $transaction = db_transaction();
    try {
      module_invoke_all('user_delete', $account);
      module_invoke_all('entity_delete', $account, 'user');
      field_attach_delete('user', $account);
      drupal_session_destroy_uid($account->uid);
      db_delete('users')
        ->condition('uid', $uids, 'IN')
        ->execute();
      db_delete('users_roles')
        ->condition('uid', $uids, 'IN')
        ->execute();
      db_delete('authmap')
        ->condition('uid', $uids, 'IN')
        ->execute();
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('user', $e);
      throw $e;
    }
    $this
      ->resetCache($uids);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::buildQuery protected function Builds the query to load the entity. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.
SimpleLdapUserController::blockUser protected function Block a user, setting status to 0. This will store the current status as stored in the database into a separate value, for use in user hooks.
SimpleLdapUserController::delete_single private function Delete a user from the system.
SimpleLdapUserController::load public function Verifies that the user exists in the LDAP directory. Overrides DrupalDefaultEntityController::load
SimpleLdapUserController::resetCache public function Resets the entity cache. Overrides DrupalDefaultEntityController::resetCache
UserController::attachLoad function Attaches data to entities upon loading. Overrides DrupalDefaultEntityController::attachLoad