1 | <?php |
||
14 | class RbacController extends Controller |
||
15 | { |
||
16 | /** |
||
17 | * @var authManager |
||
18 | */ |
||
19 | private $auth; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | public $path; |
||
24 | |||
25 | /** |
||
26 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
27 | */ |
||
28 | 1 | public function options($actionId = '') |
|
32 | |||
33 | 3 | public function beforeAction($action) |
|
34 | { |
||
35 | 3 | if (parent::beforeAction($action)) { |
|
36 | 3 | if (empty($this->path)) { |
|
37 | 1 | throw new InvalidConfigException('`path` should be specified'); |
|
38 | } |
||
39 | } |
||
40 | 2 | return true; |
|
41 | } |
||
42 | |||
43 | 4 | public function init() |
|
48 | |||
49 | 2 | public function actionUp() |
|
50 | { |
||
51 | 2 | $currentPermissions = $this->auth->getPermissions(); |
|
52 | 2 | $newPermissions = require Yii::getAlias($this->path); |
|
53 | |||
54 | 2 | $this->cleanUnusedPermissions($currentPermissions, $newPermissions); |
|
55 | 2 | $this->updatePermissions($currentPermissions, $newPermissions); |
|
56 | |||
57 | 2 | $this->stdout("Done!\n", Console::FG_GREEN); |
|
58 | 2 | } |
|
59 | |||
60 | 2 | private function cleanUnusedPermissions($currentPermissions, $newPermissions) |
|
61 | { |
||
62 | 2 | foreach ($currentPermissions as $currentPermission) { |
|
63 | 2 | if (!isset($newPermissions[$currentPermission->name])) { |
|
64 | 2 | $this->auth->remove($currentPermission); |
|
65 | } |
||
66 | } |
||
67 | 2 | } |
|
68 | |||
69 | 2 | private function updatePermissions($currentPermissions, $newPermissions) |
|
70 | { |
||
71 | 2 | foreach ($newPermissions as $name => $description) { |
|
72 | 2 | $isNew = !isset($currentPermissions[$name]); |
|
73 | 2 | if ($isNew) { |
|
74 | 1 | $this->add($name, $description); |
|
75 | 1 | continue; |
|
76 | } |
||
77 | 2 | $this->update($name, $description); |
|
78 | } |
||
79 | 2 | } |
|
80 | |||
81 | 1 | private function add($name, $description) |
|
88 | |||
89 | 2 | private function update($name, $description) |
|
96 | } |
||
97 |