From 9571a21c2a332ca26fc8cc2be7762e8ff2f9e549 Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Wed, 31 Jul 2024 16:48:42 +0900 Subject: [PATCH 1/2] feat: add policy --- src/LocalizationsServiceProvider.php | 27 +++++-- src/Policies/LocalizationPolicy.php | 108 +++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/Policies/LocalizationPolicy.php diff --git a/src/LocalizationsServiceProvider.php b/src/LocalizationsServiceProvider.php index 2fc0b90..c66c23f 100644 --- a/src/LocalizationsServiceProvider.php +++ b/src/LocalizationsServiceProvider.php @@ -4,9 +4,7 @@ use AdminKit\Localizations\Commands\InstallCommand; use AdminKit\Localizations\Providers\RouteServiceProvider; -use AdminKit\Localizations\UI\API\Repositories\CachedLocalizationRepository; -use AdminKit\Localizations\UI\API\Repositories\LocalizationRepository; -use AdminKit\Localizations\UI\API\Repositories\LocalizationRepositoryInterface; +use Illuminate\Support\Facades\Gate; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -39,11 +37,9 @@ public function bootingPackage() { $this->publishFiles(); - $repository = match ((bool) config('admin-kit.cache.enabled')) { - true => CachedLocalizationRepository::class, - false => LocalizationRepository::class, - }; - $this->app->bind(LocalizationRepositoryInterface::class, $repository); + $this->bindingPolicies(); + + $this->bindingRepositories(); } protected function registerConfigs(): self @@ -63,4 +59,19 @@ protected function publishFiles(): self return $this; } + + protected function bindingPolicies(): void + { + Gate::policy(\AdminKit\Localizations\Models\Localization::class, \AdminKit\Localizations\Policies\LocalizationPolicy::class); + } + + protected function bindingRepositories(): void + { + $repository = \AdminKit\Localizations\UI\API\Repositories\LocalizationRepository::class; + if (config('admin-kit.cache.enabled')) { + $repository = \AdminKit\Localizations\UI\API\Repositories\CachedLocalizationRepository::class; + } + + $this->app->bind(\AdminKit\Localizations\UI\API\Repositories\LocalizationRepositoryInterface::class, $repository); + } } diff --git a/src/Policies/LocalizationPolicy.php b/src/Policies/LocalizationPolicy.php new file mode 100644 index 0000000..ebb6952 --- /dev/null +++ b/src/Policies/LocalizationPolicy.php @@ -0,0 +1,108 @@ +can('view_any_localization'); + } + + /** + * Determine whether the adminKitUser can view the model. + */ + public function view(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('view_localization'); + } + + /** + * Determine whether the adminKitUser can create models. + */ + public function create(AdminKitUser $adminKitUser): bool + { + return $adminKitUser->can('create_localization'); + } + + /** + * Determine whether the adminKitUser can update the model. + */ + public function update(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('update_localization'); + } + + /** + * Determine whether the adminKitUser can delete the model. + */ + public function delete(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('delete_localization'); + } + + /** + * Determine whether the adminKitUser can bulk delete. + */ + public function deleteAny(AdminKitUser $adminKitUser): bool + { + return $adminKitUser->can('delete_any_localization'); + } + + /** + * Determine whether the adminKitUser can permanently delete. + */ + public function forceDelete(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('force_delete_localization'); + } + + /** + * Determine whether the adminKitUser can permanently bulk delete. + */ + public function forceDeleteAny(AdminKitUser $adminKitUser): bool + { + return $adminKitUser->can('force_delete_any_localization'); + } + + /** + * Determine whether the adminKitUser can restore. + */ + public function restore(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('restore_localization'); + } + + /** + * Determine whether the adminKitUser can bulk restore. + */ + public function restoreAny(AdminKitUser $adminKitUser): bool + { + return $adminKitUser->can('restore_any_localization'); + } + + /** + * Determine whether the adminKitUser can replicate. + */ + public function replicate(AdminKitUser $adminKitUser, Localization $localization): bool + { + return $adminKitUser->can('replicate_localization'); + } + + /** + * Determine whether the adminKitUser can reorder. + */ + public function reorder(AdminKitUser $adminKitUser): bool + { + return $adminKitUser->can('reorder_localization'); + } +} From 79578a7446c0415baad8546bd8fc9a10f2b42ef9 Mon Sep 17 00:00:00 2001 From: ast21 Date: Wed, 31 Jul 2024 07:49:10 +0000 Subject: [PATCH 2/2] Fix styling --- src/Models/Localization.php | 2 +- src/Policies/LocalizationPolicy.php | 2 +- src/UI/Filament/Filters/NotTranslatedFilter.php | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Models/Localization.php b/src/Models/Localization.php index a334c01..670e056 100644 --- a/src/Models/Localization.php +++ b/src/Models/Localization.php @@ -42,6 +42,6 @@ protected static function boot() protected static function newFactory(): LocalizationFactory { - return new LocalizationFactory(); + return new LocalizationFactory; } } diff --git a/src/Policies/LocalizationPolicy.php b/src/Policies/LocalizationPolicy.php index ebb6952..fb5c0ad 100644 --- a/src/Policies/LocalizationPolicy.php +++ b/src/Policies/LocalizationPolicy.php @@ -2,8 +2,8 @@ namespace AdminKit\Localizations\Policies; -use App\Models\AdminKitUser; use AdminKit\Localizations\Models\Localization; +use App\Models\AdminKitUser; use Illuminate\Auth\Access\HandlesAuthorization; class LocalizationPolicy diff --git a/src/UI/Filament/Filters/NotTranslatedFilter.php b/src/UI/Filament/Filters/NotTranslatedFilter.php index a1ead3c..91f9c72 100644 --- a/src/UI/Filament/Filters/NotTranslatedFilter.php +++ b/src/UI/Filament/Filters/NotTranslatedFilter.php @@ -3,7 +3,6 @@ namespace AdminKit\Localizations\UI\Filament\Filters; use AdminKit\Core\DTO\LocaleData; -use AdminKit\Core\Facades\AdminKit; use Filament\Forms\Components\Select; use Filament\Tables\Filters\Filter; use Illuminate\Database\Eloquent\Builder;