Closed BartMommens closed 3 years ago
I've found a solution to make this work with a special thanks to @masterix21, who pointed me in the correct direction. And has been a great help on the Spatie Multi-tenancy package.
config\permission.php
'models' => [
'permission' => App\Models\TenantPermission::class,
'role' => App\Models\TenantRole::class,
],
New Role model,
App\Models\TenantRole.php:
use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;
use Spatie\Permission\Models\Role as BaseRole;
class TenantRole extends BaseRole
{
use UsesLandlordConnection;
}
New Permission model
App\Models\TenantPermission.php:
namespace App\Models;
use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;
use Spatie\Permission\Models\Permission as BasePermission;
class TenantPermission extends BasePermission
{
use UsesLandlordConnection;
}
App\Models\User.php :
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use UsesTenantConnection, HasRoles;
/**
* A model may have multiple roles.
*/
public function roles (): BelongsToMany {
return $this->belongsToMany(
config('permission.models.role'), //This model uses the LandlordConnection
Tenant::current()->getDatabaseName().'.'.config('permission.table_names.model_has_roles'), //Just inserted the tenant DB name here
'role_id',
'model_id'
);
}
/**
* A model may have multiple direct permissions.
*/
public function permissions(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.permission'),
'model',
Tenant::current()->getDatabaseName().'.'.config('permission.table_names.model_has_permissions'),
config('permission.column_names.model_morph_key'),
'permission_id'
);
}
}
I hope other people can use this method and implement it
Originally posted by @BartMommens in https://github.com/spatie/laravel-multitenancy/discussions/244#discussioncomment-767957
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
migrate:rollback
is a bump on the road.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: permissions, roles_has_permissions
tenant db tables: roles
...
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.
Landlord tables:
Tenant Tables:
Relations:
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
use UsesLandlordConnection;
from the models it's even deader than dead and he's now looking in the Tenant DB.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 ?
Or should i find a different approach and abandon this road?
Anyway thanks for your feedback
Originally posted by @BartMommens in https://github.com/spatie/laravel-multitenancy/discussions/244