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.
No description
20 Replies
awcodes
awcodes5mo ago
Did you follow all the setup for teams inside the spatie package. It should be able to handle that.
mohdaftab
mohdaftabOP5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
but I did follow the manual for team configuration from this page: https://spatie.be/docs/laravel-permission/v6/installation-laravel
mohdaftab
mohdaftabOP5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
this is what happens when I try to click save changes on this form.
No description
mohdaftab
mohdaftabOP5mo ago
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.
awcodes
awcodes5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
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.
awcodes
awcodes5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
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
awcodes
awcodes5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
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.
awcodes
awcodes5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
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
awcodes
awcodes5mo ago
If you are using filament panels then they are scoped to the current tenant. Maybe that is creating the issue.
mohdaftab
mohdaftabOP5mo ago
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.
awcodes
awcodes5mo ago
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.
mohdaftab
mohdaftabOP5mo ago
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.
Povilas K
Povilas K5mo ago
@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.
mohdaftab
mohdaftabOP5mo ago
Thank you so much for the reply 🙂 no problem, I will add custom actions to do this for now.
Want results from more Discord servers?
Add your server