You are here

mass_password_change.module in Mass Password Change 7

Mass Password Change

File

mass_password_change.module
View source
<?php

/**
 * @file
 * Mass Password Change
 */

/**
 * Implements hook_user_operations().
 */
function mass_password_change_user_operations($form = array(), $form_state = array()) {
  $operations = array(
    'mass_password_reset' => array(
      'label' => t('Mass password reset'),
      'callback' => 'mass_password_change_reset',
    ),
    'mass_password_change' => array(
      'label' => t('Mass password change'),
      'callback' => 'mass_password_change_change',
    ),
  );
  if (!empty($form_state['submitted']) && $form_state['values']['operation'] == 'mass_password_change') {
    $password = $form_state['values']['mass_password_change_password'];
    $operations['mass_password_change']['callback arguments'] = array(
      $password,
    );
  }
  return $operations;
}

/**
 * Password reset callback.
 */
function mass_password_change_reset($accounts, $message = TRUE) {
  global $language;

  // Prevent uid=1 user.
  if (array_key_exists(1, $accounts)) {
    unset($accounts[1]);
  }
  foreach ($accounts as $uid) {
    $account = user_load($uid);
    _user_mail_notify('password_reset', $account, $language);
  }
  if ($message) {
    watchdog('Mass Password Change', 'Mass password reset complete.');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mass_password_change_form_user_admin_account_alter(&$form, &$form_state, $form_id) {
  $form['#attached'] = array(
    'css' => array(
      drupal_get_path('module', 'mass_password_change') . '/css/mass_password_change.css',
    ),
  );
  $form['options']['container'] = array(
    '#type' => 'container',
    '#tree' => FALSE,
    '#states' => array(
      'visible' => array(
        ':input[name="operation"]' => array(
          'value' => 'mass_password_change',
        ),
      ),
    ),
  );
  $form['options']['container']['mass_password_change_password'] = array(
    '#type' => 'password_confirm',
    '#required' => FALSE,
    '#size' => 25,
  );
  $form['#validate'][] = 'mass_password_change_validate_password';
}

/**
 * Password validation.
 */
function mass_password_change_validate_password(&$form, &$form_state) {
  if ($form_state['values']['operation'] == 'mass_password_change') {
    if (empty($form_state['values']['mass_password_change_password'])) {
      form_set_error('mass_password_change_password', t('Password is required.'));
    }
  }
}

/**
 * Password change callback.
 */
function mass_password_change_change($accounts, $password) {

  // Prevent uid=1 user.
  if (array_key_exists(1, $accounts)) {
    unset($accounts[1]);
  }
  foreach ($accounts as $uid) {
    $account = user_load($uid);
    user_save($account, array(
      'pass' => $password,
    ));
  }
  watchdog('Mass Password Change', 'Mass password change complete.');
}

/**
 * Implements hook_action_info().
 */
function mass_password_change_action_info() {
  return array(
    'mass_password_reset_action' => array(
      'type' => 'user',
      'label' => t('Mass password reset'),
      'configurable' => FALSE,
    ),
    'mass_password_change_action' => array(
      'type' => 'user',
      'label' => t('Mass password change'),
      'configurable' => TRUE,
    ),
  );
}

/**
 * Password reset action.
 */
function mass_password_reset_action($account, $context = array()) {
  mass_password_change_reset(array(
    $account->uid,
  ), FALSE);
}

/**
 * Password change form.
 */
function mass_password_change_action_form($context) {
  $form = array();
  $form['mass_password_change_password'] = array(
    '#type' => 'password_confirm',
    '#required' => TRUE,
    '#size' => 25,
  );
  return $form;
}

/**
 * Password change form - submit.
 */
function mass_password_change_action_submit($form, $form_state) {
  return array(
    'password' => $form_state['values']['mass_password_change_password'],
  );
}

/**
 * Password change action.
 */
function mass_password_change_action($account, $context = array()) {
  user_save($account, array(
    'pass' => $context['password'],
  ));
}