簡體   English   中英

Yii2 auth_rule 用法(Yii2 RBAC)

[英]Yii2 auth_rule usage (Yii2 RBAC)

我正在使用Yii2基本模板並處理auth_manager

我已在以下鏈接中閱讀了此內容:

Yii2授權

我也檢查過這個

我們如何在 Yii2 RBAC 中使用 auth_rule 表?

現在我知道這些事情:

  1. 如何分配用戶角色。

  2. 如何為任何角色分配權限。

  3. 我已經了解3 tables的用法。 auth_assignmentauth_itemauth_item_child

現在我沒有得到auth_rule表的用法。

表中有 4 列

name

data

created_at

updated_at

現在我很好奇

  1. 我需要在這些列中存儲什么?

  2. 我以后如何使用這些數據?

我的意思是很容易理解只有兩列,即created_atupdated_at ,但是namedata列中的內容。

我無法通過網絡找到任何相關信息。 因此,如果有人知道這一點,那將對我和那些也在尋找同樣東西的人非常有幫助。

謝謝你

我推薦閱讀這個: 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 個示例PhpManagerDbManager如果您將配置從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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM