You are here

function _password_policy_load_active_policy in Password Policy 7

Same name and namespace in other branches
  1. 6 password_policy.module \_password_policy_load_active_policy()

Loads the first enabled policy that matches the specified roles.

Parameters

int[] $roles: An array of role IDs.

object $account: Populated user object.

Return value

array|false A policy array, or FALSE if no active policy exists.

4 calls to _password_policy_load_active_policy()
password_policy_cron in ./password_policy.module
Implements hook_cron().
password_policy_form_alter in ./password_policy.module
Implements hook_form_alter().
password_policy_user_login in ./password_policy.module
Implements hook_user_login().
_password_policy_constraint_validate in ./password_policy.module
Validates user password.

File

./password_policy.module, line 1505
Allows enforcing restrictions on user passwords by defining policies.

Code

function _password_policy_load_active_policy(array $roles, &$account) {
  static $cache = array();
  if (empty($roles)) {
    $roles = array(
      DRUPAL_ANONYMOUS_RID,
    );
  }

  // If the role is a name, not an ID, replace with the ID.
  for ($i = 0; $i < count($roles); $i++) {
    if (!is_numeric($roles[$i])) {
      $row = db_select('role', 'r', array(
        'target' => 'slave',
      ))
        ->fields('r', array(
        'rid',
      ))
        ->condition('r.name', $roles[$i])
        ->execute()
        ->fetchAssoc();
      if (!empty($row['rid'])) {
        $roles[$i] = $row['rid'];
      }
    }
  }
  $key = implode(',', $roles);

  // Use array_key_exists() instead of isset() as NULLs may be in the array.
  if (!array_key_exists($key, $cache)) {
    $query = db_select('password_policy', 'p', array(
      'target' => 'slave',
    ));
    $query
      ->innerJoin('password_policy_role', 'r', 'p.pid = r.pid');
    $row = $query
      ->fields('p')
      ->condition('p.enabled', 1)
      ->condition('r.rid', $roles, 'IN')
      ->orderBy('p.weight')
      ->range(0, 1)
      ->execute()
      ->fetchAssoc();
    if (is_array($row)) {
      $constraints = $row['constraints'];
      $constraints = unserialize($constraints);
      $row['constraints'] = $constraints;
      _password_policy_load_policy_excluded_authentication_modules($row);
      $cache[$key] = $row;
    }
    else {
      $cache[$key] = FALSE;
    }
  }
  $policy = $cache[$key];
  if ($policy && _password_policy_policy_excludes_authentication_module_of_user($policy, $account)) {
    $policy = FALSE;
  }
  return $policy;
}