[英]Yii2 auth_rule usage (Yii2 RBAC)
我正在使用Yii2
基本模板並處理auth_manager
。
我已在以下鏈接中閱讀了此內容:
我也檢查過這個
我們如何在 Yii2 RBAC 中使用 auth_rule 表?
現在我知道這些事情:
如何分配用戶角色。
如何為任何角色分配權限。
我已經了解3 tables
的用法。 即auth_assignment
、 auth_item
和auth_item_child
。
現在我沒有得到auth_rule
表的用法。
表中有 4 列
name
data
created_at
updated_at
現在我很好奇
我需要在這些列中存儲什么?
我以后如何使用這些數據?
我的意思是很容易理解只有兩列,即created_at
和updated_at
,但是name
和data
列中的內容。
我無法通過網絡找到任何相關信息。 因此,如果有人知道這一點,那將對我和那些也在尋找同樣東西的人非常有幫助。
謝謝你
我推薦閱讀這個: http ://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules
您(可能)不想手動向該表添加數據。 name
字段將包含規則的名稱,這也是auth_item
表中的外鍵。 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;
}
}
auth 項和規則使用 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 時,表中的數據會自動填充正確的值。
它是存儲對 auth 規則類的引用的地方。 http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules
有 2 個示例PhpManager
和DbManager
如果您將配置從PHP
更改為DB
,它將如下所示。
return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
// uncomment if you want to cache RBAC items hierarchy
// 'cache' => 'cache',
],
// ...
],
];
DbManager
使用四個數據庫表來存儲其數據:
itemTable
:存放授權項的表。 默認為“auth_item”。itemChildTable
:存儲授權項層次結構的表。 默認為“auth_item_child”。assignmentTable
:用於存儲授權項分配的表。 默認為“auth_assignment”。ruleTable
:存放規則的表。 默認為“auth_rule”。 你不需要在這里存儲任何東西,而不是直接使用它。 它在使用您用於PHP manager
的相同方法期間被填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.