1 | <?php |
||
2 | /** |
||
3 | * RBAC implementation for HiPanel |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/hipanel-rbac |
||
6 | * @package hipanel-rbac |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2016-2020, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hipanel\rbac\console; |
||
12 | |||
13 | use hipanel\rbac\AbstractIniter; |
||
14 | use hipanel\rbac\console\converter\ConverterInterface; |
||
15 | use hipanel\rbac\RbacIniterInterface; |
||
16 | use yii\base\Module; |
||
17 | use yii\helpers\Console; |
||
18 | use yii\rbac\CheckAccessInterface; |
||
19 | |||
20 | /** |
||
21 | * Class RbacController. |
||
22 | * |
||
23 | * Usage: `hidev rbac/init` |
||
24 | */ |
||
25 | class RbacController extends \yii\console\Controller |
||
26 | { |
||
27 | /** |
||
28 | * @var RbacIniterInterface|AbstractIniter |
||
29 | */ |
||
30 | protected $initer; |
||
31 | |||
32 | /** |
||
33 | * @var CheckAccessInterface |
||
34 | */ |
||
35 | private $auth; |
||
36 | |||
37 | private ConverterInterface $converter; |
||
38 | |||
39 | public function __construct( |
||
40 | $id, |
||
41 | Module $module, |
||
42 | RbacIniterInterface $initer, |
||
43 | ConverterInterface $converter, |
||
44 | CheckAccessInterface $auth, |
||
45 | $config = []) |
||
46 | { |
||
47 | parent::__construct($id, $module, $config); |
||
48 | |||
49 | $this->initer = $initer; |
||
50 | $this->auth = $auth; |
||
51 | $this->converter = $converter; |
||
52 | } |
||
53 | |||
54 | public $defaultAction = 'show'; |
||
55 | |||
56 | public function actionInit() |
||
57 | { |
||
58 | $this->initer->init($this->auth); |
||
59 | $this->converter->convert(); |
||
60 | $this->stdout('Consider running rbac/generate-descriptions to generate default role descriptions', Console::FG_YELLOW); |
||
61 | } |
||
62 | |||
63 | public function actionGenerateJs() |
||
64 | { |
||
65 | $this->converter->convert(); |
||
66 | } |
||
67 | |||
68 | public function actionReinit() |
||
69 | { |
||
70 | $this->initer->reinit($this->auth); |
||
71 | |||
72 | $path = dirname(__DIR__) . '/files/source/metadata.php'; |
||
73 | (new MetadataGenerator($this->auth, $this->initer))->dump($path); |
||
74 | |||
75 | $this->initer->reinit($this->auth); // Reinit using newly generated descriptions |
||
76 | |||
77 | $this->converter->convert(); |
||
78 | } |
||
79 | |||
80 | public function actionShow() |
||
81 | { |
||
82 | echo "Permissions:\n"; |
||
83 | $permissions = $this->auth->getPermissions(); |
||
84 | ksort($permissions); |
||
85 | foreach ($permissions as $name => $perm) { |
||
86 | echo " $perm->name – $perm->description\n"; |
||
87 | } |
||
88 | |||
89 | echo "Roles:\n"; |
||
90 | foreach ($this->auth->getRoles() as $name => $role) { |
||
91 | $children = implode(', ', array_keys($this->auth->getChildren($name))); |
||
92 | printf(" %-12s\n %s\n\n", "$role->name – $role->description", $children); |
||
93 | } |
||
94 | |||
95 | echo "Assignments:\n"; |
||
96 | foreach ($this->auth->getAllAssignments() as $userId => $roles) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
97 | $roles = implode(', ', array_keys($roles)); |
||
98 | printf(" %-12s %s\n", "$userId:", $roles); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | public function actionGenerateDescriptions() |
||
103 | { |
||
104 | $path = dirname(__DIR__) . '/files/source/metadata.php'; |
||
105 | (new MetadataGenerator($this->auth, $this->initer))->dump($path); |
||
106 | |||
107 | $this->initer->reinit($this->auth); |
||
108 | $this->converter->convert(); |
||
109 | } |
||
110 | |||
111 | public function actionPlantuml() |
||
112 | { |
||
113 | $path = dirname(__DIR__, 2) . '/docs/test.txt'; |
||
114 | |||
115 | $plant = new PlantUML($this->auth); |
||
116 | |||
117 | $uml = $plant->build(); |
||
118 | |||
119 | file_put_contents($path, $uml); |
||
120 | } |
||
121 | } |
||
122 |