rbac php yii,yii2 实现rbac

1.配置RBAC

在配置文件配置RBAC

return [

// ...

'components' => [

'authManager' => [

// 'class' => 'yii\rbac\PhpManager', //使用php脚本保存授权数据

'class' => 'yii\rbac\DbMannager' //使用数据库保存授权数据

//'cache' => 'cache' //使用缓存

],

'user' => [

'identityClass' => 'app\models\Admin',//后台用户(管理员)模型(model)类

'enableAutoLogin' => true,

'enableSession' => false,//是否启用session,用token登录设为false

]

// ...

],

];

2.迁移(migration)权限数据库

使用命令行

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

之后会在数据库生成4张表

* [itemTable]: 该表存放授权条目(角色和权限同一张表,通过type区分)。默认表名为 "auth_item" 。

* [itemChildTable]: 该表存放授权条目的层次关系。默认表名为 "auth_item_child"。

* [assignmentTable]: 该表存放授权条目对用户的指派情况。默认表名为 "auth_assignment"。

* [ruleTable]: 该表存放规则(自定义规则)。默认表名为 "auth_rule"。

3.在控制器基础类里添加验证行为(behaviors)

namespace app\controllers\admin;

use Yii;

use yii\web\Controller;

use yii\filters\auth\HttpHeaderAuth;

/**

* 基础控制器类,其他控制器再继承该类

*/

class BaseController extends Controller

{

public function behaviors()

{

return [

//...

//简单权限检测,可以不配置该字段

"access" => [

'class' => AccessControl::class,

'only' => [], //需要检测的action

'except' => [],//不需要检测的action

//规则

'rules' => [

[

'allow' => true, //true代表符合该规则的可以访问,false表示符合该规则的不可以访问

'actions' => ['test'],//action id

'roles' => ['@'] //@表示已经认证的用户,?表示路人访客

]

],

//拒绝访问的回调函数

'denyCallback' => function($rule, $action){

print('You are not allowed to access this page');

}

],

//登录状态检测

'authValidate' => [

//检测是否登录的类

//yii\filters\auth\HttpBasicAuth 使用账号密码实现登陆状态

//yii\filters\auth\QueryParamAuth 使用url传递access-token

//yii\filters\auth\HttpHeaderAuth 使用http header里X-Api-Key字段传token

'class' => HttpHeaderAuth::class,

//access-token 部分接口需要验证,需要排除比如 login register 这样的接口

'optional' => ['register', 'login', "index"],

],

];

//...

}

4.在后台用户模型类里实现yii\web\IdentityInterface认证接口

use yii\db\ActiveRecord;

use yii\web\IdentityInterface;

class Admin extends ActiveRecord implements IdentityInterface

{

/**

* 根据给到的ID查询身份。

*

* @param string|integer $id 被查询的ID

* @return IdentityInterface|null 通过ID匹配到的身份对象

*/

public static function findIdentity($id)

{

return static::findOne($id);

}

/**

* 根据 token 查询身份。

*yii\filters\auth\HttpHeaderAuth获取到的token后会调用此函数

* @param string $token 被查询的 token

* @return IdentityInterface|null 通过 token 得到的身份对象

*/

public static function findIdentityByAccessToken($token, $type = null)

{

return static::findOne(['access_token' => $token]);

}

/**

* @return int|string 当前用户ID

*/

public function getId()

{

return $this->id;

}

/**

* 如果是token登录此函数体留空

* @return string 当前用户的(cookie)认证密钥

*/

public function getAuthKey()

{

return $this->auth_key;

}

/**

* 如果是token登录此函数体留空

* @param string $authKey

* @return boolean if auth key is valid for current user

*/

public function validateAuthKey($authKey)

{

return $this->getAuthKey() === $authKey;

}

}

5.角色,权限,分配

$auth = Yii::$app->authManager;

// 添加 "createPost" 权限

$createPost = $auth->createPermission('createPost');

$createPost->description = '创建文章';

$auth->add($createPost);

//添加admin用户组

$admin = $auth->createRole("admin");

$auth->add($admin);

//给角色role分配权限permission

$auth->addChild($admin,$createPost );

//给用户分配角色role

$auth->assign($admin, 用户id);

6.判断是否有权限

$controller = Yii::$app->controller->id;

$action = Yii::$app->controller->action->id;

//假如权限格式是 controller/action

if (Yii::$app->user->can($controller."/".$action)) {

echo "有权限";

} else {

echo "没权限";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值