-1

Yii::$app->user->can('do_all') return null when i enter by my user (in db only 1 user). What i did wrong.

1)- I created 4 tables.

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

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

3)- In controller i added

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

1 Answers1

1

I think you shuld add value in auth_item_child and organize the value this way

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');
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thank you. Your updated answer give me some ideas- the problem is, that i use just installed yii2 and my loginned user is a user by default- he doesn't in the database- he is in the class User and has id 100. And i have also a user in the db (with id 1) but this user's table doesn't included in the application, this is the other user and he has id 1. I changed id in the class from 100 to 1 and it begin to work. – user5538720 Nov 14 '15 at 14:08