2019-02-18 00:52:01 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-21 14:06:24 +00:00
|
|
|
namespace App\Console\Command\Users;
|
2019-02-18 00:52:01 +00:00
|
|
|
|
|
|
|
use App\Acl;
|
2020-09-21 14:06:24 +00:00
|
|
|
use App\Console\Command\CommandAbstract;
|
2019-02-18 00:52:01 +00:00
|
|
|
use App\Entity;
|
2020-06-26 20:22:53 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-02-18 00:52:01 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
|
2019-09-10 23:10:57 +00:00
|
|
|
class SetAdministratorCommand extends CommandAbstract
|
2019-02-18 00:52:01 +00:00
|
|
|
{
|
2019-09-10 23:10:57 +00:00
|
|
|
public function __invoke(
|
|
|
|
SymfonyStyle $io,
|
2020-06-26 20:22:53 +00:00
|
|
|
EntityManagerInterface $em,
|
2019-09-29 07:50:24 +00:00
|
|
|
Entity\Repository\RolePermissionRepository $perms_repo,
|
2019-09-10 23:10:57 +00:00
|
|
|
string $email
|
2020-10-14 22:19:31 +00:00
|
|
|
): int {
|
2019-02-18 00:52:01 +00:00
|
|
|
$io->title('Set Administrator');
|
|
|
|
|
|
|
|
$user = $em->getRepository(Entity\User::class)
|
2019-09-10 23:10:57 +00:00
|
|
|
->findOneBy(['email' => $email]);
|
2019-02-18 00:52:01 +00:00
|
|
|
|
|
|
|
if ($user instanceof Entity\User) {
|
|
|
|
$admin_role = $em->getRepository(Entity\Role::class)
|
|
|
|
->find(Entity\Role::SUPER_ADMINISTRATOR_ROLE_ID);
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
if (null === $admin_role) {
|
|
|
|
$io->error('Administrator role not found.');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$perms_repo->setActionsForRole(
|
|
|
|
$admin_role,
|
|
|
|
[
|
|
|
|
'actions_global' => [
|
|
|
|
Acl::GLOBAL_ALL,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2019-02-18 00:52:01 +00:00
|
|
|
|
|
|
|
$user_roles = $user->getRoles();
|
|
|
|
|
|
|
|
if (!$user_roles->contains($admin_role)) {
|
|
|
|
$user_roles->add($admin_role);
|
|
|
|
}
|
|
|
|
|
|
|
|
$em->persist($user);
|
|
|
|
$em->flush();
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
$io->text(__(
|
|
|
|
'The account associated with e-mail address "%s" has been set as an administrator',
|
|
|
|
$user->getEmail()
|
|
|
|
));
|
2019-02-18 00:52:01 +00:00
|
|
|
$io->newLine();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$io->error(__('Account not found.'));
|
|
|
|
$io->newLine();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|