past_passwords.inc in Password Policy 7.2
File
plugins/constraint/past_passwords.inc
View source
<?php
$plugin = array(
'admin form callback' => 'password_policy_past_passwords_count_admin_form',
'constraint callback' => 'password_policy_past_passwords_count_constraint',
'message' => t('Password cannot match @past_passwords past passwords.'),
'prime value' => 'past_passwords',
'config' => array(
'past_passwords' => NULL,
),
);
function password_policy_past_passwords_count_admin_form($form, &$form_state, $constraint) {
$sub_form['past_passwords_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Past passwords'),
);
$sub_form['past_passwords_fieldset']['past_passwords'] = array(
'#type' => 'textfield',
'#title' => t('Check against previous passwords'),
'#default_value' => $constraint->config['past_passwords'],
'#description' => t('Password cannot match this many previous passwords.'),
);
return $sub_form;
}
function password_policy_past_passwords_count_constraint($password, $account, $constraint) {
global $user;
if (!$account) {
$account = $user;
}
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
$count = $constraint->config['past_passwords'];
$match = FALSE;
if (!isset($account->password_history)) {
password_policy_user_load(array(
$account->uid => $account,
));
}
for ($i = 0; $i < $count; $i++) {
if (isset($account->password_history[$i])) {
$match = $match || user_check_password($password, $account->password_history[$i]);
}
else {
break;
}
}
return !$match;
}