RbacController.php
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace common\modules\comment\commands;
class RbacController extends \yii\console\Controller
{
public function actionInstall()
{
/**
* @var \common\modules\comment\Module $module
*/
$module = \Yii::$app->controller->module;
if(!$module->useRbac) {
throw new \yii\base\InvalidConfigException('Please set useRbac config to TRUE in your module configs');
}
$auth = \Yii::$app->getAuthManager();
if(!$auth instanceof \yii\rbac\ManagerInterface) {
throw new \yii\base\InvalidConfigException('ManagerInterface is not configured');
}
if(!empty($module->rbac['rules'])) {
foreach($module->rbac['rules'] as $rule) {
$rule_model = new $rule();
echo "Creating rule: ".$rule_model->name."\n";
if($auth->add($rule_model)) {
echo "Successful\n";
} else {
echo "Failed\n";
}
unset($rule_model);
}
}
if(!empty($module->rbac['permissions'])) {
foreach($module->rbac['permissions'] as $permission) {
echo "Creating permission: ".$permission['name']."\n";
if($auth->add(new \yii\rbac\Permission($permission))) {
echo "Successful\n";
} else {
echo "Failed\n";
}
}
}
}
public function actionUninstall()
{
/**
* @var \common\modules\comment\Module $module
*/
$module = \Yii::$app->controller->module;
if(!$module->useRbac) {
throw new \yii\base\InvalidConfigException('Please set useRbac config to TRUE in your module configs');
}
$auth = \Yii::$app->getAuthManager();
if(!$auth instanceof \yii\rbac\ManagerInterface) {
throw new \yii\base\InvalidConfigException('ManagerInterface is not configured');
}
if(!empty($module->rbac['rules'])) {
foreach($module->rbac['rules'] as $rule) {
$rule_model = new $rule();
echo "Removing rule: ".$rule_model->name."\n";
if($auth->remove($rule_model)) {
echo "Successful\n";
} else {
echo "Failed\n";
}
unset($rule_model);
}
}
if(!empty($module->rbac['permissions'])) {
foreach($module->rbac['permissions'] as $permission) {
echo "Removing permission: ".$permission['name']."\n";
if($auth->remove(new \yii\rbac\Permission($permission))) {
echo "Successful\n";
} else {
echo "Failed\n";
}
}
}
}
}