Multi-tenancy : Fixed Roles and Permissions #244
-
Hey all, This question dangles somewhere between Multi-tenancy and Spatie's roles and permissions. So the idea is to create a multi-tenant application, this application has FIXED roles and permissions for users. And i want them to be "synced" over all tenants. At first i wast thinking about seeding force seeding the new roles permissions on production but that doesn't seem like a smart approach at all. My second approach would be to create migrations that manually insert roles and permissions, or revoke delete in the up and down of the migration files. This looks like a better approach then seeding, yet the problem might arise for new tenant apps since all the migrations are done in batch 1 so Third approach would be to place all roles and permissions in the Landlord Database, and keeping those up to date with migrations. But i have no clue how to split up everything and dealing with the foreign keys. Plan would be in the following landlord db tables: tenant db tables: But the problem is the Roles foreign key, i have no idea how to separate these over two databases and letting them work together. Still kinda stuck in the concept thinking phase . And i'm fairly new to Laravel as well, coming from codeigniter 3 :) Any advice, tips about approach and how to set it up? My preference would be option 3 but not sure if it's a valid approach. Thanks in advance for your feedback and insights I got a little further in Third approach but kinda hit a brick wall here... So what i did was: Split up migration files -> one for landlord :
And one for the tenants:
Migration was a great success, added some roles and permissions in landlord database, and added role to a user in the tenant. The relationship is a success and works. Now i've extended the Spatie's Role and Permission model in order to use landlord connection: TenantRole :
TenantPermission :
Oke everything booted, but i had an error on the permissions guard, DAMN! I disabled cache for testing purpose ... nothing... alright then moving on... In my view file i tried to check permissions for the logged in user:
And here is where it all broke down :( This is the error i got: It looks like he's looking for the model_has_permissions table in the landlord database instead of the tenant database, if i remove the So anyone know how i can separate those two things ? since i only have 2 models to work with form spatie package : Role & Permission. It works like 80% but that extra 20% is gonna whomp me into oblivion isn't it ? Anyway thanks for your feedback |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
If all tenants use the same roles and permissions, my suggestion is to change only the relations with the pivot table.
In models that uses roles and permissions, override the public function roles()
{
public function roles(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
'role_id'
)
->using(ModelHasRole::class);
}
} I haven't tried the code, but it should work. |
Beta Was this translation helpful? Give feedback.
-
Do you have attached a role or permission to the user? |
Beta Was this translation helpful? Give feedback.
-
@masterix21 When i write my queries manually in Sequel PRO i get results: I think it's somekind of Laravel issue on belongstomany: What ever i throw at it it keeps on looking in the current connection: fs_rp_mt_tenant_01.tenant_roles SQLSTATE[42S02]: Base table or view not found: 1146 Table 'fs_rp_mt_tenant_01.tenant_roles' doesn't exist (SQL: select |
Beta Was this translation helpful? Give feedback.
@masterix21 When i write my queries manually in Sequel PRO i get results:
I think it's somekind of Laravel issue on belongstomany:
What ever i throw at it it keeps on looking in the current connection:
fs_rp_mt_tenant_01.tenant_roles
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'fs_rp_mt_tenant_01.tenant_roles' doesn't exist (SQL: select
tenant_roles
.*,model_has_roles
.role_id
aspivot_role_id
,model_has_roles
.model_id
aspivot_model_id
fromtenant_roles
inner joinmodel_has_roles
ontenant_roles
.id
=model_has_roles
.model_id
wheremodel_has_roles
.role_id
= 1