1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Command; |
6
|
|
|
|
7
|
|
|
use Gbere\SimpleAuth\Command\Exception\InvalidRolePatternException; |
8
|
|
|
use Gbere\SimpleAuth\Entity\Role; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Question\Question; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
final class GbRoleAddCommand extends AbstractCommand |
16
|
|
|
{ |
17
|
|
|
private const ROLE_ALLOWED_PATTERN = '/^ROLE_[A-Z_]*$/'; |
18
|
|
|
private const QUESTION_MAX_ATTEMPTS = 3; |
19
|
|
|
|
20
|
|
|
protected static $defaultName = 'gb:role:add'; |
21
|
|
|
|
22
|
|
|
protected function configure(): void |
23
|
|
|
{ |
24
|
|
|
$this |
25
|
|
|
->setDescription('Add a new role') |
26
|
|
|
->addArgument('role', InputArgument::OPTIONAL) |
27
|
|
|
; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws InvalidRolePatternException |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
34
|
|
|
{ |
35
|
|
|
$io = new SymfonyStyle($input, $output); |
36
|
|
|
$helper = $this->getHelper('question'); |
37
|
|
|
$roleName = $input->getArgument('role'); |
38
|
|
|
|
39
|
|
|
if (null === $roleName) { |
40
|
|
|
$question = new Question('Enter a role name. Example ROLE_USER: '); |
41
|
|
|
$question->setValidator(function ($answer) { |
42
|
|
|
if (null === $answer) { |
43
|
|
|
throw new \Exception('The role name cannot be empty'); |
44
|
|
|
} |
45
|
|
|
if (false === $this->isValidRoleName($answer)) { |
46
|
|
|
throw new InvalidRolePatternException(self::ROLE_ALLOWED_PATTERN); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $answer; |
50
|
|
|
}); |
51
|
|
|
$question->setMaxAttempts(self::QUESTION_MAX_ATTEMPTS); |
52
|
|
|
if ($this->isTestEnv()) { |
53
|
|
|
$question->setMaxAttempts(1); |
54
|
|
|
} |
55
|
|
|
$roleName = $helper->ask($input, $output, $question); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (false === $this->isValidRoleName($roleName)) { |
59
|
|
|
throw new InvalidRolePatternException(self::ROLE_ALLOWED_PATTERN); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var Role|null $role */ |
63
|
|
|
$role = $this->findRoleByName($roleName); |
64
|
|
|
if (null !== $role) { |
65
|
|
|
$io->error(sprintf('The role name %s already exist', $roleName)); |
66
|
|
|
|
67
|
|
|
return 1; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$role = (new Role()) |
71
|
|
|
->setName($roleName) |
72
|
|
|
->setDescription($this->transformRoleNameToRoleDescription($roleName)) |
73
|
|
|
; |
74
|
|
|
$this->getEntityManager()->persist($role); |
75
|
|
|
$this->getEntityManager()->flush(); |
76
|
|
|
|
77
|
|
|
$io->success(sprintf('The role %s was added successfully', $roleName)); |
78
|
|
|
|
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function isValidRoleName(string $roleName): bool |
83
|
|
|
{ |
84
|
|
|
return (bool) preg_match(self::ROLE_ALLOWED_PATTERN, $roleName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function transformRoleNameToRoleDescription(string $roleName): string |
88
|
|
|
{ |
89
|
|
|
return mb_strtolower($roleName.'.description'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|