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\RbacIniterInterface; |
14
|
|
|
use yii\base\Module; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RbacController. |
18
|
|
|
* |
19
|
|
|
* Usage: `hidev rbac/init` |
20
|
|
|
*/ |
21
|
|
|
class RbacController extends \yii\console\Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RbacIniterInterface |
25
|
|
|
*/ |
26
|
|
|
protected $initer; |
27
|
|
|
|
28
|
|
|
public function __construct($id, Module $module, RbacIniterInterface $initer, $config = []) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($id, $module, $config); |
31
|
|
|
|
32
|
|
|
$this->initer = $initer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public $defaultAction = 'show'; |
36
|
|
|
|
37
|
|
|
public function actionInit() |
38
|
|
|
{ |
39
|
|
|
$auth = $this->getApp()->get('authManager'); |
40
|
|
|
$this->initer->init($auth); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function actionReinit() |
44
|
|
|
{ |
45
|
|
|
$auth = $this->getApp()->get('authManager'); |
46
|
|
|
$this->initer->reinit($auth); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function actionShow() |
50
|
|
|
{ |
51
|
|
|
$auth = $this->getApp()->get('authManager'); |
52
|
|
|
|
53
|
|
|
echo "Permissions:\n"; |
54
|
|
|
$permissions = $auth->getPermissions(); |
55
|
|
|
ksort($permissions); |
56
|
|
|
foreach ($permissions as $name => $perm) { |
57
|
|
|
echo " $perm->name $perm->description\n"; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
echo "Roles:\n"; |
61
|
|
|
foreach ($auth->getRoles() as $name => $role) { |
62
|
|
|
$children = implode(',', array_keys($auth->getChildren($name))); |
63
|
|
|
printf(" %-12s %s\n", "$role->name:", $children); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
echo "Assignments:\n"; |
67
|
|
|
foreach ($auth->getAllAssignments() as $userId => $roles) { |
68
|
|
|
$roles = implode(',', array_keys($roles)); |
69
|
|
|
printf(" %-12s %s\n", "$userId:", $roles); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getApp() |
74
|
|
|
{ |
75
|
|
|
return class_exists('Yii') ? \Yii::$app : \yii\helpers\Yii::getApp(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|