Reputation: 2305
I have a role called 'new'. While I don't explicitly set it, I want it to be the default role as soon as a user registers. I am using yii2-user and yii2-rbac extension by dektrium.
I have the following in my components...
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['new'],
],
However, the following returns false...
if (Yii::$app->user->can('new'))
And the following returns an empty array too...
print_r(Yii::$app->authManager->getRolesByUser( Yii::$app->user->identity->id ));
I am not sure where to look. I double checked and see the 'new' role properly set up in the RBAC configuration views.
Due to some reason, either Yii2 is not respecting my defaultRole config, or my expectation is wrong from that setting. Appreciate any help.
Upvotes: 2
Views: 443
Reputation: 33548
This:
Yii::$app->user->can('new')
should work (should return true
), make sure you have not overrided defaultRoles
somewhere else in application config (this is common mistake especially in advanced app).
I'd recommend check what Yii::$app->authManager->defaultRoles
contains right before it. Seems like there are no your added new
role.
In another words, RBAC respect defaultRoles
option, so it's your mistake.
Also note that this:
Yii::$app->authManager->getRolesByUser($userId);
returns all roles without default. This is by design.
However
Yii::$app->authManager->getRoles();
returns all roles including default.
Upvotes: 0