Yii2身份验证规则用法(Yii2 RBAC)
我正在使用Yii2身份验证规则用法(Yii2 RBAC),yii2,yii2-advanced-app,yii2-basic-app,yii2-user,yii2-rbac,Yii2,Yii2 Advanced App,Yii2 Basic App,Yii2 User,Yii2 Rbac,我正在使用Yii2基本模板和auth\u manager 我已在以下链接中了解到这一点: 我也检查过了 现在我知道了这些事情: 如何分配用户角色 如何为任何角色分配权限 我已经了解了3个表的用法。i、 e.auth_赋值,auth_项和auth_项子项 现在,我没有得到auth_rule表的用法 表中有4列 name 数据 在 更新时间 现在我很好奇 我需要在这些列中存储什么 我以后如何使用这些数据 我的意思是很容易理解只有两列,即在创建的列和在更新的列,但是名称和数据列中的内容 我在网上找
Yii2
基本模板和auth\u manager
我已在以下链接中了解到这一点:
我也检查过了
现在我知道了这些事情:
3个表的用法
。i、 e.auth_赋值
,auth_项
和auth_项子项
auth_rule
表的用法
表中有4列
name
数据
在
更新时间
现在我很好奇
在
创建的列和在
更新的列,但是名称
和数据
列中的内容
我在网上找不到任何关于它的信息。所以,如果有人知道这一点,这将是非常有帮助的,对我和他们谁也在寻找同样的东西
谢谢我建议您阅读以下内容: 您(可能)不想手动将数据添加到此表中。
name
字段将包含规则的名称,这也是auth\u项
表中的外键。data
列将包含实际为规则的类的序列化版本
因此,在文档中的示例中,有一个AuthorRule:
namespace app\rbac;
use yii\rbac\Rule;
use app\models\Post;
class AuthorRule extends Rule
{
public $name = 'isAuthor';
public function execute($user, $item, $params)
{
return isset($params['post']) ? $params['post']->createdBy == $user : false;
}
}
使用authManager组件连接验证项目和规则:
$auth = Yii::$app->authManager;
// add the rule
$rule = new \app\rbac\AuthorRule;
$auth->add($rule);
// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission('updateOwnPost');
$updateOwnPost->description = 'Update own post';
$updateOwnPost->ruleName = $rule->name;
$auth->add($updateOwnPost);
// "updateOwnPost" will be used from "updatePost"
$auth->addChild($updateOwnPost, $updatePost);
// allow "author" to update their own posts
$auth->addChild($author, $updateOwnPost);
当使用DbManager时,带有的表中的数据将自动填充为正确的值。它存储对身份验证规则类的引用 有两个示例
PhpManager
和DbManager
,如果您将配置从PHP
更改为DB
,如下所示
return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
// uncomment if you want to cache RBAC items hierarchy
// 'cache' => 'cache',
],
// ...
],
];
DbManager
使用四个数据库表存储其数据:
:用于存储授权项的表。默认为“auth_item”itemTable
:用于存储授权项层次结构的表。默认为“auth_item_child”itemChildTable
:用于存储授权项分配的表。默认为“授权分配”assignmentTable
:用于存储规则的表。默认为“授权规则”ruleTable