Skip to content

Commit 9ebfac0

Browse files
committed
feat: implement permission caching
1 parent 197fb09 commit 9ebfac0

5 files changed

Lines changed: 354 additions & 35 deletions

File tree

src/Filament/Clusters/World/Resources/CountryResource.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Eclipse\World\Filament\Clusters\World\Resources;
44

55
use Closure;
6+
use Eclipse\Common\Filament\Concerns\HasCachedAbilityChecks;
67
use Eclipse\World\Filament\Clusters\World;
78
use Eclipse\World\Filament\Clusters\World\Resources\CountryResource\Pages\ListCountries;
89
use Eclipse\World\Models\Country;
@@ -28,11 +29,14 @@
2829
use Filament\Tables\Filters\TrashedFilter;
2930
use Filament\Tables\Table;
3031
use Illuminate\Database\Eloquent\Builder;
32+
use Illuminate\Database\Eloquent\Model;
3133
use Illuminate\Database\Eloquent\SoftDeletingScope;
3234
use TangoDevIt\FilamentEmojiPicker\EmojiPickerAction;
3335

3436
class CountryResource extends Resource
3537
{
38+
use HasCachedAbilityChecks;
39+
3640
protected static ?string $model = Country::class;
3741

3842
protected static ?string $slug = 'countries';
@@ -41,6 +45,51 @@ class CountryResource extends Resource
4145

4246
protected static ?string $cluster = World::class;
4347

48+
public static function canUpdateAny(): bool
49+
{
50+
return static::canOnce('update_country');
51+
}
52+
53+
public static function canDeleteAny(): bool
54+
{
55+
return static::canOnce('delete_country');
56+
}
57+
58+
public static function canRestoreAny(): bool
59+
{
60+
return static::canOnce('restore_country');
61+
}
62+
63+
public static function canForceDeleteAny(): bool
64+
{
65+
return static::canOnce('force_delete_country');
66+
}
67+
68+
public static function canBulkDelete(): bool
69+
{
70+
return static::canOnce('delete_any_country');
71+
}
72+
73+
public static function canEdit(Model $record): bool
74+
{
75+
return static::canUpdateAny();
76+
}
77+
78+
public static function canDelete(Model $record): bool
79+
{
80+
return static::canDeleteAny() && ! $record->trashed();
81+
}
82+
83+
public static function canRestore(Model $record): bool
84+
{
85+
return static::canRestoreAny() && $record->trashed();
86+
}
87+
88+
public static function canForceDelete(Model $record): bool
89+
{
90+
return static::canForceDeleteAny() && $record->trashed();
91+
}
92+
4493
public static function form(Schema $schema): Schema
4594
{
4695
return $schema
@@ -222,30 +271,45 @@ public static function table(Table $table): Table
222271
->recordActions([
223272
EditAction::make()
224273
->label(__('eclipse-world::countries.actions.edit.label'))
225-
->modalHeading(__('eclipse-world::countries.actions.edit.heading')),
274+
->modalHeading(__('eclipse-world::countries.actions.edit.heading'))
275+
->authorize(fn () => self::canUpdateAny()),
276+
226277
ActionGroup::make([
227278
DeleteAction::make()
228279
->label(__('eclipse-world::countries.actions.delete.label'))
229-
->modalHeading(__('eclipse-world::countries.actions.delete.heading')),
280+
->modalHeading(__('eclipse-world::countries.actions.delete.heading'))
281+
->visible(fn (Country $record) => ! $record->trashed())
282+
->authorize(fn () => self::canDeleteAny()),
283+
230284
RestoreAction::make()
231285
->label(__('eclipse-world::countries.actions.restore.label'))
232-
->modalHeading(__('eclipse-world::countries.actions.restore.heading')),
286+
->modalHeading(__('eclipse-world::countries.actions.restore.heading'))
287+
->visible(fn (Country $record) => $record->trashed())
288+
->authorize(fn () => self::canRestoreAny()),
289+
233290
ForceDeleteAction::make()
234291
->label(__('eclipse-world::countries.actions.force_delete.label'))
235292
->modalHeading(__('eclipse-world::countries.actions.force_delete.heading'))
236293
->modalDescription(fn (Country $record): string => __('eclipse-world::countries.actions.force_delete.description', [
237294
'name' => $record->name,
238-
])),
295+
]))
296+
->visible(fn (Country $record) => $record->trashed())
297+
->authorize(fn () => self::canForceDeleteAny()),
239298
]),
240299
])
241300
->toolbarActions([
242301
BulkActionGroup::make([
243302
DeleteBulkAction::make()
244-
->label(__('eclipse-world::countries.actions.delete.label')),
303+
->label(__('eclipse-world::countries.actions.delete.label'))
304+
->authorize(fn () => self::canBulkDelete()),
305+
245306
RestoreBulkAction::make()
246-
->label(__('eclipse-world::countries.actions.restore.label')),
307+
->label(__('eclipse-world::countries.actions.restore.label'))
308+
->authorize(fn () => self::canRestoreAny()),
309+
247310
ForceDeleteBulkAction::make()
248-
->label(__('eclipse-world::countries.actions.force_delete.label')),
311+
->label(__('eclipse-world::countries.actions.force_delete.label'))
312+
->authorize(fn () => self::canForceDeleteAny()),
249313
]),
250314
]);
251315
}

src/Filament/Clusters/World/Resources/CurrencyResource.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Eclipse\World\Filament\Clusters\World\Resources;
44

5+
use Eclipse\Common\Filament\Concerns\HasCachedAbilityChecks;
56
use Eclipse\World\Filament\Clusters\World;
67
use Eclipse\World\Filament\Clusters\World\Resources\CurrencyResource\Pages\ListCurrencies;
78
use Eclipse\World\Models\Currency;
@@ -23,10 +24,13 @@
2324
use Filament\Tables\Filters\TrashedFilter;
2425
use Filament\Tables\Table;
2526
use Illuminate\Database\Eloquent\Builder;
27+
use Illuminate\Database\Eloquent\Model;
2628
use Illuminate\Database\Eloquent\SoftDeletingScope;
2729

2830
class CurrencyResource extends Resource
2931
{
32+
use HasCachedAbilityChecks;
33+
3034
protected static ?string $model = Currency::class;
3135

3236
protected static ?string $slug = 'currencies';
@@ -35,6 +39,51 @@ class CurrencyResource extends Resource
3539

3640
protected static ?string $cluster = World::class;
3741

42+
public static function canUpdateAny(): bool
43+
{
44+
return static::canOnce('update_currency');
45+
}
46+
47+
public static function canDeleteAny(): bool
48+
{
49+
return static::canOnce('delete_currency');
50+
}
51+
52+
public static function canRestoreAny(): bool
53+
{
54+
return static::canOnce('restore_currency');
55+
}
56+
57+
public static function canForceDeleteAny(): bool
58+
{
59+
return static::canOnce('force_delete_currency');
60+
}
61+
62+
public static function canBulkDelete(): bool
63+
{
64+
return static::canOnce('delete_any_currency');
65+
}
66+
67+
public static function canEdit(Model $record): bool
68+
{
69+
return static::canUpdateAny();
70+
}
71+
72+
public static function canDelete(Model $record): bool
73+
{
74+
return static::canDeleteAny() && ! $record->trashed();
75+
}
76+
77+
public static function canRestore(Model $record): bool
78+
{
79+
return static::canRestoreAny() && $record->trashed();
80+
}
81+
82+
public static function canForceDelete(Model $record): bool
83+
{
84+
return static::canForceDeleteAny() && $record->trashed();
85+
}
86+
3887
public static function form(Schema $schema): Schema
3988
{
4089
return $schema
@@ -84,30 +133,45 @@ public static function table(Table $table): Table
84133
->recordActions([
85134
EditAction::make()
86135
->label(__('eclipse-world::currencies.actions.edit.label'))
87-
->modalHeading(__('eclipse-world::currencies.actions.edit.heading')),
136+
->modalHeading(__('eclipse-world::currencies.actions.edit.heading'))
137+
->authorize(fn () => self::canUpdateAny()),
138+
88139
ActionGroup::make([
89140
DeleteAction::make()
90141
->label(__('eclipse-world::currencies.actions.delete.label'))
91-
->modalHeading(__('eclipse-world::currencies.actions.delete.heading')),
142+
->modalHeading(__('eclipse-world::currencies.actions.delete.heading'))
143+
->visible(fn (Currency $record) => ! $record->trashed())
144+
->authorize(fn () => self::canDeleteAny()),
145+
92146
RestoreAction::make()
93147
->label(__('eclipse-world::currencies.actions.restore.label'))
94-
->modalHeading(__('eclipse-world::currencies.actions.restore.heading')),
148+
->modalHeading(__('eclipse-world::currencies.actions.restore.heading'))
149+
->visible(fn (Currency $record) => $record->trashed())
150+
->authorize(fn () => self::canRestoreAny()),
151+
95152
ForceDeleteAction::make()
96153
->label(__('eclipse-world::currencies.actions.force_delete.label'))
97154
->modalHeading(__('eclipse-world::currencies.actions.force_delete.heading'))
98155
->modalDescription(fn (Currency $record): string => __('eclipse-world::currencies.actions.force_delete.description', [
99156
'name' => $record->name,
100-
])),
157+
]))
158+
->visible(fn (Currency $record) => $record->trashed())
159+
->authorize(fn () => self::canForceDeleteAny()),
101160
]),
102161
])
103162
->toolbarActions([
104163
BulkActionGroup::make([
105164
DeleteBulkAction::make()
106-
->label(__('eclipse-world::currencies.actions.delete.label')),
165+
->label(__('eclipse-world::currencies.actions.delete.label'))
166+
->authorize(fn () => self::canBulkDelete()),
167+
107168
RestoreBulkAction::make()
108-
->label(__('eclipse-world::currencies.actions.restore.label')),
169+
->label(__('eclipse-world::currencies.actions.restore.label'))
170+
->authorize(fn () => self::canRestoreAny()),
171+
109172
ForceDeleteBulkAction::make()
110-
->label(__('eclipse-world::currencies.actions.force_delete.label')),
173+
->label(__('eclipse-world::currencies.actions.force_delete.label'))
174+
->authorize(fn () => self::canForceDeleteAny()),
111175
]),
112176
]);
113177
}

src/Filament/Clusters/World/Resources/PostResource.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Eclipse\World\Filament\Clusters\World\Resources;
44

5+
use Eclipse\Common\Filament\Concerns\HasCachedAbilityChecks;
56
use Eclipse\World\Filament\Clusters\World;
67
use Eclipse\World\Filament\Clusters\World\Resources\PostResource\Pages\ListPosts;
78
use Eclipse\World\Models\Post;
@@ -24,11 +25,14 @@
2425
use Filament\Tables\Filters\TrashedFilter;
2526
use Filament\Tables\Table;
2627
use Illuminate\Database\Eloquent\Builder;
28+
use Illuminate\Database\Eloquent\Model;
2729
use Illuminate\Database\Eloquent\SoftDeletingScope;
2830
use Illuminate\Validation\Rule;
2931

3032
class PostResource extends Resource
3133
{
34+
use HasCachedAbilityChecks;
35+
3236
protected static ?string $model = Post::class;
3337

3438
protected static ?string $slug = 'posts';
@@ -37,6 +41,51 @@ class PostResource extends Resource
3741

3842
protected static ?string $cluster = World::class;
3943

44+
public static function canUpdateAny(): bool
45+
{
46+
return static::canOnce('update_post');
47+
}
48+
49+
public static function canDeleteAny(): bool
50+
{
51+
return static::canOnce('delete_post');
52+
}
53+
54+
public static function canRestoreAny(): bool
55+
{
56+
return static::canOnce('restore_post');
57+
}
58+
59+
public static function canForceDeleteAny(): bool
60+
{
61+
return static::canOnce('force_delete_post');
62+
}
63+
64+
public static function canBulkDelete(): bool
65+
{
66+
return static::canOnce('delete_any_post');
67+
}
68+
69+
public static function canEdit(Model $record): bool
70+
{
71+
return static::canUpdateAny();
72+
}
73+
74+
public static function canDelete(Model $record): bool
75+
{
76+
return static::canDeleteAny() && ! $record->trashed();
77+
}
78+
79+
public static function canRestore(Model $record): bool
80+
{
81+
return static::canRestoreAny() && $record->trashed();
82+
}
83+
84+
public static function canForceDelete(Model $record): bool
85+
{
86+
return static::canForceDeleteAny() && $record->trashed();
87+
}
88+
4089
public static function form(Schema $schema): Schema
4190
{
4291
return $schema
@@ -99,30 +148,45 @@ public static function table(Table $table): Table
99148
->recordActions([
100149
EditAction::make()
101150
->label(__('eclipse-world::posts.actions.edit.label'))
102-
->modalHeading(__('eclipse-world::posts.actions.edit.heading')),
151+
->modalHeading(__('eclipse-world::posts.actions.edit.heading'))
152+
->authorize(fn () => self::canUpdateAny()),
153+
103154
ActionGroup::make([
104155
DeleteAction::make()
105156
->label(__('eclipse-world::posts.actions.delete.label'))
106-
->modalHeading(__('eclipse-world::posts.actions.delete.heading')),
157+
->modalHeading(__('eclipse-world::posts.actions.delete.heading'))
158+
->visible(fn (Post $record) => ! $record->trashed())
159+
->authorize(fn () => self::canDeleteAny()),
160+
107161
RestoreAction::make()
108162
->label(__('eclipse-world::posts.actions.restore.label'))
109-
->modalHeading(__('eclipse-world::posts.actions.restore.heading')),
163+
->modalHeading(__('eclipse-world::posts.actions.restore.heading'))
164+
->visible(fn (Post $record) => $record->trashed())
165+
->authorize(fn () => self::canRestoreAny()),
166+
110167
ForceDeleteAction::make()
111168
->label(__('eclipse-world::posts.actions.force_delete.label'))
112169
->modalHeading(__('eclipse-world::posts.actions.force_delete.heading'))
113170
->modalDescription(fn (Post $record): string => __('eclipse-world::posts.actions.force_delete.description', [
114171
'name' => $record->name,
115-
])),
172+
]))
173+
->visible(fn (Post $record) => $record->trashed())
174+
->authorize(fn () => self::canForceDeleteAny()),
116175
]),
117176
])
118177
->toolbarActions([
119178
BulkActionGroup::make([
120179
DeleteBulkAction::make()
121-
->label(__('eclipse-world::posts.actions.delete.label')),
180+
->label(__('eclipse-world::posts.actions.delete.label'))
181+
->authorize(fn () => self::canBulkDelete()),
182+
122183
RestoreBulkAction::make()
123-
->label(__('eclipse-world::posts.actions.restore.label')),
184+
->label(__('eclipse-world::posts.actions.restore.label'))
185+
->authorize(fn () => self::canRestoreAny()),
186+
124187
ForceDeleteBulkAction::make()
125-
->label(__('eclipse-world::posts.actions.force_delete.label')),
188+
->label(__('eclipse-world::posts.actions.force_delete.label'))
189+
->authorize(fn () => self::canForceDeleteAny()),
126190
]),
127191
]);
128192
}

0 commit comments

Comments
 (0)