Skip to content

Commit

Permalink
Config values to toggle whether to display the relation managers in t… (
Browse files Browse the repository at this point in the history
#225)

* Config values to toggle whether to display the relation managers in the resources - overriding the getRelations function returned errors

* Default to true

---------

Co-authored-by: Jamie Collins <[email protected]>
  • Loading branch information
JamieCee20 and Jamie Collins authored Nov 19, 2024
1 parent d9630a7 commit 3ae31bd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
9 changes: 9 additions & 0 deletions config/filament-spatie-roles-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,21 +84,21 @@ 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()
->label(__('filament-spatie-roles-permissions::filament-spatie.field.roles'))
->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;
Expand All @@ -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')
Expand Down Expand Up @@ -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([
Expand All @@ -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
Expand Down
40 changes: 23 additions & 17 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand All @@ -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')
Expand All @@ -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')),
]),
Expand All @@ -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(),
Expand All @@ -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
Expand Down

0 comments on commit 3ae31bd

Please sign in to comment.