Laravel-permission: Roles and Ownership of Records

Created on 6 Jun 2018  路  5Comments  路  Source: spatie/laravel-permission

Let's say I've multiple users - Admin, Manager, User. User can CRUD records owned by him. Manager can CRUD records owned by him and his Users. Admin can CRUD records of all. Every record has owner_id column which stores the user_id. How to achieve this using this package?

question

All 5 comments

That can be done generally with Laravel's policy functionality: first define all the CRUD operation policies, and then augment them with role/permission logic.

An example of adding basic Policy support to an app can be seen here: https://github.com/drbyte/spatie-permissions-demo/pull/2/files

@drbyte thanks for the nice response with perfect example. One thing missing is the middleman (Manager). The example clearly outlines the Admin and User roles. Can you throw some light, if Manager comes in between, what need to be done?

You will need to define in your app what a Manager means. That includes determining how you implement the idea of a "manager has users". That might include some polymorphic relationships; perhaps some belongsToManyThrough relations defined on the models, and then enforced with your defined policies.

@drbyte Maybe this question doesn't belong here, but I'm taking the liberty to ask. Based on your suggestion, I've gone through the 'Has Many Through' relationship and it will perfectly suit the requirement. But it has support only for one intermediary. What if there are multiple intermediaries (managers), what is the best possible way?

Closing this for now.
If there's a need for multiple intermediary discussion affecting this package, we can start another issue for that ... preferably with code samples to start from.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

enghelewa picture enghelewa  路  4Comments

younus93 picture younus93  路  4Comments

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments

ionesculiviucristian picture ionesculiviucristian  路  4Comments

ergonomicus picture ergonomicus  路  3Comments