View source
<?php
function force_password_change_permission() {
return array(
'Administer force password change' => array(
'title' => t('Force changing of passwords'),
'description' => t('Gives users the ability to force users to change their password.'),
),
);
}
function force_password_change_menu() {
$menu['admin/config/people/force_password_change'] = array(
'title' => 'Force password change',
'description' => t('Force users to change their password either immediately or after a period of time.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'force_password_change_settings',
),
'access arguments' => array(
'Administer force password change',
),
'file' => 'force_password_change.pages.inc',
);
$menu['admin/config/people/force_password_change/list/%'] = array(
'title' => 'dummy title. Does not need translation',
'page callback' => 'force_password_change_list',
'page arguments' => array(
5,
),
'access arguments' => array(
'Administer force password change',
),
'file' => 'force_password_change.pages.inc',
'type' => MENU_CALLBACK,
);
return $menu;
}
function force_password_change_init() {
global $user;
$change_password_url = preg_replace('/!uid/', $user->uid, variable_get('force_password_change_change_password_url', 'user/!uid/edit'));
if ($user->uid && current_path() != $change_password_url && current_path() != drupal_get_path_alias('user/logout')) {
$redirect = FALSE;
if (variable_get('force_password_change_login_or_init', 0)) {
$pending_users = variable_get('force_password_change_pending_login_users', array());
if (isset($pending_users[$user->uid])) {
$value = $pending_users[$user->uid]['value'];
$destination = array();
$redirect = TRUE;
}
}
else {
$pending_change = force_password_change_check();
if ($pending_change) {
$value = $pending_change;
$destination = array(
'query' => array(
'destination' => $_GET['q'],
),
);
$redirect = TRUE;
}
}
if ($redirect) {
if ($value == 1) {
drupal_set_message(t('An administrator has required that you change your password. Please change your password to proceed.'), 'error', FALSE);
}
else {
$time_period = force_password_change_get_text_date($value);
drupal_set_message(t('This site requires that you change your password every !time_period. Please change your password to proceed.', array(
'!time_period' => $time_period,
)));
}
drupal_goto($change_password_url, $destination);
}
}
}
function force_password_change_check() {
global $user;
if (isset($user->force_password_change) && $user->force_password_change) {
return 1;
}
elseif (variable_get('force_password_change_expire_password', FALSE)) {
$query = db_select('force_password_change_users', 'fpcu');
$alias = $query
->join('users', 'u', 'u.uid = fpcu.uid');
$query
->fields('fpcu', array(
'last_password_change',
))
->fields($alias, array(
'created',
))
->condition($alias . '.uid', $user->uid);
$user_data = $query
->execute()
->fetchObject();
$query = db_select('force_password_change_expiry', 'fpce');
$expiry = $query
->fields('fpce', array(
'expiry',
))
->condition('fpce.rid', array_keys($user->roles), 'IN')
->orderBy('fpce.weight')
->range(0, 1)
->addTag('force_password_change_expiry_check')
->execute()
->fetchField();
if ($expiry && ($user_data->last_password_change != '' && REQUEST_TIME - $expiry > $user_data->last_password_change) || $user_data->last_password_change == '' && REQUEST_TIME - $expiry > $user_data->created) {
$query = db_update('users')
->fields(array(
'force_password_change' => 1,
))
->condition('uid', $user->uid)
->execute();
return $expiry;
}
}
return FALSE;
}
function force_password_change_get_text_date($timestamp) {
$year = 60 * 60 * 24 * 365;
if ($timestamp % $year === 0) {
$time_period = $timestamp / $year;
$time_period = $time_period > 1 ? $time_period . ' ' . t('years') : t('year');
}
else {
$week = 60 * 60 * 24 * 7;
if ($timestamp % $week === 0) {
$time_period = $timestamp / $week;
$time_period = $time_period > 1 ? $time_period . ' ' . t('weeks') : t('week');
}
else {
$day = 60 * 60 * 24;
if ($timestamp % $day === 0) {
$time_period = $timestamp / $day;
$time_period = $time_period > 1 ? $time_period . ' ' . t('days') : t('day');
}
else {
$hour = 60 * 60;
if ($timestamp % $hour === 0) {
$time_period = $timestamp / $hour;
$time_period = $time_period > 1 ? $time_period . ' ' . t('hours') : t('hour');
}
}
}
}
return $time_period;
}
function force_password_change_user_login(&$edit, $account) {
if (variable_get('force_password_change_login_or_init', 0)) {
$pending_change = force_password_change_check();
if ($pending_change) {
$pending_users = variable_get('force_password_change_pending_login_users', array());
$pending_users[$account->uid] = array(
'destination' => current_path(),
'value' => $pending_change,
);
variable_set('force_password_change_pending_login_users', $pending_users);
}
}
}
function force_password_change_user_insert(&$edit, $account, $category) {
$query = db_insert('force_password_change_users')
->fields(array(
'uid' => $account->uid,
))
->execute();
if (variable_get('force_password_change_first_time_login_password_change', 0)) {
$query = db_update('users')
->fields(array(
'force_password_change' => 1,
))
->condition('uid', $account->uid)
->execute();
}
elseif (isset($edit['force_password_change']) && $edit['force_password_change']) {
$query = db_update('users')
->fields(array(
'force_password_change' => 1,
))
->condition('uid', $account->uid)
->execute();
$forced_uids = variable_get('force_password_change_first_time_uids', array());
$forced_uids[$account->uid] = $account->uid;
variable_set('force_password_change_first_time_uids', $forced_uids);
}
unset($edit['force_password_change']);
}
function force_password_change_user_update(&$edit, $account, $category) {
global $user;
if ($account->pass != $account->original->pass) {
$query = db_update('force_password_change_users')
->fields(array(
'last_password_change' => REQUEST_TIME,
))
->condition('uid', $account->uid)
->execute();
}
if ($account->force_password_change && $user->uid == $account->uid && isset($edit['pending_force_password_change']) && $edit['pending_force_password_change']) {
$query = db_update('users')
->fields(array(
'force_password_change' => 0,
))
->condition('uid', $account->uid)
->execute();
$forced_uids = variable_get('force_password_change_first_time_uids', array());
if (isset($forced_uids[$account->uid])) {
unset($forced_uids[$account->uid]);
variable_set('force_password_change_first_time_uids', $forced_uids);
}
$pending_users = variable_get('force_password_change_pending_login_users', array());
if (isset($pending_users[$account->uid])) {
$destination = $pending_users[$account->uid]['destination'];
unset($pending_users[$account->uid]);
variable_set('force_password_change_pending_login_users', $pending_users);
$_REQUEST['destination'] = $destination;
}
}
if (isset($edit['force_password_change']) && $edit['force_password_change']) {
$query = db_update('users')
->fields(array(
'force_password_change' => 1,
))
->condition('uid', $account->uid)
->execute();
$query = db_update('force_password_change_users')
->fields(array(
'last_force' => REQUEST_TIME,
))
->condition('uid', $account->uid)
->execute();
unset($edit['force_password_change']);
}
}
function force_password_change_user_delete($account) {
$query = db_delete('force_password_change_users')
->condition('uid', $account->uid)
->execute();
}
function force_password_change_validate_user($form, &$form_state) {
global $user;
if (isset($form['#user']->force_password_change) && $form['#user']->force_password_change && $form['#user']->uid == $user->uid) {
if ($form_state['input']['pass']['pass1'] == '') {
form_set_error('pass', t('You must choose a new password'));
}
}
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
if (user_check_password($form_state['input']['pass']['pass1'], $form['#user'])) {
form_set_error('pass', t('You cannot use your current password. Please choose a different password.'));
}
}
function force_password_change_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_admin_roles') {
$form['#theme'] = array(
'force_password_change_user_admin_roles_form',
);
$form['add']['#submit'][] = 'force_password_change_add_role';
}
elseif ($form_id == 'user_admin_role') {
if (variable_get('force_password_change_login_or_init', 0)) {
$description = t('Users who are not signed in will be required to change their password immediately upon login. Users who are currently signed in will be required to change their password upon their next page click, and after changing their password will be redirected back to the page they were attempting to access.');
}
else {
$description = t('Users will be required to change their password upon their next login.');
}
$form['force_password_change'] = array(
'#type' => 'checkbox',
'#title' => t('Force users in this role to change their password'),
'#description' => $description . '<br />' . t('Note: When you return to this page, this box will be unchecked. This is because this setting is a trigger, not a persistant state.'),
'#weight' => -1,
);
$form['name']['#weight'] = -2;
$form['actions']['delete']['#submit'][] = 'force_password_change_delete_role';
$form['#submit'][] = 'force_password_change_edit_role';
}
elseif ($form_id == 'user_profile_form') {
global $user;
if (user_access('Administer force password change', $user)) {
if (isset($form['account'])) {
$use_form =& $form['account'];
}
else {
$use_form =& $form;
}
$use_form['name']['#weight'] = -10;
$use_form['mail']['#weight'] = -9;
$use_form['password'] = array(
'#type' => 'fieldset',
'#title' => t('Password'),
'#weight' => -1,
);
$use_form['password']['pass'] = $use_form['pass'];
if (isset($use_form['pass'])) {
unset($use_form['pass']);
}
if (variable_get('force_password_change_login_or_init', 0)) {
$description = t('If this box is checked, the user will be forced to change their password. If the user is signed in, they will be forced to change their password on their next page load. If they are not signed in, they will be forced to change their password the next time they log in.');
}
else {
$description = t('If this box is checked, the user will be forced to change their password upon their next login.');
}
$use_form['password']['force_password_change'] = array(
'#type' => 'checkbox',
'#title' => t('Force this user to change their password'),
'#access' => user_access('Administer force password change'),
'#description' => $description . '<br />' . t('Note: This box will be unchecked each time the page is loaded, as it is a trigger, not a persistent state.'),
);
$force_password_data = db_query('SELECT last_password_change, last_force FROM {force_password_change_users} WHERE uid = :uid', array(
':uid' => $form['#user']->uid,
))
->fetchObject();
if ($force_password_data->last_force != '') {
$last_force = format_date($force_password_data->last_force, 'small');
}
elseif (variable_get('force_password_change_first_time_login_password_change', FALSE) && $form['#user']->created > variable_get('force_password_change_installation_date', 0)) {
$last_force = t('Their first login');
}
else {
$forced_uids = variable_get('force_password_change_first_login_change', array());
if (count($forced_uids) && isset($forced_uids[$form['#user']->uid])) {
$last_force = t('Their first login');
}
elseif ($force_password_data->last_password_change != '') {
$last_force = t('Their first login');
}
else {
$last_force = t('Never');
}
}
$variables = array(
'pending_change' => $form['#user']->force_password_change ? t('Yes') : t('No'),
'last_force' => $last_force,
'last_change' => $force_password_data->last_password_change != '' ? format_date($force_password_data->last_password_change, 'small') : t('Never'),
);
$use_form['password']['password_stats'] = array(
'#markup' => theme('force_password_change_stats', $variables),
);
}
$form['pending_force_password_change'] = array(
'#type' => 'value',
'#value' => $form['#user']->force_password_change,
);
$form['#validate'][] = 'force_password_change_validate_user';
}
elseif ($form_id == 'user_register_form' && $GLOBALS['user']->uid != 0) {
if (variable_get('force_password_change_first_login_change', 0)) {
if ($form['account']) {
$use_form =& $form['account'];
}
else {
$use_form =& $form;
}
$use_form['name']['#weight'] = -10;
$use_form['mail']['#weight'] = -9;
$use_form['password']['#weight'] = -8;
$use_form['password']['pass'] = $use_form['pass'];
unset($use_form['pass']);
$use_form['password']['force_password_change'] = array(
'#type' => 'checkbox',
'#title' => t('Force password change on first-time login'),
'#description' => t('If this box is checked, the user will be forced to change their password on their first login.'),
'#access' => user_access('Administer force password change'),
);
}
}
}
function force_password_change_theme() {
return array(
'force_password_change_user_admin_roles_form' => array(
'render element' => 'form',
'file' => 'force_password_change.pages.inc',
),
'force_password_change_settings' => array(
'render element' => 'form',
'file' => 'force_password_change.pages.inc',
),
'force_password_change_expiry' => array(
'render element' => 'form',
'file' => 'force_password_change.pages.inc',
),
'force_password_change_list' => array(
'arguments' => array(
'last_change' => NULL,
'pending_users_table' => NULL,
'non_pending_users_table' => NULL,
'force_password_change_form' => NULL,
'back_button' => NULL,
),
'file' => 'force_password_change.pages.inc',
),
'force_password_change_stats' => array(
'arguments' => array(
'variables' => array(
'pending_change' => NULL,
'last_force' => NULL,
'last_change' => NULL,
),
),
'file' => 'force_password_change.pages.inc',
),
);
}
function force_password_change_add_role($form, &$form_state) {
$rid = db_query('SELECT rid FROM {role} WHERE name = :name', array(
':name' => $form_state['values']['name'],
))
->fetchCol();
$query = db_insert('force_password_change_roles')
->fields(array(
'rid' => $rid[0],
))
->execute();
}
function force_password_change_delete_role($form, &$form_state) {
$query = db_delete('force_password_change_roles')
->condition('rid', $form_state['values']['rid'])
->execute();
}
function force_password_change_edit_role($form, &$form_state) {
if ($form_state['values']['force_password_change']) {
$db_uids = db_query('SELECT uid ' . 'FROM {users_roles} ' . 'WHERE rid = :rid', array(
':rid' => $form_state['values']['rid'],
));
$uids = array();
foreach ($db_uids as $uid) {
$uids[] = $uid->uid;
}
if (isset($uids[0])) {
force_password_change_force_users($uids);
}
$query = db_update('force_password_change_roles')
->fields(array(
'last_force' => REQUEST_TIME,
))
->condition('rid', $form_state['values']['rid'])
->execute();
if (variable_get('force_password_change_login_or_init', 0)) {
$description = t('Users in this role will be required to immediately change their password');
}
else {
$description = t('Users will be required to change their password upon their next login.');
}
drupal_set_message($description);
}
}
function force_password_change_force_users($uids = array()) {
$query = db_update('users')
->fields(array(
'force_password_change' => 1,
));
if (!empty($uids)) {
$query
->condition('uid', $uids, 'IN');
}
$query
->execute();
$query = db_update('force_password_change_users')
->fields(array(
'last_force' => REQUEST_TIME,
));
if (!empty($uids)) {
$query
->condition('uid', $uids, 'IN');
}
$query
->execute();
}