devzakir
devzakir

Reputation: 407

How do you setup permission/views/routes for different user roles?

I am building a eCommerce platform. Where I have to make several user roles and specific permission for them. I have successfully created admin and default user roles and permission.

But I am getting so much trouble to show the views and menu items based on other user roles like Editor/Manager/CS Team.

I tried to do using different middleware for every one of them. But It's not working efficiently and even at all.

For the Admin role, I created a Admin Middleware where I am checking user role type and giving access. I defined the admin middleware on Route gruop.

Can you suggest me? - how to setup permission/views/menu items efficiently for different user roles?

Thanks in Advance.

Note: I am trying to do it without any package.

admin middleware, ignore the read underline

web.php route group for admin

Upvotes: 0

Views: 1257

Answers (2)

Akash Bhalani
Akash Bhalani

Reputation: 192

You need to define policy. Policies are a great way to protect actions on your Eloquent Model. Laravel Policies are the part of Laravel Authorization that helps you to protect resources from unauthorized access.

Please refer this documentation to how to register policy and how it works in views file:

https://www.larashout.com/laravel-policies-controlling-authorization-in-laravel

Upvotes: 3

BlackXero
BlackXero

Reputation: 880

Yes you can make your own custom build library by setting roles,permission table in database and as soon as the user log's in you put all that information in session or cache. But doing so might get you in trouble in future coz lack of testing it's all feature, You have to be sure what exactly you are doing to manage it by yourself or else you can use already tested many time library like

laravel-permission Using a well known and trusted library ensures that it will solve your problem, but take your time to read it's documentation and analyse if it contains all features that you want in your application.

Upvotes: 4

Related Questions