Yii Framework Forum: Yii2: authManager should have a method like userHasRole() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Yii2: authManager should have a method like userHasRole() Rate Topic: -----

#1 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

Posted 21 November 2014 - 03:18 AM

Sometime I just want to know if a user has a certain role or not. Especially for the current authenticated user.

One can use Yii::$app->authManager->getRolesByUser($user_id) to get the roles and then loop through them to find out if the user has got the role. But it could already part of the BaseManager (abstract or base implementation, and finally implemented in PhpManager and DbManager) with a method like userHasRole($user_id, $role) which returns true or false.

Even better would be Yii::$app->user->hasRole($role). Is this possible?

This could result in better performance - e.g. the DbManager could use better queries.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,193
  • Joined: 17-January 09
  • Location:Russia

Posted 23 November 2014 - 12:17 PM

Yes, it's just

Yii::$app->user->can($role);

Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

Posted 24 November 2014 - 10:45 AM

Okay, this already exists. Though, in my opionion 'can' is a not so obvious method name in context of roles.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,193
  • Joined: 17-January 09
  • Location:Russia

Posted 25 November 2014 - 08:46 AM

Yeah but checking for role and not specific permission isn't common either.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#5 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

Posted 26 November 2014 - 02:46 AM

Ah, I see. Thanks for you hint. I'm not that experienced with RBAC. I thought it would be okay, but most likely you're right.

Is this documented in the Yii Guide or anywhere else? Do you have a link? I think the Yii Guide just says that you can use roles and permissions but does not suggest how to do it or what is best practice.
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,193
  • Joined: 17-January 09
  • Location:Russia

Posted 29 November 2014 - 04:01 AM

http://www.yiiframew...horization.html
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users