You are here

function password_policy_add_policy_js in Password Policy 6

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

Add password policy JS

Parameters

$policy: A policy array.

$uid: A user ID for which the policy is applied.

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 1013
The password policy module allows you to enforce a specific level of password complexity for the user passwords on the system.

Code

function password_policy_add_policy_js($policy, $uid) {

  // Print out the javascript which checks the strength of the password.
  // It overwrites the default core javascript function.
  drupal_add_js(drupal_get_path('module', 'password_policy') . '/password_policy.js', 'module');
  $s = "/**\n";
  $s .= " * Evaluate the strength of a user's password.\n";
  $s .= " *\n";
  $s .= " * Returns the estimated strength and the relevant output message.\n";
  $s .= " */\n";
  $s .= "Drupal.evaluatePasswordStrength = function(value) {\n";
  $s .= "  var strength = \"high\", msg = [], translate = Drupal.settings.password_policy;\n";
  $s .= "  var trimmedSpaces = /^\\s+|\\s+\$/.test(value);\n";
  $s .= "  if (/^\\s+\$/.test(value)) {\n";
  $s .= "    return { strength: \"low\", message: translate.allSpaces };\n";
  $s .= "  }\n";
  $s .= "  value = value.replace(/^\\s+|\\s+\$/g,'')\n";

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

    // Constraints' error messages are used in javascript.
    $translate['constraint_' . $key] = _password_policy_constraint_error($key, $value);
  }
  $s .= "  msg = msg.length > 0 ? translate.needsMoreVariation +\"<ul><li>\"+ msg.join(\"</li><li>\") +\"</li></ul>\" : \"\";\n";
  $s .= "  if (trimmedSpaces && strength != \"high\") {\n";
  $s .= "    msg = msg.concat(translate.trimmedSpaces);\n";
  $s .= "  }\n";
  $s .= "  return { strength: strength, message: msg };\n";
  $s .= "};\n";
  drupal_add_js($s, 'inline');
  drupal_add_js(array(
    'password_policy' => array_merge(array(
      'strengthTitle' => t('Password quality:'),
      'lowStrength' => t('Bad'),
      'mediumStrength' => t('Good'),
      'highStrength' => t('Good'),
      '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.'),
      'confirmSuccess' => t('Yes'),
      'confirmFailure' => t('No'),
      'confirmTitle' => t('Passwords match:'),
    ), $translate),
  ), 'setting');
}