|
@@ -86,7 +86,7 @@ class InitController extends Controller |
|
@@ -86,7 +86,7 @@ class InitController extends Controller |
86
|
*/
|
86
|
*/
|
87
|
public function actionRbac(){
|
87
|
public function actionRbac(){
|
88
|
|
88
|
|
89
|
- $this->stdout("1: Create permission\n2: Remove permission\n3: Create role\n4: Remove role\n5: Grant permission to a role\n6: Grant role to a user\n");
|
89
|
+ $this->stdout("1: Create permission\n2: Remove permission\n3: Create role\n4: Remove role\n5: Grant permission to a role\n6: Revoke permission from a role\n7: Grant role to a user\n8: Revoke role from a a\n");
|
90
|
$action = $this->prompt('Which:');
|
90
|
$action = $this->prompt('Which:');
|
91
|
|
91
|
|
92
|
$auth = Yii::createObject('yii\rbac\DbManager');
|
92
|
$auth = Yii::createObject('yii\rbac\DbManager');
|
|
@@ -119,9 +119,11 @@ class InitController extends Controller |
|
@@ -119,9 +119,11 @@ class InitController extends Controller |
119
|
}elseif ($action == 3){
|
119
|
}elseif ($action == 3){
|
120
|
echo "创建一个新角色 ...\n"; // 提示当前操作
|
120
|
echo "创建一个新角色 ...\n"; // 提示当前操作
|
121
|
$name = $this->prompt('Role Name:'); // 角色名称
|
121
|
$name = $this->prompt('Role Name:'); // 角色名称
|
|
|
122
|
+ $description = $this->prompt('Description(default is as name):'); // 描述
|
122
|
|
123
|
|
123
|
if ($name){
|
124
|
if ($name){
|
124
|
$role = $auth->createRole($name);
|
125
|
$role = $auth->createRole($name);
|
|
|
126
|
+ $role->description = $description ? : ucfirst($name);
|
125
|
if ($auth->add($role)){
|
127
|
if ($auth->add($role)){
|
126
|
$this->stdout('create ok');
|
128
|
$this->stdout('create ok');
|
127
|
}
|
129
|
}
|
|
@@ -155,6 +157,21 @@ class InitController extends Controller |
|
@@ -155,6 +157,21 @@ class InitController extends Controller |
155
|
$this->stdout('grant permission ok');
|
157
|
$this->stdout('grant permission ok');
|
156
|
}
|
158
|
}
|
157
|
}elseif ($action == 6){
|
159
|
}elseif ($action == 6){
|
|
|
160
|
+ echo "从某个角色剥夺权限 ...\n"; // 提示当前操作
|
|
|
161
|
+ $name = $this->prompt('Role Name:'); // 接收用户名
|
|
|
162
|
+ if (!$role = $auth->getRole($name)){
|
|
|
163
|
+ $this->stderr('Not a predefined role');
|
|
|
164
|
+ return 1;
|
|
|
165
|
+ }
|
|
|
166
|
+ $name = $this->prompt('Permission Name:'); // 接收用户名
|
|
|
167
|
+ if (!$permission = $auth->getPermission($name)){
|
|
|
168
|
+ $this->stderr('Not a predefined permission');
|
|
|
169
|
+ return 1;
|
|
|
170
|
+ }
|
|
|
171
|
+ if ($auth->removeChild($role,$permission)){
|
|
|
172
|
+ $this->stdout('revoke permission ok');
|
|
|
173
|
+ }
|
|
|
174
|
+ }elseif ($action == 7){
|
158
|
echo "向某个用户赋予角色 ...\n"; // 提示当前操作
|
175
|
echo "向某个用户赋予角色 ...\n"; // 提示当前操作
|
159
|
$username = $this->prompt('User Name:'); // 接收用户名
|
176
|
$username = $this->prompt('User Name:'); // 接收用户名
|
160
|
if (!$user = Admin::findByUsername($username)){
|
177
|
if (!$user = Admin::findByUsername($username)){
|
|
@@ -169,6 +186,21 @@ class InitController extends Controller |
|
@@ -169,6 +186,21 @@ class InitController extends Controller |
169
|
if ($auth->assign($role,$user->getId())){
|
186
|
if ($auth->assign($role,$user->getId())){
|
170
|
$this->stdout('grant role ok');
|
187
|
$this->stdout('grant role ok');
|
171
|
}
|
188
|
}
|
|
|
189
|
+ }elseif ($action == 8){
|
|
|
190
|
+ echo "从某个用户剥夺角色 ...\n"; // 提示当前操作
|
|
|
191
|
+ $username = $this->prompt('User Name:'); // 接收用户名
|
|
|
192
|
+ if (!$user = Admin::findByUsername($username)){
|
|
|
193
|
+ echo '找不到该用户';
|
|
|
194
|
+ return 1;
|
|
|
195
|
+ }
|
|
|
196
|
+ $name = $this->prompt('Role Name:'); // 接收用户名
|
|
|
197
|
+ if (!$role = $auth->getRole($name)){
|
|
|
198
|
+ $this->stderr('Not a predefined role');
|
|
|
199
|
+ return 1;
|
|
|
200
|
+ }
|
|
|
201
|
+ if ($auth->revoke($role,$user->getId())){
|
|
|
202
|
+ $this->stdout('revoke role ok');
|
|
|
203
|
+ }
|
172
|
}else{
|
204
|
}else{
|
173
|
$this->stderr('Not a predefined option');
|
205
|
$this->stderr('Not a predefined option');
|
174
|
return 1;
|
206
|
return 1;
|