yii2 rbac authmanager getRoles()返回空

时间:2016-07-01 11:21:10

标签: php yii2 rbac

我正在使用yii2实现rbac。但是当我尝试获取我之前创建的角色时,我得到一个空变量:$authorRole = $auth->getRole('admin');

规则类,我在其中放置了实际的规则逻辑。

  

YII /控制台/控制器/ UserGroupRule.php

namespace app\rbac;

use Yii;
use yii\rbac\Rule;

/**
 * Checks if user group matches
 */
class UserGroupRule extends Rule
{
    public $name = 'userGroup';

    public function execute($user, $item, $params)
    {
        if (!Yii::$app->user->isGuest) {
            $group = Yii::$app->user->identity->group;
            if ($item->name === 'admin') {
                return $group == 1;
            } elseif ($item->name === 'author') {
                return $group == 1 || $group == 2;
            }
        }
        return false;
    }
}

现在定义角色..

yii/console/controller/RbacController.php
namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        $rule = new \app\rbac\UserGroupRule;
        $auth->add($rule);

        $admin = $auth->createRole('admin');
        $admin->ruleName = $rule->name;
        $auth->add($admin);

    }
}

在此之后,我能够运行./yii rbac / init来生成规则文件:

  • console / rbac / items.php
  • 控制台/ RBAC / rules.php

这与文档

大致相同
  

YII /黎民/配置/ main.php

'authManager' => [
    'class' => 'yii\rbac\PhpManager',
    'defaultRoles' => ['admin', 'author'], // your define roles
],  

但是在

  

前端\模型\ SignupForm ::注册()

当我尝试获得管理员角色时,我得到一个空的结果:

public function signup()
{
    if ($this->validate()) {
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->save(false);

        $auth = Yii::$app->authManager;
        $authorRole = $auth->getRole('admin');
        $auth->assign($authorRole, $user->getId());

        return $user;
    }

    return null;
}

这是$ auth:

的值
yii\rbac\PhpManager#1
(
    [itemFile] => '/advanced/frontend/rbac/items.php'
    [assignmentFile] => '/advanced/frontend/rbac/assignments.php'
    [ruleFile] => '/advanced/frontend/rbac/rules.php'
    [*:items] => []
    [*:children] => []
    [*:assignments] => []
    [*:rules] => []
    [defaultRoles] => [
        0 => 'admin'
        1 => 'author'
        2 => 'admin'
        3 => 'author'
    ]
    [yii\base\Component:_events] => []
    [yii\base\Component:_behaviors] => null
)

1 个答案:

答案 0 :(得分:2)

这可能是因为你在“console / rbac / items.php和”中生成了rbac console / rbac / rules.php“但你的rbac PhpManager正在高级/前端查找这些文件

您可以移动此文件或设置正确的路径

'authManager' => [
    'class' => 'yii\rbac\PhpManager',
    'itemFile' => '@common/rbac/items.php',
    'assignmentFile' => '@common/rbac/assignments.php',
    'ruleFile' => '@common/rbac/rules.php',
    'defaultRoles' => ['admin', 'author'], // your define roles
],  

“@common”是yii2别名所有可用的别名:http://www.yiiframework.com/wiki/667/yii-2-list-of-path-aliases-available-with-default-basic-and-advanced-app/

这应该有所帮助,让我知道是否还会有问题