You are here

function password_policy_add_policy_js in Password Policy 7

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

Adds password policy JS.

Parameters

array $policy: A policy array.

object $account: User object of user for which the policy is applied.

array $render_array: (Optional) A renderable array to attach the JavaScript to. If not provided, the JavaScript will be added to the page directly.

1 call to password_policy_add_policy_js()
password_policy_form_alter in ./password_policy.module
Implements hook_form_alter().

File

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

Code

function password_policy_add_policy_js(array $policy, $account, array &$render_array = NULL) {
  $s = <<<JS
  /**
   * Evaluates the strength of a user's password.
   *
   * Returns the estimated strength and the relevant output message.
   */
  Drupal.evaluatePasswordStrength = function (value) {
    var strength = 'high';
    var msg = [];
    var translate = Drupal.settings.password;
    // Merge Password Policy translations.
    for (var setting in Drupal.settings.passwordPolicy) {
      translate[setting] = Drupal.settings.passwordPolicy[setting];
    }
    var trimmedSpaces = /^\\s+|\\s+\$/.test(value);
    if (/^\\s+\$/.test(value)) {
      return {
        strength: 10,
        indicatorText: translate.lowStrength,
        message: translate.allSpaces
      };
    }
    value = value.replace(/^\\s+|\\s+\$/g, '');
JS;

  // Print out each constraint's javascript password strength evaluation.
  foreach ($policy['constraints'] as $key => $value) {
    $s .= _password_policy_constraint_js($key, $value, $account);

    // Constraints' error messages are used in javascript.
    $translate['constraint_' . $key] = _password_policy_constraint_error($key, $value);
  }
  $s .= <<<JS

    if (msg.length > 0) {
      msg = translate.needsMoreVariation + '<ul><li>' + msg.join('</li><li>') + '</li></ul>';
    }
    else {
      msg = '';
    }
    if (trimmedSpaces) {
      msg = msg.concat(translate.trimmedSpaces);
    }
    var level = '';
    if (strength === 'high') {
      level = 100;
    }
    else {
      level = 10;
    }
    if (strength === 'high') {
      strength = translate.highStrength;
    }
    if (strength === 'medium') {
      strength = translate.mediumStrength;
    }
    if (strength === 'low') {
      strength = translate.lowStrength;
    }
    return {
      strength: level,
      indicatorText: strength,
      message: msg
    };
  }
JS;
  $options = array(
    'scope' => 'header',
    'type' => 'inline',
    'weight' => 10,
  );
  if (isset($render_array)) {
    $options['data'] = $s;
    $render_array['#attached']['js'][] = $options;
  }
  else {
    drupal_add_js($s, $options);
  }
  $data = array(
    // Override some core 'password' settings.
    // Drupal by default rates passwords in terms of strength. However, a
    // password that meets Password Policy constraints is not necessarily a
    // strong password. So we rate the password in terms of "quality". A
    // password is "bad" if it does not meet constraints, "good" if it does.
    'password' => array(
      'strengthTitle' => t('Password quality:'),
      'lowStrength' => t('Bad'),
      'mediumStrength' => t('Good'),
      'highStrength' => t('Good'),
    ),
    // Add new settings for this module.
    'passwordPolicy' => array_merge(array(
      'trimmedSpaces' => t('The password has spaces at the beginning or end which are ignored.'),
      'allSpaces' => t('The password is all spaces and will not be saved.'),
      'needsMoreVariation' => t('The password does not include enough variation to be secure.'),
    ), $translate),
  );
  if (isset($render_array)) {
    $options = array(
      'data' => $data,
      'type' => 'setting',
    );
    $render_array['#attached']['js'][] = $options;
  }
  else {
    drupal_add_js($data, 'setting');
  }
}