You are here

function password_policy_password_change_settings_submit in Password Policy 7

Same name and namespace in other branches
  1. 6 password_policy.admin.inc \password_policy_password_change_settings_submit()

Submit hook for forced password change form.

File

./password_policy.admin.inc, line 675
Admin page callback file for the Password Policy module.

Code

function password_policy_password_change_settings_submit($form, &$form_state) {
  global $user;
  $selected_roles = array();
  variable_set('password_policy_new_login_change', $form_state['values']['password_policy_new_login_change']);
  if ($form_state['values']['password_policy_new_login_change'] == 1) {
    watchdog('password_policy', 'New user accounts must change password on new login enabled by !admin.', array(
      '!admin' => $user->name,
    ), WATCHDOG_NOTICE);
  }
  $form_state['values']['password_policy_new_login_change'] ? drupal_set_message(t('New users will be required to change their password on first-time login.')) : drupal_set_message(t('New users will not be required to change their password on first-time login.'));
  foreach ($form_state['values']['password_policy_force_change_roles'] as $role) {

    // Skip over null values returned by unselected roles.
    if ($role == 0) {
      continue;
    }
    $uids = array();

    // Special handling for authenticated users role, since this role makes no
    // entries in the users_roles table.
    if ($role == 2) {

      // No point in flagging anonymous since they can't log in anyway.
      $db_uids = db_query('SELECT uid FROM {users} WHERE uid <> 0')
        ->fetchCol();
    }
    else {
      $db_uids = db_query('SELECT uid FROM {users_roles} WHERE rid = :rid', array(
        'rid' => $role,
      ))
        ->fetchCol();
    }
    foreach ($db_uids as $uid) {
      if ($uid == 1 && variable_get('password_policy_admin', 1) || $uid > 1) {
        $uids[] = $uid;
      }
    }
    if (!empty($uids)) {

      // If there are a large number of users to process, PHP memory limit may
      // be reached, so process the users in batches.
      $chunks = array_chunk($uids, 50000);
      foreach ($chunks as $chunk) {
        db_update('password_policy_force_change')
          ->fields(array(
          'force_change' => 1,
        ))
          ->condition('uid', $chunk, 'IN')
          ->execute();
      }
    }
    $selected_roles[] = $role;
  }
  if (count($selected_roles)) {
    $roles = user_roles(TRUE);
    $list = array();
    foreach ($selected_roles as $sr) {
      $list[] = $roles[$sr];
    }
    $list = implode(', ', $list);
    drupal_set_message(t('Users in the following roles will be required to immediately change their password: %list', array(
      '%list' => $list,
    )), 'status');
    watchdog('password_policy', '!admin has flagged users in the following roles to immediately change their password: %list', array(
      '%list' => $list,
      '!admin' => $user->name,
    ), WATCHDOG_NOTICE);
  }
  else {
    drupal_set_message(t('No roles were selected.'));
  }
}