filament spatie roles and permissions
i'm trying to prefrom a multi team sudin filament tenancy but filament roles and permission is givving me hard time , have anyone tried it before ?
16 Replies
I am currently also busy with it (in combination with shield) but so far didn’t figure it out. Think I will post a guide if I succeed because more people are struggling. If i figure it out🥲
yeah me too same as you 😄 with filament shield and spatie roles and permissions 😄
keep me updated with your news 😄
Ok I think I just figured it out. Have to see where I can best write down how to do it. It is not terribly complicated but i made a lot of dumb mistakes while trying to get it to work.
Can you share knowleage , my problems are with modifying relations
What kind of relations? In a relationmanager? I didnt tested that yet
no no spatie permisions and roles , filament shield both of these
ok so in short:
Step 1: you first modify permission (config file) and make sure this is set:
'teams' => true,
AND make sure your team_id is changed it whatever you use. (with me its clinic_id)
Step 2: Then you have to rerun the migration for spatie permissions OR make a separate migration (https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions).
Step 3: make a new middleware (i named mine SyncSpatiePermissionsWithFilamentTenants). Mine has this content:
namespace App\Http\Middleware;
use App\Models\Clinic;
use Closure;
use Filament\Facades\Filament;
use Illuminate\Http\Request;
class SyncSpatiePermissionsWithFilamentTenants
{
public function handle(Request $request, Closure $next)
{
if(!empty(auth()->user()) && Filament::getTenant()) {
// check if user changed tenant $filament = Filament::getTenant()->id; $spatie = getPermissionsTeamId(); if ($filament !== $spatie) { setPermissionsTeamId($filament); auth()->user()->unsetRelation('roles')->unsetRelation('permissions'); } } return $next($request); } }'''
// check if user changed tenant $filament = Filament::getTenant()->id; $spatie = getPermissionsTeamId(); if ($filament !== $spatie) { setPermissionsTeamId($filament); auth()->user()->unsetRelation('roles')->unsetRelation('permissions'); } } return $next($request); } }'''
Introduction | laravel-permission
laravel-permission
Step 4: register this middleware on your tenant page like this:
->tenantMiddleware([
ApplyTenantScopes::class,
SyncSpatiePermissionsWithFilamentTenants::class,
], isPersistent: true)
now make sure you have correct records for your own account (my dumb mistake), you can check with debugbar if the queries being made are correct. And make sure your canAccessPanel() is set up correctly, also I mistake i made.
hope it helps you !
still not working :((
when seeding a user
$user->assignRole('super_admin');
it give this error
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'clinic_id' cannot be null (Connection: mysql, SQL: insert into
model_has_roles
(clinic_id
, model_id
, model_type
, role_id
) values (?, 1, Hegabovic\UserManagement\Models\User, 1))
here is the super admin seeder
try to add this:
setPermissionsTeamId($clinic->id);
So like this:
$user->clinics()->syncWithoutDetaching([$clinic->id]);
setPermissionsTeamId($clinic->id);
$user->assignRole('super_admin');
Does that work?
no in docs said leave the roles without assigned clinic id to be global with all team
see this roles in yellow
it should by default apear in each clinic registered
but still it doesn't so all system is down
see
What I understood is something different, if you register roles with clinic_id NULL then you can USE it on every clinic. They only show up if they are linked to an clinic (set with correct ID). You can also make a special role for JUST one clinic by limiting the role with a clinic_id. Then only THAT clinic can use that specific role. Thats also how it seems to work in practise
yeah this logic is not working