简体   繁体   English

Yii2 Rbac (DbManager)- 返回 null

[英]Yii2 Rbac (DbManager)- return null

Yii::$app->user->can('do_all') return null when i enter by my user (in db only 1 user). Yii::$app->user->can('do_all')当我的用户输入时返回 null(在 db 中只有 1 个用户)。 What i did wrong.我做错了什么。

1)- I created 4 tables. 1)- 我创建了 4 个表。

auth_asignment:  item_name => do_all, user_id => 1
auth_item:       name => do_all, type => 1
auth_item_child: empty
auth_rule:       empty

2)- I added in config/web.php 2)- 我在 config/web.php 中添加

'components' => [
    'authManager' => [
        'class' => 'yii\rbac\DbManager',
        'defaultRoles' => ['guest'],
    ]
]

3)- In controller i added 3)- 在控制器中我添加了

public function actionIndex()
{
    if(Yii::$app->user->can('do_all')){
        return $this->render('index');
    }
    else{
        throw new ForbiddenHttpException('The requested page is not exist.');
    }
}

I think you shuld add value in auth_item_child and organize the value this way我认为您应该在 auth_item_child 中添加价值并以这种方式组织价值

try adding a role ind this way尝试以这种方式添加角色

auth_item:        name => do_all_role, type => 1

auth_item_child   parent: do_all_role,  child : do_all

auth_asignment:  item_name => do_all_role, user_id => 1

and use (attention at the class name normally the User class name start with an Uppercase char)并使用(注意类名通常用户类名以大写字符开头)

 Yii::$app->User->can('do_all');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM