View source
<?php
namespace Drupal\purge_users\Services;
use Drupal\user\UserInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Logger\LoggerChannelFactory;
class UserManagementService implements UserManagementServiceInterface {
protected $currentUser;
protected $config;
protected $moduleHandler;
protected $messenger;
protected $loggerFactory;
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;
}
public function purgeUser(UserInterface $user, string $method) {
$logger = $this->loggerFactory
->get('purge_users');
$edit = [];
if ($method != 'user_cancel_delete') {
$this->moduleHandler
->invokeAll('user_cancel', [
$edit,
$user,
$method,
]);
}
switch ($method) {
case 'user_cancel_block':
case 'user_cancel_block_unpublish':
default:
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':
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;
}
$config = $this->config
->get('purge_users.settings');
$send_notification = $config
->get('send_email_notification');
if ($send_notification == 1) {
purge_users_send_notification_email($user);
}
if ($user
->id() == $this->currentUser
->id()) {
$this->currentUser
->setAccount(new AnonymousUserSession());
}
}
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');
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() . '>',
]);
}
}
}