...
|
...
|
@@ -86,10 +86,11 @@ class InitController extends Controller |
|
|
*/
|
|
|
public function actionRbac(){
|
|
|
|
|
|
$this->stdout("1: Create rbac permission\n2: Delete rbac permission\n");
|
|
|
$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");
|
|
|
$action = $this->prompt('Which:');
|
|
|
|
|
|
$auth = Yii::$app->authManager;
|
|
|
$auth = Yii::createObject('yii\rbac\DbManager');
|
|
|
//$auth = Yii::$app->authManager;
|
|
|
if ($action == 1){
|
|
|
echo "创建一个新权限标签 ...\n"; // 提示当前操作
|
|
|
$name = $this->prompt('Permission Name:'); // 权限名称
|
...
|
...
|
@@ -98,7 +99,9 @@ class InitController extends Controller |
|
|
if ($name){
|
|
|
$permission = $auth->createPermission($name);
|
|
|
$permission->description = $description ? : ucwords(strtr($name,array('/'=>' ')));
|
|
|
$r = $auth->add($permission);
|
|
|
if ($auth->add($permission)){
|
|
|
$this->stdout('create ok');
|
|
|
}
|
|
|
}
|
|
|
}elseif ($action == 2){
|
|
|
echo "移除一个权限标签 ...\n"; // 提示当前操作
|
...
|
...
|
@@ -109,14 +112,68 @@ class InitController extends Controller |
|
|
$this->stderr('Not a predefined permission');
|
|
|
return 1;
|
|
|
}
|
|
|
$r = $auth->remove($permission);
|
|
|
if ($auth->remove($permission)){
|
|
|
$this->stdout('remove ok');
|
|
|
}
|
|
|
}
|
|
|
}elseif ($action == 3){
|
|
|
echo "创建一个新角色 ...\n"; // 提示当前操作
|
|
|
$name = $this->prompt('Role Name:'); // 角色名称
|
|
|
|
|
|
if ($name){
|
|
|
$role = $auth->createRole($name);
|
|
|
if ($auth->add($role)){
|
|
|
$this->stdout('create ok');
|
|
|
}
|
|
|
}
|
|
|
}elseif ($action == 4){
|
|
|
echo "移除一个角色 ...\n"; // 提示当前操作
|
|
|
$name = $this->prompt('Role Name:'); // 角色名称
|
|
|
|
|
|
if ($name){
|
|
|
if (!$role = $auth->getRole($name)){
|
|
|
$this->stderr('Not a predefined role');
|
|
|
return 1;
|
|
|
}
|
|
|
if ($auth->remove($role)){
|
|
|
$this->stdout('remove ok');
|
|
|
}
|
|
|
}
|
|
|
}elseif ($action == 5){
|
|
|
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->addChild($role,$permission)){
|
|
|
$this->stdout('grant permission ok');
|
|
|
}
|
|
|
}elseif ($action == 6){
|
|
|
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->assign($role,$user->getId())){
|
|
|
$this->stdout('grant role ok');
|
|
|
}
|
|
|
}else{
|
|
|
$this->stderr('Not a predefined option');
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
var_dump($r);
|
|
|
return 0;
|
|
|
}
|
|
|
} |
...
|
...
|
|