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
Sjoerd24
Sjoerd246mo ago
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🥲
Hegabovic
Hegabovic6mo ago
yeah me too same as you 😄 with filament shield and spatie roles and permissions 😄 keep me updated with your news 😄
Sjoerd24
Sjoerd246mo ago
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.
Hegabovic
Hegabovic6mo ago
Can you share knowleage , my problems are with modifying relations
Sjoerd24
Sjoerd246mo ago
What kind of relations? In a relationmanager? I didnt tested that yet
Hegabovic
Hegabovic6mo ago
no no spatie permisions and roles , filament shield both of these
Sjoerd24
Sjoerd246mo ago
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); } }'''
Sjoerd24
Sjoerd246mo ago
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 !
Hegabovic
Hegabovic6mo ago
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
public function run(): void
{
$codeGenerator = new CodeGenerator();
$code = $codeGenerator->setModel(User::class)->create();

$user = User::updateOrInsert(
['email' => 'admin@admin.com'],
[
'name' => 'admin',
'email' => 'admin@admin.com',
'password' => Hash::make('admin@admin.com'),
'code' => $code,
'created_at' => now(),
'updated_at' => now(),
]
);

$user = User::where('email', 'admin@admin.com')->first();

if ($user) {
// Find or create the clinic
$clinic = Clinic::firstOrCreate([
// Add any necessary clinic creation logic here
'name' => 'Clinic 1',
'slug' => 'clinic-1',
'created_at' => now(),
'updated_at' => now(),
]);
$user->clinics()->syncWithoutDetaching([$clinic->id]);
$user->assignRole('super_admin');
dd($user);
}

$this->command->info('Super admin seeder added.');
}
public function run(): void
{
$codeGenerator = new CodeGenerator();
$code = $codeGenerator->setModel(User::class)->create();

$user = User::updateOrInsert(
['email' => 'admin@admin.com'],
[
'name' => 'admin',
'email' => 'admin@admin.com',
'password' => Hash::make('admin@admin.com'),
'code' => $code,
'created_at' => now(),
'updated_at' => now(),
]
);

$user = User::where('email', 'admin@admin.com')->first();

if ($user) {
// Find or create the clinic
$clinic = Clinic::firstOrCreate([
// Add any necessary clinic creation logic here
'name' => 'Clinic 1',
'slug' => 'clinic-1',
'created_at' => now(),
'updated_at' => now(),
]);
$user->clinics()->syncWithoutDetaching([$clinic->id]);
$user->assignRole('super_admin');
dd($user);
}

$this->command->info('Super admin seeder added.');
}
Sjoerd24
Sjoerd246mo ago
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?
Hegabovic
Hegabovic6mo ago
no in docs said leave the roles without assigned clinic id to be global with all team
Hegabovic
Hegabovic6mo ago
see this roles in yellow
No description
Hegabovic
Hegabovic6mo ago
it should by default apear in each clinic registered but still it doesn't so all system is down
Hegabovic
Hegabovic6mo ago
see
No description
Sjoerd24
Sjoerd246mo ago
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
Hegabovic
Hegabovic6mo ago
yeah this logic is not working
Want results from more Discord servers?
Add your server
More Posts