1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Command\User; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
12
|
|
|
use Yiisoft\Rbac\Manager; |
13
|
|
|
use Yiisoft\Rbac\Role; |
14
|
|
|
use Yiisoft\Rbac\StorageInterface; |
15
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
16
|
|
|
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy; |
17
|
|
|
|
18
|
|
|
class AssignRoleCommand extends Command |
19
|
|
|
{ |
20
|
|
|
private CycleDependencyProxy $promise; |
21
|
|
|
private Manager $manager; |
22
|
|
|
private StorageInterface $storage; |
23
|
|
|
|
24
|
|
|
protected static $defaultName = 'user/assignRole'; |
25
|
|
|
|
26
|
|
|
public function __construct(CycleDependencyProxy $promise, Manager $manager, StorageInterface $storage) |
27
|
|
|
{ |
28
|
|
|
$this->promise = $promise; |
29
|
|
|
$this->manager = $manager; |
30
|
|
|
$this->storage = $storage; |
31
|
|
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function configure(): void |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setDescription('Assign RBAC role to given user') |
38
|
|
|
->setHelp('This command allows you to assign RBAC role to user') |
39
|
|
|
->addArgument('role', InputArgument::REQUIRED, 'RBAC role') |
40
|
|
|
->addArgument('userId', InputArgument::REQUIRED, 'User id'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
44
|
|
|
{ |
45
|
|
|
$io = new SymfonyStyle($input, $output); |
46
|
|
|
|
47
|
|
|
$roleName = $input->getArgument('role'); |
48
|
|
|
$userId = $input->getArgument('userId'); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$orm = $this->promise->getORM(); |
52
|
|
|
$userRepo = $orm->getRepository(User::class); |
53
|
|
|
$user = $userRepo->findByPK($userId); |
54
|
|
|
if (is_null($user)) { |
55
|
|
|
throw new \Exception('Can\'t find user'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$role = $this->storage->getRoleByName($roleName); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (is_null($role)) { |
61
|
|
|
$helper = $this->getHelper('question'); |
62
|
|
|
$question = new ConfirmationQuestion('Role doesn\'t exist. Create new one? ', false); |
63
|
|
|
|
64
|
|
|
if (!$helper->ask($input, $output, $question)) { |
65
|
|
|
return ExitCode::OK; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$role = new Role($roleName); |
|
|
|
|
69
|
|
|
$this->manager->addRole($role); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->manager->assign($role, $user->getId()); |
73
|
|
|
|
74
|
|
|
$io->success('Role was assigned to given user'); |
75
|
|
|
} catch (\Throwable $t) { |
76
|
|
|
$io->error($t->getMessage()); |
77
|
|
|
return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return ExitCode::OK; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|