function AccountCancellation::cancelAccount

Same name and namespace in other branches
  1. main core/modules/user/src/AccountCancellation.php \Drupal\user\AccountCancellation::cancelAccount()

Implements callback_batch_operation().

Last step for cancelling a user account.

Since batch and session API require a valid user account, the actual cancellation of a user account needs to happen last.

@internal

Parameters

array $edit: An array of submitted form values.

\Drupal\user\UserInterface $account: The user account to cancel.

string $method: The account cancellation method to use.

See also

AccountCancellation::cancel()

File

core/modules/user/src/AccountCancellation.php, line 118

Class

AccountCancellation
User account cancellation service.

Namespace

Drupal\user

Code

public function cancelAccount(array $edit, UserInterface $account, string $method) : void {
  switch ($method) {
    case 'user_cancel_block':
    case 'user_cancel_block_unpublish':
    default:
      // Send account blocked notification if option was checked.
      if (!empty($edit['user_cancel_notify'])) {
        $this->notificationHandler
          ->sendStatusBlocked($account);
      }
      $account->block();
      $account->save();
      $this->messenger
        ->addStatus($this->t('Account %name has been disabled.', [
        '%name' => $account->getDisplayName(),
      ]));
      $this->logger
        ->notice('Blocked user: %name %email.', [
        '%name' => $account->getAccountName(),
        '%email' => '<' . $account->getEmail() . '>',
      ]);
      break;

    case 'user_cancel_reassign':
    case 'user_cancel_delete':
      // Send account canceled notification if option was checked.
      if (!empty($edit['user_cancel_notify'])) {
        $this->notificationHandler
          ->sendStatusCancelled($account);
      }
      $account->delete();
      $this->messenger
        ->addStatus($this->t('Account %name has been deleted.', [
        '%name' => $account->getDisplayName(),
      ]));
      $this->logger
        ->notice('Deleted user: %name %email.', [
        '%name' => $account->getAccountName(),
        '%email' => '<' . $account->getEmail() . '>',
      ]);
      break;

  }
  // After cancelling account, ensure that user is logged out. We can't
  // destroy their session though, as we might have information in it, and we
  // can't regenerate it because batch API uses the session ID, we will
  // regenerate it in static::regenerateSession().
  if ($account->id() == $this->currentUser
    ->id()) {
    $this->currentUser
      ->setAccount(new AnonymousUserSession());
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.