【问题标题】:yii2 rbac authmanager getRoles() return emptyyii2 rbac authmanager getRoles() 返回空
【发布时间】:2016-07-01 11:21:09
【问题描述】:

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

规则类,我放置实际规则逻辑的地方。

yii/console/controller/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 来生成规则文件:

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

这与文档基本相同

yii/commom/config/main.php

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

但是在

前端\模型\SignupForm::signup()

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

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
)

【问题讨论】:

    标签: php yii2 rbac


    【解决方案1】:

    这可能是因为您在“console/rbac/items.php 和 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/

    这应该有帮助,如果还有问题请告诉我

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 2015-08-09
      • 1970-01-01
      • 2016-06-09
      • 2016-08-26
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多