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
Sjoerd2411mo 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
HegabovicOP11mo ago
yeah me too same as you 😄 with filament shield and spatie roles and permissions 😄 keep me updated with your news 😄
Sjoerd24
Sjoerd2411mo 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
HegabovicOP11mo ago
Can you share knowleage , my problems are with modifying relations
Sjoerd24
Sjoerd2411mo ago
What kind of relations? In a relationmanager? I didnt tested that yet
Hegabovic
HegabovicOP11mo ago
no no spatie permisions and roles , filament shield both of these
Sjoerd24
Sjoerd2411mo 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
Sjoerd2411mo 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
HegabovicOP11mo 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' => '[email protected]'],
[
'name' => 'admin',
'email' => '[email protected]',
'password' => Hash::make('[email protected]'),
'code' => $code,
'created_at' => now(),
'updated_at' => now(),
]
);

$user = User::where('email', '[email protected]')->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' => '[email protected]'],
[
'name' => 'admin',
'email' => '[email protected]',
'password' => Hash::make('[email protected]'),
'code' => $code,
'created_at' => now(),
'updated_at' => now(),
]
);

$user = User::where('email', '[email protected]')->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
Sjoerd2411mo 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
HegabovicOP11mo ago
no in docs said leave the roles without assigned clinic id to be global with all team
Hegabovic
HegabovicOP11mo ago
see this roles in yellow
No description
Hegabovic
HegabovicOP11mo ago
it should by default apear in each clinic registered but still it doesn't so all system is down
Hegabovic
HegabovicOP11mo ago
see
No description
Sjoerd24
Sjoerd2411mo 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
HegabovicOP11mo ago
yeah this logic is not working
Want results from more Discord servers?
Add your server