class UserManagementService in Auto Purge Users 8.3
Same name and namespace in other branches
- 8.2 src/Services/UserManagementService.php \Drupal\purge_users\Services\UserManagementService
Class that holds the purging logic.
@package Drupal\purge_users\Services
Hierarchy
- class \Drupal\purge_users\Services\UserManagementService implements UserManagementServiceInterface
Expanded class hierarchy of UserManagementService
1 string reference to 'UserManagementService'
1 service uses UserManagementService
File
- src/
Services/ UserManagementService.php, line 18
Namespace
Drupal\purge_users\ServicesView source
class UserManagementService implements UserManagementServiceInterface {
/**
* Current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Logger channel service.
*
* @var \Drupal\Core\Logger\LoggerChannelFactory
*/
protected $loggerFactory;
/**
* UserManagementService constructor.
*
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* Current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
* @param \Drupal\Core\Logger\LoggerChannelFactory $logger_factory
* The logger factory.
*/
public function __construct(AccountProxyInterface $currentUser, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, MessengerInterface $messenger, LoggerChannelFactory $logger_factory) {
$this->currentUser = $currentUser;
$this->config = $config_factory;
$this->moduleHandler = $module_handler;
$this->messenger = $messenger;
$this->loggerFactory = $logger_factory;
}
/**
* {@inheritdoc}
*/
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());
}
}
/**
* {@inheritdoc}
*/
public function notifyUser(UserInterface $user) : void {
$notifier = $this->loggerFactory
->get('notification_users');
$config = $this->config
->get('purge_users.settings');
$send_email_user_before_notification = $config
->get('send_email_user_before_notification');
$send_notification = $config
->get('send_email_notification');
$user_ids = purge_users_get_user_ids('purge_users');
/*
* Checks if notification before deletion is enabled,
* AND if send email notification is enabled AND
* the user was purged OR the current user
* is not in the users purged we are not sending
* an email to avoid spamming.
*/
if ($send_email_user_before_notification == 1 && ($send_notification !== 1 && in_array($user
->id(), $user_ids)) || !in_array($user
->id(), $user_ids)) {
purge_users_send_notification_email($user, 'notification_users');
$this->messenger
->addStatus(t('%name has been notified.', [
'%name' => $user
->getDisplayName(),
]));
$notifier
->notice('Notified user: %name %email.', [
'%name' => $user
->getAccountName(),
'%email' => '<' . $user
->getEmail() . '>',
]);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UserManagementService:: |
protected | property | The config factory. | |
UserManagementService:: |
protected | property | Current user. | |
UserManagementService:: |
protected | property | Logger channel service. | |
UserManagementService:: |
protected | property | The messenger. | |
UserManagementService:: |
protected | property | The module handler. | |
UserManagementService:: |
public | function |
Sends a user notification before cancel and anonymize it. Overrides UserManagementServiceInterface:: |
|
UserManagementService:: |
public | function |
Cancel a user and anonymize it. Overrides UserManagementServiceInterface:: |
|
UserManagementService:: |
public | function | UserManagementService constructor. |