1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\commands; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use yii\console\{Controller, ExitCode}; |
8
|
|
|
use yii\helpers\Console; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Command for update permissions |
12
|
|
|
* |
13
|
|
|
* @see config/rbac/permissions.php |
14
|
|
|
*/ |
15
|
|
|
class RbacController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var authManager |
19
|
|
|
*/ |
20
|
|
|
private $auth; |
21
|
|
|
/** |
22
|
|
|
* @var string Config file path |
23
|
|
|
*/ |
24
|
|
|
public $path; |
25
|
1 |
|
|
26
|
|
|
public function options($actionId = '') |
27
|
1 |
|
{ |
28
|
|
|
return ['path']; |
29
|
|
|
} |
30
|
3 |
|
|
31
|
|
|
public function beforeAction($action) |
32
|
3 |
|
{ |
33
|
3 |
|
if (parent::beforeAction($action)) { |
34
|
1 |
|
if (empty($this->path)) { |
35
|
|
|
throw new InvalidConfigException('`path` should be specified'); |
36
|
|
|
} |
37
|
2 |
|
} |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
4 |
|
|
41
|
|
|
public function init() |
42
|
4 |
|
{ |
43
|
4 |
|
$this->auth = Yii::$app->authManager; |
44
|
4 |
|
Yii::$app->cache->delete('rbac-permissions'); |
45
|
|
|
} |
46
|
2 |
|
|
47
|
|
|
public function actionUp(): int |
48
|
2 |
|
{ |
49
|
2 |
|
$currentPermissions = $this->auth->getPermissions(); |
50
|
|
|
$newPermissions = require Yii::getAlias($this->path); |
51
|
2 |
|
|
52
|
2 |
|
$this->cleanUnusedPermissions($currentPermissions, $newPermissions); |
53
|
|
|
$this->updatePermissions($currentPermissions, $newPermissions); |
54
|
2 |
|
|
55
|
2 |
|
$this->stdout("Done!\n", Console::FG_GREEN); |
56
|
|
|
return ExitCode::OK; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
private function cleanUnusedPermissions(array $currentPermissions, array $newPermissions): void |
60
|
2 |
|
{ |
61
|
2 |
|
foreach ($currentPermissions as $currentPermission) { |
62
|
|
|
if (!isset($newPermissions[$currentPermission->name])) { |
63
|
|
|
$this->auth->remove($currentPermission); |
64
|
2 |
|
} |
65
|
|
|
} |
66
|
2 |
|
} |
67
|
|
|
|
68
|
2 |
|
private function updatePermissions(array $currentPermissions, array $newPermissions): void |
69
|
2 |
|
{ |
70
|
2 |
|
foreach ($newPermissions as $name => $description) { |
71
|
1 |
|
$isNew = !isset($currentPermissions[$name]); |
72
|
1 |
|
if ($isNew) { |
73
|
|
|
$this->add($name, $description); |
74
|
2 |
|
continue; |
75
|
|
|
} |
76
|
2 |
|
$this->update($name, $description); |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
private function add(string $name, string $description): void |
81
|
1 |
|
{ |
82
|
1 |
|
$permission = $this->auth->createPermission($name); |
83
|
1 |
|
$permission->description = $description; |
84
|
1 |
|
$this->auth->add($permission); |
85
|
|
|
$this->stdout("Added: $name\n", Console::FG_GREEN); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
private function update(string $name, string $description): void |
89
|
2 |
|
{ |
90
|
2 |
|
$permission = $this->auth->getPermission($name); |
91
|
2 |
|
$permission->description = $description; |
92
|
2 |
|
$this->auth->update($name, $permission); |
93
|
|
|
$this->stdout("Updated: $name\n", Console::FG_GREEN); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|