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