yii2-usuario icon indicating copy to clipboard operation
yii2-usuario copied to clipboard

Check accessibility of routes by role

Open kwazaro opened this issue 7 years ago • 6 comments

What steps will reproduce the problem?

  1. Create some roles ('admin', 'editor', 'author' etc);
  2. Add yii\widgets\Menu;
  3. Configure items array for Menu (e.g. ['label' => 'Posts', 'url' => ['/post/edit', 'id' => 25]])

What is the expected result?

It would be better, if Menu will show only items, which are accessible by current user's role. Maybe there is some Helper or Filter, to process items array, before adding it to Menu configuration?

What do you get instead?

How to achieve this?

kwazaro avatar Oct 10 '18 10:10 kwazaro

'visible' => Yii::$app->user->can("admin")

https://www.yiiframework.com/doc/api/2.0/yii-web-user#can()-detail

maxxer avatar Oct 10 '18 12:10 maxxer

https://www.yiiframework.com/doc/api/2.0/yii-widgets-menu#$items-detail

maxxer avatar Oct 10 '18 12:10 maxxer

Hey, I know about this. This is not what I mean. There should be some helper function in component, to check, if ROUTE is accessible by current user. Something like User::canRoute(['/post/edit']) which returns TRUE if current user can access post/edit, or FALSE, if not. I can't use 'visible' property of Menu widget items, I need Yii do it automatically, based on RBAC. Check the webvimark/user-management and it's canRoute() method. Not everytime we can use conditions for widget configurations, in my case, I can pass to Menu widget only LABEL and URL for items, and I need Yii to check URL (route) for access by RBAC

kwazaro avatar Oct 10 '18 12:10 kwazaro

Thanks for clarification.

If canRoute return false? What do you get in the url?

maxxer avatar Oct 10 '18 13:10 maxxer

canRoute() can be used not only for Menu widget, it can be useful to determine, which routes are accessible for current user. Menu widget is just for example.

kwazaro avatar Oct 10 '18 13:10 kwazaro

@kwazaro do you mind proposing a pull request?

tonydspaniard avatar Jun 29 '19 06:06 tonydspaniard