在Yii 2.0中添加自定义RBAC规则到PhpManager


Add custom RBAC rule to PhpManager in Yii 2.0

我想添加一个自定义规则的PhpManager RBAC在Yii 2.0。

下面是自定义规则(@app/rbac/OwnerRule.php):
<?php
namespace app'rbac;
use yii'rbac'Rule;
/**
 * Checks if userID matches user passed via params
 */
class OwnerRule extends Rule
{
    public $name = 'isOwner';
    public function execute($user, $item, $params)
    {
        $access = false;
        if(isset($params['id'])){ 
            // My custom logic used to set $access
        }
        return $access;
    }
}

这是RBAC的层次结构文件(@app/data/RBAC .php)

<?php
use yii'rbac'Item;
return [
    'manageThing0' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
    'manageThing1' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
    'manageThing2' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
    // AND THE ROLES
    'guest' => [
        'type' => Item::TYPE_ROLE,
        'description' => 'Guest',
        'bizRule' => NULL,
        'data' => NULL
    ],
    'user' => [
        'type' => Item::TYPE_ROLE,
        'description' => 'User',
        'children' => [
            'guest',
            'manageThing0', // User can edit thing0
        ],
        'bizRule' => 'return !Yii::$app->user->isGuest;',
        'data' => NULL
    ],
    'moderator' => [
        'type' => Item::TYPE_ROLE,
        'description' => 'Moderator',
        'children' => [
            'user',         // Can manage all that user can
            'manageThing1', // and also thing1
        ],
        'bizRule' => NULL,
        'data' => NULL
    ],
    'admin' => [
        'type' => Item::TYPE_ROLE,
        'description' => 'Admin',
        'children' => [
            'moderator',    // can do all the stuff that moderator can
            'manageThing2', // and also manage thing2
        ],
        'bizRule' => NULL,
        'data' => NULL
    ],
]; 

如何在层次结构文件中使用自定义规则?

看这些链接希望你能找到你想要的,

http://www.yiiframework.com/doc - 2.0/-指导-安全- authorization.html

http://yii2-user.dmeroff.ru/docs/custom-access-control

基本yii2模板的RBAC