Skip to content

Commit

Permalink
Fix unique name for roles, scope to tenant disabling (#162)
Browse files Browse the repository at this point in the history
* Fix duplicate warning.

* Filament by default attaches to the pviot model and assigned the role to the tenant through the team relation. This allows changing that if you already have a custom global scope in place you don't need to scope the model role as you are using the user/customer

---------

Co-authored-by: tonypartridge <[email protected]>
  • Loading branch information
tonypartridge and tonypartridger authored Feb 6, 2024
1 parent 4b6f6cb commit c47812d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/filament-spatie-roles-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
'navigation_section_group' => 'filament-spatie-roles-permissions::filament-spatie.section.roles_and_permissions', // Default uses language constant

'team_model' => \App\Models\Team::class,

'scope_to_tenant' => true,

/*
* Set as false to remove from navigation.
Expand Down
19 changes: 17 additions & 2 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Validation\Rules\Unique;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class RoleResource extends Resource
{



public static function isScopedToTenant(): bool
{
return config('filament-spatie-roles-permissions.scope_to_tenant', true);
}

public static function getNavigationIcon(): ?string
{
return config('filament-spatie-roles-permissions.icons.role_navigation');
Expand Down Expand Up @@ -70,7 +77,15 @@ public static function form(Form $form): Form
->schema([
TextInput::make('name')
->label(__('filament-spatie-roles-permissions::filament-spatie.field.name'))
->required(),
->required()
->unique(ignoreRecord: true, modifyRuleUsing: function (Unique $rule) {
// If using teams and Tenancy, ensure uniqueness against current tenant
if(config('permission.teams', false) && Filament::hasTenancy()) {
// Check uniqueness against current user/team
$rule->where(config('permission.column_names.team_foreign_key', 'team_id'), Filament::getTenant()->id);
}
return $rule;
}),

Select::make('guard_name')
->label(__('filament-spatie-roles-permissions::filament-spatie.field.guard_name'))
Expand Down

0 comments on commit c47812d

Please sign in to comment.