fsi-open /
admin-security-bundle
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * (c) FSi sp. z o.o. <[email protected]> |
||||
| 5 | * |
||||
| 6 | * For the full copyright and license information, please view the LICENSE |
||||
| 7 | * file that was distributed with this source code. |
||||
| 8 | */ |
||||
| 9 | |||||
| 10 | declare(strict_types=1); |
||||
| 11 | |||||
| 12 | namespace FSi\Bundle\AdminSecurityBundle\Command; |
||||
| 13 | |||||
| 14 | use Exception; |
||||
| 15 | use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
||||
| 16 | use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
||||
| 17 | use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface; |
||||
| 18 | use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface; |
||||
| 19 | use InvalidArgumentException; |
||||
| 20 | use Symfony\Component\Console\Command\Command; |
||||
| 21 | use Symfony\Component\Console\Helper\QuestionHelper; |
||||
| 22 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 23 | use Symfony\Component\Console\Input\InputInterface; |
||||
| 24 | use Symfony\Component\Console\Output\OutputInterface; |
||||
| 25 | use Symfony\Component\Console\Question\Question; |
||||
| 26 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||||
| 27 | |||||
| 28 | class DemoteUserCommand extends Command |
||||
| 29 | { |
||||
| 30 | /** |
||||
| 31 | * @var UserRepositoryInterface |
||||
| 32 | */ |
||||
| 33 | private $userRepository; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var EventDispatcherInterface |
||||
| 37 | */ |
||||
| 38 | private $eventDispatcher; |
||||
| 39 | |||||
| 40 | public function __construct( |
||||
| 41 | UserRepositoryInterface $userRepository, |
||||
| 42 | EventDispatcherInterface $eventDispatcher, |
||||
| 43 | $name = null |
||||
| 44 | ) { |
||||
| 45 | parent::__construct($name); |
||||
| 46 | |||||
| 47 | $this->userRepository = $userRepository; |
||||
| 48 | $this->eventDispatcher = $eventDispatcher; |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | protected function configure(): void |
||||
| 52 | { |
||||
| 53 | $this |
||||
| 54 | ->setName('fsi:user:demote') |
||||
| 55 | ->setDescription('Demote a user.') |
||||
| 56 | ->setDefinition([ |
||||
| 57 | new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
||||
| 58 | new InputArgument('role', InputArgument::REQUIRED, 'The role'), |
||||
| 59 | ]) |
||||
| 60 | ->setHelp(<<<EOT |
||||
| 61 | The <info>fsi:user:demote</info> command demotes the user |
||||
| 62 | |||||
| 63 | <info>php app/console fsi:user:demote [email protected] ROLE_ADMIN</info> |
||||
| 64 | |||||
| 65 | EOT |
||||
| 66 | ); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | protected function execute(InputInterface $input, OutputInterface $output): void |
||||
| 70 | { |
||||
| 71 | $email = $input->getArgument('email'); |
||||
| 72 | $role = $input->getArgument('role'); |
||||
| 73 | |||||
| 74 | $user = $this->userRepository->findUserByEmail($email); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 75 | if (false === $user instanceof UserInterface) { |
||||
| 76 | throw new InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email)); |
||||
|
0 ignored issues
–
show
It seems like
$email can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 77 | } |
||||
| 78 | |||||
| 79 | $user->removeRole($role); |
||||
|
0 ignored issues
–
show
It seems like
$role can also be of type null and string[]; however, parameter $role of FSi\Bundle\AdminSecurity...Interface::removeRole() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 80 | $this->eventDispatcher->dispatch(AdminSecurityEvents::DEMOTE_USER, new UserEvent($user)); |
||||
|
0 ignored issues
–
show
FSi\Bundle\AdminSecurity...rityEvents::DEMOTE_USER of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 81 | |||||
| 82 | $output->writeln(sprintf('User <comment>%s</comment> has been demoted', $email)); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | protected function interact(InputInterface $input, OutputInterface $output): void |
||||
| 86 | { |
||||
| 87 | if (!$input->getArgument('email')) { |
||||
| 88 | $this->askEmail($input, $output); |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | if (!$input->getArgument('role')) { |
||||
| 92 | $this->askRole($input, $output); |
||||
| 93 | } |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | private function askEmail(InputInterface $input, OutputInterface $output): void |
||||
| 97 | { |
||||
| 98 | $question = new Question('Please choose an email:'); |
||||
| 99 | $question->setValidator(function (string $email): string { |
||||
| 100 | if (empty($email)) { |
||||
| 101 | throw new Exception('Email can not be empty'); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | return $email; |
||||
| 105 | }); |
||||
| 106 | |||||
| 107 | $email = $this->getQuestionHelper()->ask($input, $output, $question); |
||||
| 108 | $input->setArgument('email', $email); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | private function askRole(InputInterface $input, OutputInterface $output): void |
||||
| 112 | { |
||||
| 113 | $question = new Question('Please choose a role:'); |
||||
| 114 | $question->setValidator(function (string $password): string { |
||||
| 115 | if (empty($password)) { |
||||
| 116 | throw new Exception('Role can not be empty'); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | return $password; |
||||
| 120 | }); |
||||
| 121 | |||||
| 122 | $role = $this->getQuestionHelper()->ask($input, $output, $question); |
||||
| 123 | $input->setArgument('role', $role); |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | private function getQuestionHelper(): QuestionHelper |
||||
| 127 | { |
||||
| 128 | return $this->getHelper('question'); |
||||
| 129 | } |
||||
| 130 | } |
||||
| 131 |