genpass.module in Generate Password 7.2
Same filename and directory in other branches
Genpass module: automatically sets strong passwords.
File
genpass.moduleView source
<?php
/**
* @file
* Genpass module: automatically sets strong passwords.
*/
/**
* Implements hook_form_FORM_ID_alter().
*/
function genpass_form_user_register_form_alter(&$form, &$form_state) {
// This form structure only exists for admin creation.
if (isset($form['account']['pass'])) {
$form['account']['pass']['#access'] = FALSE;
$form['account']['pass']['#type'] = 'value';
$form['account']['genpass_message'] = array(
'#type' => 'markup',
'#markup' => t("<h3>Password</h3><p>A strong password will be generated automatically. The user should use the password reset function to set their password.</p>"),
);
// Add validation function, where password is actually set.
$form['#validate'][] = 'genpass_admin_create_validate';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function genpass_form_user_profile_form_alter(&$form, &$form_state) {
// Only change the edit form for admins modifying another user.
if ($form['#user']->uid != $GLOBALS['user']->uid && isset($form['account']['pass'])) {
// When editing, give admins a checkbox to change the password.
$form['account']['pass']['#access'] = FALSE;
$form['account']['genpass_generate'] = array(
'#type' => 'checkbox',
'#title' => t('Generate a new strong password. Their current password will stop working.'),
'#prefix' => '<h3>' . t('Password') . '</h3>',
);
$form['#validate'][] = 'genpass_admin_edit_validate';
}
}
/**
* Form validate function to optionally set a password value.
*/
function genpass_admin_edit_validate($form, &$form_state) {
if (!empty($form_state['values']['genpass_generate'])) {
$password = genpass_genpass();
// If the site wants to show the password, show it.
if (variable_get('genpass_display_password', FALSE)) {
drupal_set_message(t('Automatically generated a password %password for this user.', array(
'%password' => $password,
)));
}
form_set_value($form['account']['pass'], $password, $form_state);
}
return $form;
}
/**
* Form validate function to set a password value.
*/
function genpass_admin_create_validate($form, &$form_state) {
$password = genpass_genpass();
// If the site wants to show the password, show it.
if (variable_get('genpass_display_password', FALSE)) {
drupal_set_message(t('The automatically generated password is: %password.', array(
'%password' => $password,
)));
}
form_set_value($form['account']['pass'], $password, $form_state);
return $form;
}
/**
* Generates a pretty, pretty, pretty good password.
*
* @param int $length
* Length of the password. This is in addition to the base of 4 digits.
*
* @return string
* A password.
*/
function genpass_genpass($length = 15) {
// This array contains the list of allowable characters for the
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true
// of 'I', 1, and 'l'.
$character_sets = array(
'lower_letters' => 'abcdefghijkmnopqrstuvwxyz',
'upper_letters' => 'ABCDEFGHJKLMNPQRSTUVWXYZ',
'digits' => '23456789',
'special' => '@#$%^&()=/|[]{};<>/',
);
// Always include at least 1 character of each class to ensure generated
// passwords meet simplistic password strength rules.
$pass = '';
foreach ($character_sets as $character_set) {
$pass .= _genpass_genpass_substring($character_set, 1);
}
// Now add length to get entropy.
$pass .= _genpass_genpass_substring(implode('', $character_sets), $length);
return $pass;
}
/**
* Picks random characters from a given string.
*
* @see user_password().
*
* @param $allowable_characters
* A set of characters from which to choose.
* @param $length
* The desired length of the returned random string.
*
* @return string
* The random string.
*/
function _genpass_genpass_substring($allowable_characters, $length) {
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
// Declare the password as a blank string.
$pass = '';
// Loop the number of times specified by $length.
for ($i = 0; $i < $length; $i++) {
do {
// Find a secure random number within the range needed.
$index = ord(drupal_random_bytes(1));
} while ($index > $len);
// Each iteration, pick a random character from the
// allowable string and append it to the password:
$pass .= $allowable_characters[$index];
}
return $pass;
}
Functions
Name | Description |
---|---|
genpass_admin_create_validate | Form validate function to set a password value. |
genpass_admin_edit_validate | Form validate function to optionally set a password value. |
genpass_form_user_profile_form_alter | Implements hook_form_FORM_ID_alter(). |
genpass_form_user_register_form_alter | Implements hook_form_FORM_ID_alter(). |
genpass_genpass | Generates a pretty, pretty, pretty good password. |
_genpass_genpass_substring | Picks random characters from a given string. |