...
|
...
|
@@ -86,7 +86,7 @@ class InitController extends Controller |
|
|
*/
|
|
|
public function actionRbac(){
|
|
|
|
|
|
$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");
|
|
|
$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");
|
|
|
$action = $this->prompt('Which:');
|
|
|
|
|
|
$auth = Yii::createObject('yii\rbac\DbManager');
|
...
|
...
|
@@ -119,9 +119,11 @@ class InitController extends Controller |
|
|
}elseif ($action == 3){
|
|
|
echo "创建一个新角色 ...\n"; // 提示当前操作
|
|
|
$name = $this->prompt('Role Name:'); // 角色名称
|
|
|
$description = $this->prompt('Description(default is as name):'); // 描述
|
|
|
|
|
|
if ($name){
|
|
|
$role = $auth->createRole($name);
|
|
|
$role->description = $description ? : ucfirst($name);
|
|
|
if ($auth->add($role)){
|
|
|
$this->stdout('create ok');
|
|
|
}
|
...
|
...
|
@@ -155,6 +157,21 @@ class InitController extends Controller |
|
|
$this->stdout('grant permission ok');
|
|
|
}
|
|
|
}elseif ($action == 6){
|
|
|
echo "从某个角色剥夺权限 ...\n"; // 提示当前操作
|
|
|
$name = $this->prompt('Role Name:'); // 接收用户名
|
|
|
if (!$role = $auth->getRole($name)){
|
|
|
$this->stderr('Not a predefined role');
|
|
|
return 1;
|
|
|
}
|
|
|
$name = $this->prompt('Permission Name:'); // 接收用户名
|
|
|
if (!$permission = $auth->getPermission($name)){
|
|
|
$this->stderr('Not a predefined permission');
|
|
|
return 1;
|
|
|
}
|
|
|
if ($auth->removeChild($role,$permission)){
|
|
|
$this->stdout('revoke permission ok');
|
|
|
}
|
|
|
}elseif ($action == 7){
|
|
|
echo "向某个用户赋予角色 ...\n"; // 提示当前操作
|
|
|
$username = $this->prompt('User Name:'); // 接收用户名
|
|
|
if (!$user = Admin::findByUsername($username)){
|
...
|
...
|
@@ -169,6 +186,21 @@ class InitController extends Controller |
|
|
if ($auth->assign($role,$user->getId())){
|
|
|
$this->stdout('grant role ok');
|
|
|
}
|
|
|
}elseif ($action == 8){
|
|
|
echo "从某个用户剥夺角色 ...\n"; // 提示当前操作
|
|
|
$username = $this->prompt('User Name:'); // 接收用户名
|
|
|
if (!$user = Admin::findByUsername($username)){
|
|
|
echo '找不到该用户';
|
|
|
return 1;
|
|
|
}
|
|
|
$name = $this->prompt('Role Name:'); // 接收用户名
|
|
|
if (!$role = $auth->getRole($name)){
|
|
|
$this->stderr('Not a predefined role');
|
|
|
return 1;
|
|
|
}
|
|
|
if ($auth->revoke($role,$user->getId())){
|
|
|
$this->stdout('revoke role ok');
|
|
|
}
|
|
|
}else{
|
|
|
$this->stderr('Not a predefined option');
|
|
|
return 1;
|
...
|
...
|
|