Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/policy #17

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/LocalizationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Models/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ protected static function boot()

protected static function newFactory(): LocalizationFactory
{
return new LocalizationFactory();
return new LocalizationFactory;
}
}
108 changes: 108 additions & 0 deletions src/Policies/LocalizationPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace AdminKit\Localizations\Policies;

use AdminKit\Localizations\Models\Localization;
use App\Models\AdminKitUser;
use Illuminate\Auth\Access\HandlesAuthorization;

class LocalizationPolicy
{
use HandlesAuthorization;

/**
* Determine whether the adminKitUser can view any models.
*/
public function viewAny(AdminKitUser $adminKitUser): bool
{
return $adminKitUser->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');
}
}
1 change: 0 additions & 1 deletion src/UI/Filament/Filters/NotTranslatedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down