AzuraCast/src/Console/Command/SetAdministratorCommand.php

52 lines
1.4 KiB
PHP

<?php
namespace App\Console\Command;
use App\Acl;
use App\Entity;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SetAdministratorCommand extends CommandAbstract
{
public function __invoke(
SymfonyStyle $io,
EntityManagerInterface $em,
Entity\Repository\RolePermissionRepository $perms_repo,
string $email
) {
$io->title('Set Administrator');
$user = $em->getRepository(Entity\User::class)
->findOneBy(['email' => $email]);
if ($user instanceof Entity\User) {
$admin_role = $em->getRepository(Entity\Role::class)
->find(Entity\Role::SUPER_ADMINISTRATOR_ROLE_ID);
$perms_repo->setActionsForRole($admin_role, [
'actions_global' => [
Acl::GLOBAL_ALL,
],
]);
$user_roles = $user->getRoles();
if (!$user_roles->contains($admin_role)) {
$user_roles->add($admin_role);
}
$em->persist($user);
$em->flush();
$io->text(__('The account associated with e-mail address "%s" has been set as an administrator',
$user->getEmail()));
$io->newLine();
return 0;
}
$io->error(__('Account not found.'));
$io->newLine();
return 1;
}
}