Home > database >  Can we use laravel-permissions for multi tenant application?
Can we use laravel-permissions for multi tenant application?

Time:09-26

I am creating one multi-tenant app in Laravel with Single Database and thinking to use laravel-permission package by spatie.

My Requirement is pretty straightforward, I want my tenants to create their own Roles, whereas permissions will be managed by Super Admin only.

My problem is when I was trying using, It worked for 1st client but 2nd time it gives error:

A role 'Admin' already exists for guard 'admin'.

As I mentioned client can create roles, so they can crate duplicate roles.

Please recommend better approach or package or should I try writing custom code.

Any help appreciated!

CodePudding user response:

Because the name is indexed in the role table, you cannot create a duplicate role name, I ask you not to change the package, but in case you have 2 ways to handle this

1- unindex the name collemn or disable unique feature, and add you tenant id to table so by do this you can manage and get right role for each tenant

2- add another table to manage your sub role (tenant role) and connect you sub role with master role by id

CodePudding user response:

I would consider using a hidden prefix: on the roles and permissions that would scope them to a particular tenant. So for example:

Roles

system:admin
tenant_a:admin
tenant_b:admin

Permissions

system:creates-roles
system:reads-roles
tenant_a:creates-roles
tenant_a:reads-roles

The prefix would not be assignable by a Tenant, the system would automatically assign that based on the User. However, if you're a System Admin (i.e. Super Admin) then you could create/view/assign a prefix in order to manage the roles and permissions.

This would require you to write some custom logic for handling the prefix, however, it is pretty flexible (you could nest unlimited identifiers - a:b:c:d:e etc.) and doesn't require you to go messing with any underlying packages (i.e. laravel-permissions).

  • Related