Created
July 28, 2023 21:38
-
-
Save lucacastelnuovo/c25416192a554b5b923d305856c35e38 to your computer and use it in GitHub Desktop.
Filament V3 multi-tenancy & laravel-permission role helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Support; | |
use App\Models\Tenant; | |
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | |
use Spatie\Permission\Traits\HasRoles as SpatieHasRoles; | |
trait HasRoles | |
{ | |
use SpatieHasRoles; | |
public function rolesOnTenant(Tenant $tenant): BelongsToMany | |
{ | |
$originalTenantId = getPermissionsTeamId(); | |
setPermissionsTeamId($tenant); | |
$roles = $this->roles(); | |
setPermissionsTeamId($originalTenantId); | |
return $roles; | |
} | |
public function syncRolesOnTenant( | |
Tenant $tenant, | |
array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection|string|int $roles | |
): self { | |
$originalTenantId = getPermissionsTeamId(); | |
setPermissionsTeamId($tenant); | |
$this->syncRoles($roles); | |
setPermissionsTeamId($originalTenantId); | |
return $this; | |
} | |
public function assignRoleOnTenant( | |
Tenant $tenant, | |
array|string|int|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $role | |
): self { | |
$originalTenantId = getPermissionsTeamId(); | |
setPermissionsTeamId($tenant); | |
$this->assignRole($role); | |
setPermissionsTeamId($originalTenantId); | |
return $this; | |
} | |
} |
Hi @bilogic,
- This is not so much a replacement, it is more some extended functionality of spatie/laravel-permissions.
- To see how
setPermisisonsTeamId
works, have a look at this: https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions - I do not understand this question, any part of your code can call this method.
ok thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi
setPermissionsTeamId
accept any model as long as it represents a tenant?rolesOnTenant
?thanks!