You are here

function mass_pwreset_batch_process in Mass Password Reset 8

Same name and namespace in other branches
  1. 7 mass_pwreset.batch.inc \mass_pwreset_batch_process()
  2. 2.x mass_pwreset.batch.inc \mass_pwreset_batch_process()

Batch process callback.

Loads user object, resets password, and optionally sends notify email.

1 string reference to 'mass_pwreset_batch_process'
mass_pwreset_multiple_reset in ./mass_pwreset.module
Establish batch operation for resetting passwords.

File

./mass_pwreset.batch.inc, line 16
Contains batch process functions for resetting passwords.

Code

function mass_pwreset_batch_process($data, &$context) {

  // Initiate count values for batch iteration.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_row'] = 0;
    $context['sandbox']['max'] = count($data['uids']);
  }

  // Establish index for loading user objects.
  $i = $context['sandbox']['current_row'];

  // Reset user password.
  if (isset($data['uids'][$i])) {
    $user = User::load($data['uids'][$i]);
    $user
      ->setPassword(user_password(40));
    $user
      ->save();

    // Update status message.
    $context['message'] = t('Processing uid: @uid', [
      '@uid' => $user
        ->id(),
    ]);
    $context['results']['passwords_reset'][] = t('User password reset');

    // Send password reset email and log event.
    if ($data['notify_active_users'] == 1) {

      // Set batch message.
      $email_message = t('Password reset email sent to uid: @uid', [
        '@uid' => $user
          ->id(),
      ]);
      $context['message'] = $email_message;

      // Notify active users.
      if (!$user
        ->isBlocked()) {
        _user_mail_notify('password_reset', $user);
        \Drupal::logger('mass_pwreset')
          ->notice($email_message);
      }

      // Notify blocked users if applicable.
      if ($data['notify_blocked_users'] == 1 && $user
        ->isBlocked()) {
        _user_mail_notify('password_reset', $user);
        \Drupal::logger('mass_pwreset')
          ->notice($email_message);
      }
    }

    // Log password reset for this uid.
    $message = t('Password reset for uid: @uid', [
      '@uid' => $user
        ->id(),
    ]);
    \Drupal::logger('mass_pwreset')
      ->notice($message);
  }

  // Update batch progress information.
  $context['sandbox']['progress'] += 1;
  $context['sandbox']['current_row'] += 1;

  // Inform the batch engine of estimated completion level reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }

  // Save roles in results for finished logging.
  $context['results']['roles'] = $data['roles'];
}