QaAccountsCreateDelete.php in QA Accounts 8
File
src/QaAccountsCreateDelete.php
View source
<?php
namespace Drupal\qa_accounts;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class QaAccountsCreateDelete implements QaAccountsCreateDeleteInterface {
protected $entityTypeManager;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger_factory
->get('qa_accounts');
}
public function createQaAccounts() {
$roles = $this->entityTypeManager
->getStorage('user_role')
->loadMultiple();
foreach ($roles as $role_name => $role) {
if ($role_name === 'anonymous') {
continue;
}
$this
->createQaAccountForRole($role_name);
}
}
public function createQaAccountForRole($role_name) {
$username = 'qa_' . $role_name;
$user_storage = $this->entityTypeManager
->getStorage('user');
$uids = $user_storage
->getQuery()
->condition('name', $username)
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($uids) {
$this->logger
->notice('User @name already exists.', [
'@name' => $username,
]);
}
else {
$user = $user_storage
->create();
$user
->enforceIsNew();
$user
->setUsername($username);
$user
->setEmail($username . '@example.com');
$user
->setPassword($username);
if ($role_name !== 'authenticated') {
$user
->addRole($role_name);
}
$user
->activate();
$user
->save();
$this->logger
->notice('Created user @name.', [
'@name' => $username,
]);
}
}
public function deleteQaAccounts() {
$roles = $this->entityTypeManager
->getStorage('user_role')
->loadMultiple();
foreach ($roles as $role_name => $role) {
if ($role_name === 'anonymous') {
continue;
}
$this
->deleteQaAccountForRole($role_name);
}
}
public function deleteQaAccountForRole($role_name) {
$username = 'qa_' . $role_name;
$users = $this->entityTypeManager
->getStorage('user')
->loadByProperties([
'name' => $username,
]);
$user = $users ? reset($users) : NULL;
if ($user) {
$user
->delete();
$this->logger
->notice('Deleted user @name.', [
'@name' => $username,
]);
}
else {
$this->logger
->notice('No such user @name.', [
'@name' => $username,
]);
}
}
}