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-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\rbac\console; |
12
|
|
|
|
13
|
|
|
use hipanel\rbac\AbstractIniter; |
14
|
|
|
use hipanel\rbac\AuthManager; |
15
|
|
|
use hipanel\rbac\RbacIniterInterface; |
16
|
|
|
use hiqdev\yii\compat\yii; |
17
|
|
|
use yii\base\Module; |
18
|
|
|
use yii\helpers\Console; |
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
|
|
|
public function __construct($id, Module $module, RbacIniterInterface $initer, $config = []) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($id, $module, $config); |
35
|
|
|
|
36
|
|
|
$this->initer = $initer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public $defaultAction = 'show'; |
40
|
|
|
|
41
|
|
|
public function actionInit() |
42
|
|
|
{ |
43
|
|
|
$auth = yii::getApp()->get('authManager'); |
44
|
|
|
$this->initer->init($auth); |
|
|
|
|
45
|
|
|
$this->stdout('Consider running rbac/generate-descriptions to generate default role descriptions', Console::FG_YELLOW); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function actionReinit() |
49
|
|
|
{ |
50
|
|
|
$auth = yii::getApp()->get('authManager'); |
51
|
|
|
$this->initer->reinit($auth); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$path = dirname(__DIR__) . '/files/source/metadata.php'; |
54
|
|
|
(new MetadataGenerator($auth, $this->initer))->dump($path); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$this->initer->reinit($auth); // Reinit using newly generated descriptions |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function actionShow() |
60
|
|
|
{ |
61
|
|
|
$auth = yii::getApp()->get('authManager'); |
62
|
|
|
|
63
|
|
|
echo "Permissions:\n"; |
64
|
|
|
$permissions = $auth->getPermissions(); |
65
|
|
|
ksort($permissions); |
66
|
|
|
foreach ($permissions as $name => $perm) { |
67
|
|
|
echo " $perm->name – $perm->description\n"; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
echo "Roles:\n"; |
71
|
|
|
foreach ($auth->getRoles() as $name => $role) { |
72
|
|
|
$children = implode(', ', array_keys($auth->getChildren($name))); |
73
|
|
|
printf(" %-12s\n %s\n\n", "$role->name – $role->description", $children); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
echo "Assignments:\n"; |
77
|
|
|
foreach ($auth->getAllAssignments() as $userId => $roles) { |
78
|
|
|
$roles = implode(', ', array_keys($roles)); |
79
|
|
|
printf(" %-12s %s\n", "$userId:", $roles); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function actionGenerateDescriptions() |
84
|
|
|
{ |
85
|
|
|
/** @var AuthManager $auth */ |
86
|
|
|
$auth = yii::getApp()->get('authManager'); |
87
|
|
|
|
88
|
|
|
$path = dirname(__DIR__) . '/files/source/metadata.php'; |
89
|
|
|
(new MetadataGenerator($auth, $this->initer))->dump($path); |
90
|
|
|
|
91
|
|
|
$this->initer->reinit($auth); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function actionPlantuml() |
95
|
|
|
{ |
96
|
|
|
$path = dirname(__DIR__, 2) . '/docs/test.txt'; |
97
|
|
|
|
98
|
|
|
$auth = yii::getApp()->get('authManager'); |
99
|
|
|
$plant = new PlantUML($auth); |
100
|
|
|
|
101
|
|
|
$uml = $plant->build(); |
102
|
|
|
|
103
|
|
|
file_put_contents($path, $uml); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|