public function UserManagementService::purgeUser in Auto Purge Users 8.3
Same name and namespace in other branches
- 8.2 src/Services/UserManagementService.php \Drupal\purge_users\Services\UserManagementService::purgeUser()
Cancel a user and anonymize it.
Parameters
\Drupal\user\UserInterface $user: The user to block.
string $method: The cancel method to use.
Overrides UserManagementServiceInterface::purgeUser
File
- src/
Services/ UserManagementService.php, line 80
Class
- UserManagementService
- Class that holds the purging logic.
Namespace
Drupal\purge_users\ServicesCode
public function purgeUser(UserInterface $user, string $method) {
$logger = $this->loggerFactory
->get('purge_users');
$edit = [];
// When the 'user_cancel_delete' method is used, user_delete() is called,
// which invokes hook_ENTITY_TYPE_predelete() and hook_ENTITY_TYPE_delete()
// for the user entity. Modules should use those hooks to respond to the
// account deletion.
if ($method != 'user_cancel_delete') {
// Allow modules to add further sets to this batch.
$this->moduleHandler
->invokeAll('user_cancel', [
$edit,
$user,
$method,
]);
}
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'])) {
_user_mail_notify('status_blocked', $user);
}
$user
->block();
$user
->save();
$this->messenger
->addStatus(t('%name has been disabled.', [
'%name' => $user
->getDisplayName(),
]));
$logger
->notice('Blocked user: %name %email.', [
'%name' => $user
->getAccountName(),
'%email' => '<' . $user
->getEmail() . '>',
]);
break;
case 'user_cancel_reassign':
case 'user_cancel_delete':
// Send account canceled notification if option was checked.
if (!empty($edit['user_cancel_notify'])) {
_user_mail_notify('status_canceled', $user);
}
$user
->delete();
$this->messenger
->addStatus(t('%name has been deleted.', [
'%name' => $user
->getDisplayName(),
]));
$logger
->notice('Deleted user: %name %email.', [
'%name' => $user
->getAccountName(),
'%email' => '<' . $user
->getEmail() . '>',
]);
break;
}
// Send a notification email.
$config = $this->config
->get('purge_users.settings');
$send_notification = $config
->get('send_email_notification');
if ($send_notification == 1) {
purge_users_send_notification_email($user);
}
// 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 _user_cancel_session_regenerate().
if ($user
->id() == $this->currentUser
->id()) {
$this->currentUser
->setAccount(new AnonymousUserSession());
}
}