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