-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathDemoteUserCommand.php
89 lines (72 loc) · 3.17 KB
/
DemoteUserCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\UserBundle\Command;
use Sonata\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
#[AsCommand(name: 'sonata:user:demote', description: 'Demotes a user by removing a role')]
final class DemoteUserCommand extends Command
{
public function __construct(private UserManagerInterface $userManager)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setDefinition([
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('role', InputArgument::OPTIONAL, 'The role'),
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'),
])
->setHelp(
<<<'EOT'
The <info>%command.full_name%</info> command demotes a user by removing a role
<info>php %command.full_name% matthieu ROLE_CUSTOM</info>
<info>php %command.full_name% --super-admin matthieu</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$username = $input->getArgument('username');
$role = $input->getArgument('role');
$superAdmin = (true === $input->getOption('super-admin'));
if (null !== $role && $superAdmin) {
throw new \InvalidArgumentException('You can pass either the role or the --super-admin option (but not both simultaneously).');
}
if (null === $role && !$superAdmin) {
throw new \InvalidArgumentException('Not enough arguments.');
}
$user = $this->userManager->findUserByUsername($username);
if (null === $user) {
throw new \InvalidArgumentException(\sprintf('User identified by "%s" username does not exist.', $username));
}
if ($superAdmin) {
$user->setSuperAdmin(false);
$output->writeln(\sprintf('User "%s" has been demoted as a simple user. This change will not apply until the user logs out and back in again.', $username));
} elseif ($user->hasRole($role)) {
$user->removeRole($role);
$output->writeln(\sprintf('Role "%s" has been removed from user "%s". This change will not apply until the user logs out and back in again.', $role, $username));
} else {
$output->writeln(\sprintf('User "%s" didn\'t have "%s" role.', $username, $role));
}
$this->userManager->save($user);
return 0;
}
}