Spatie Roles and Permissions package with Multi-Tenancy
Hi, I am building a multi-tenancy app for companies as tenants which has an admin panel separate from tenants panel.
I am using team = true in spatie roles and permissions and I have some set of roles with permissions which I want to assign to each user for different companies.
Like there is a user 1 who is a company admin in company A and moderator in company B, each user can have single role in a company but they can have different roles in different companies.
I am struggling to assign roles to the users as it requires a company_id to assign the role.
My question is how can I assign roles to the user for each company in the form attached as screenshot please?
Thank you so much in advance.
20 Replies
Did you follow all the setup for teams inside the spatie package. It should be able to handle that.
I used the default spatie roles and permissions package
I didn't use the plugin listed on Filamentphp plugins page as I didn't it existed.
but I did follow the manual for team configuration from this page: https://spatie.be/docs/laravel-permission/v6/installation-laravel
Installation in Laravel | laravel-permission
laravel-permission
Role::create(['name' => 'reader', 'team_id' => 1]);
I am not able to get the team_id as it is a Select multiple option in the form.
My question is how do I assign different roles to the same user for different companies please.
this is what happens when I try to click save changes on this form.
The form is only available in the Admin dashboard where an admin can add different users.
It should be working well on the company page as I have company_id available there.
But I need to make it available for admin also.
You can get the current tenant in filament with the Filament facade. Not saying it will work out of the box, but should be doable.
Yes that is for the tenant page itself.
The admin dashboard doesn't have tenants. I am just using it to add users and set roles for them as we are not allowing admin to access the tenants panel.
That’s tough to say and brought up a lot, but it stems from the point of wanting tenancy while not having tenancy in some situations, but that contradicts the point of tenancy.
Wish I had a better answer for you.
I read it on the help to separate the admin panel from the company/tenants panel so I followed that path.
and the admin is not using any spatie role as it just uses is_admin column in the users table
Right but I’m not sure spatie’s package has the concept of non tenant roles and permissions if teams is active. I could be wrong but I think it’s all or nothing.
this is exactly why I user is_admin for admin dashboard only so I don't have to provide the team_id for super admin role.
In that case you might need to provide a gate instead of permissions. Not 💯 sure how to apply that to panels in filament.
Spatie has a section in their docs for gates. Maybe start there.
That is all working well though.
I just want to assign the role to user for each company using the admin panel where I have multiple companies available for each user.
I think it can't be done on the same form so I will have to create a new resource to be able to assign roles
If you are using filament panels then they are scoped to the current tenant. Maybe that is creating the issue.
admin panel is not scoped to any tenant, it just has a drop down list of companies which has belongstomany relationship with the user.
user 1 > company 1 > admin
user 1 > company 2 > moderator
assigning roles on the tenant scoped panel is working great.
but we are not allowing the tenants to assign roles to any user, only admin can do that.
Ok, but with teams active in the spatie package I don’t think it can work without assigning a team, regardless of the panel being tenant aware or not.
I don’t think the spatie package has the awareness of maybe doesn’t have a team.
I understand, seems like I will have to add super admin to spatie roles and use Gate to be able to access each tenant and add users from there.
Thank you so much.
@PovilasKorop, please is it possible to make a tutorial on this issue where we can use admin panel to allocate multiple teams to a user with same or different roles for each team.
Sorry for tagging you but I thought it would be better to explain it in the comments section.
@mohdaftab sounds very complex and a lot of work to properly build so we postponed it and not many people would use it. But will bump it on my future to-do list when we have more time.
Thank you so much for the reply 🙂
no problem, I will add custom actions to do this for now.