diff --git a/config/filament-spatie-roles-permissions.php b/config/filament-spatie-roles-permissions.php index 40c6424..5f81860 100644 --- a/config/filament-spatie-roles-permissions.php +++ b/config/filament-spatie-roles-permissions.php @@ -57,6 +57,15 @@ ], ], + /** + * Set to true to display relation managers in the resources + */ + 'should_display_relation_managers' => [ + 'permissions' => true, + 'users' => true, + 'roles' => true, + ], + /* * If you want to place the Resource in a Cluster, then set the required Cluster class. * Eg. \App\Filament\Clusters\Cluster::class diff --git a/src/Resources/PermissionResource.php b/src/Resources/PermissionResource.php index da09eb5..1057be9 100644 --- a/src/Resources/PermissionResource.php +++ b/src/Resources/PermissionResource.php @@ -69,7 +69,7 @@ public static function getCluster(): ?string { return config('filament-spatie-roles-permissions.clusters.permissions', null); } - + public static function form(Form $form): Form { return $form @@ -84,9 +84,9 @@ public static function form(Form $form): Form ->label(__('filament-spatie-roles-permissions::filament-spatie.field.guard_name')) ->options(config('filament-spatie-roles-permissions.guard_names')) ->default(config('filament-spatie-roles-permissions.default_guard_name')) - ->visible(fn () => config('filament-spatie-roles-permissions.should_show_guard', true)) + ->visible(fn() => config('filament-spatie-roles-permissions.should_show_guard', true)) ->live() - ->afterStateUpdated(fn (Set $set) => $set('roles', null)) + ->afterStateUpdated(fn(Set $set) => $set('roles', null)) ->required(), Select::make('roles') ->multiple() @@ -94,11 +94,11 @@ public static function form(Form $form): Form ->relationship( name: 'roles', titleAttribute: 'name', - modifyQueryUsing: function(Builder $query, Get $get) { + modifyQueryUsing: function (Builder $query, Get $get) { if (!empty($get('guard_name'))) { $query->where('guard_name', $get('guard_name')); } - if(config('permission.teams', false) && Filament::hasTenancy()) { + if (config('permission.teams', false) && Filament::hasTenancy()) { return $query->where(config('permission.column_names.team_foreign_key'), Filament::getTenant()->id); } return $query; @@ -124,7 +124,7 @@ public static function table(Table $table): Table ->toggleable(isToggledHiddenByDefault: config('filament-spatie-roles-permissions.toggleable_guard_names.permissions.isToggledHiddenByDefault', true)) ->label(__('filament-spatie-roles-permissions::filament-spatie.field.guard_name')) ->searchable() - ->visible(fn () => config('filament-spatie-roles-permissions.should_show_guard', true)), + ->visible(fn() => config('filament-spatie-roles-permissions.should_show_guard', true)), ]) ->filters([ SelectFilter::make('models') @@ -173,7 +173,7 @@ public static function table(Table $table): Table ->label(__('filament-spatie-roles-permissions::filament-spatie.action.attach_to_roles')) ->action(function (Collection $records, array $data): void { Role::whereIn('id', $data['roles'])->each(function (Role $role) use ($records): void { - $records->each(fn (Permission $permission) => $role->givePermissionTo($permission)); + $records->each(fn(Permission $permission) => $role->givePermissionTo($permission)); }); }) ->form([ @@ -194,9 +194,13 @@ public static function table(Table $table): Table public static function getRelations(): array { - return [ - RoleRelationManager::class, - ]; + $relationManagers = []; + + if (config('filament-spatie-roles-permissions.should_display_relation_managers.roles', true)) { + $relationManagers[] = RoleRelationManager::class; + } + + return $relationManagers; } public static function getPages(): array diff --git a/src/Resources/RoleResource.php b/src/Resources/RoleResource.php index 9110b02..1608c84 100644 --- a/src/Resources/RoleResource.php +++ b/src/Resources/RoleResource.php @@ -25,13 +25,13 @@ 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'); @@ -85,7 +85,7 @@ public static function form(Form $form): Form ->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()) { + 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); } @@ -96,7 +96,7 @@ public static function form(Form $form): Form ->label(__('filament-spatie-roles-permissions::filament-spatie.field.guard_name')) ->options(config('filament-spatie-roles-permissions.guard_names')) ->default(config('filament-spatie-roles-permissions.default_guard_name')) - ->visible(fn () => config('filament-spatie-roles-permissions.should_show_guard', true)) + ->visible(fn() => config('filament-spatie-roles-permissions.should_show_guard', true)) ->required(), Select::make('permissions') @@ -105,20 +105,20 @@ public static function form(Form $form): Form ->label(__('filament-spatie-roles-permissions::filament-spatie.field.permissions')) ->relationship( name: 'permissions', - modifyQueryUsing: fn (Builder $query) => $query->orderBy('name'), + modifyQueryUsing: fn(Builder $query) => $query->orderBy('name'), ) ->visible(config('filament-spatie-roles-permissions.should_show_permissions_for_roles')) - ->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} ({$record->guard_name})") + ->getOptionLabelFromRecordUsing(fn(Model $record) => "{$record->name} ({$record->guard_name})") ->searchable(['name', 'guard_name']) // searchable on both name and guard_name ->preload(config('filament-spatie-roles-permissions.preload_permissions')), Select::make(config('permission.column_names.team_foreign_key', 'team_id')) ->label(__('filament-spatie-roles-permissions::filament-spatie.field.team')) - ->hidden(fn () => ! config('permission.teams', false) || Filament::hasTenancy()) + ->hidden(fn() => ! config('permission.teams', false) || Filament::hasTenancy()) ->options( - fn () => config('filament-spatie-roles-permissions.team_model', App\Models\Team::class)::pluck('name', 'id') + fn() => config('filament-spatie-roles-permissions.team_model', App\Models\Team::class)::pluck('name', 'id') ) - ->dehydrated(fn ($state) => (int) $state > 0) + ->dehydrated(fn($state) => (int) $state > 0) ->placeholder(__('filament-spatie-roles-permissions::filament-spatie.select-team')) ->hint(__('filament-spatie-roles-permissions::filament-spatie.select-team-hint')), ]), @@ -145,9 +145,7 @@ public static function table(Table $table): Table ->label(__('filament-spatie-roles-permissions::filament-spatie.field.guard_name')) ->searchable(), ]) - ->filters([ - - ]) + ->filters([]) ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\ViewAction::make(), @@ -167,10 +165,18 @@ public static function table(Table $table): Table public static function getRelations(): array { - return [ - PermissionRelationManager::class, - UserRelationManager::class, - ]; + + $relationManagers = []; + + if (config('filament-spatie-roles-permissions.should_display_relation_managers.permissions', true)) { + $relationManagers[] = PermissionRelationManager::class; + } + + if (config('filament-spatie-roles-permissions.should_display_relation_managers.users', true)) { + $relationManagers[] = UserRelationManager::class; + } + + return $relationManagers; } public static function getPages(): array