您的位置:首页 > 编程语言 > PHP开发

Yii2 RBAC

2016-07-10 01:09 726 查看
以下是在高级应用模板中的简单实践,用DBManager的方式,Rules和Default Roles略过…

配置authManager

在common/config/main.php中加入配置,像这样:

return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
// ...
],
];


接下来还需要在数据库中创建四张表,跑到项目根目录,使用这个命令:

php yii migrate --migrationPath=@yii/rbac/migrations


这样表就创建完了。在数据库里我们就会看到新增的几张表:

auth_item

auth_item_child

auth_assignment

auth_rule

同样将他们加入配置文件:

return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'auth_item',
'itemChildTable' => 'auth_item_child',
'assignmentTable' => 'auth_assignment',
'ruleTable' => 'auth_rule',
],
// ...
],
];


到此配置就完成了。

创建权限数据

在console/controllers目录下面创建控制器RbacController:

<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;

// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);

// add "updatePost" permission
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);

// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);

// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);

// Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($author, 2);
$auth->assign($admin, 1);
}
}


上面的代码很直观,无须解释更多。

然后执行下面的命令:

php yii rbac/init


执行完了,不出意外我们的数据表里增加了几条数据:





接下来就是要把权限分配给用户了。比如在用户注册的时候,赋予它相应的角色。

改一下这个frontend\models\SignupForm::signup():

public function signup()
{
if (!$this->validate()) {
return null;
}

$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->save();

// 看这里
$auth = \Yii::$app->authManager;
$authorRole = $auth->getRole('author');
$auth->assign($authorRole, $user->getId());

return $user;
}


跑到前台注册一下,auth_assignment表会产生这样的数据:



user_id=1,就是刚注册的数据。

权限控制

接下来就简单了。

在代码里用这个:

if (\Yii::$app->user->can('createPost')) {
// create post
}


我们跑到site/about里实验一下:

public function actionAbout()
{
if (! \Yii::$app->user->can('createPost')) {
return 'Permission Denied';
}
return $this->render('about');
}


刚注册的用户是可以看到这个页面的,如果把createPost改成updatePost就只能看到’Permission Denied’了。

除此之外,还可以这样做:

// 这个文件:
// frontend/controllers/SiteController.php
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
// 这里加个about
'only' => ['logout', 'signup', 'about'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
// 看这里
[
'actions' => ['about'],
'allow' => true,
'roles' => ['updatePost'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}


访问一下,会看到这样:



因为用户只有createPost权限。

如果想要动态添加角色和权限,可以查看类参考手册,yii\rbac\ManagerInterface提供了各种方法。比如下面这些方法:

\Yii::$app->authManager->getPermissions();//获取所有权限
\Yii::$app->authManager->getRoles();//获取所以角色
//还有各种add,get,remove...


至此RBAC试验完毕。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yii rbac