Skip to content

Commit

Permalink
Merge pull request #17 from IBEC-BOX/feat/policy
Browse files Browse the repository at this point in the history
Feat/policy
  • Loading branch information
ast21 authored Jul 31, 2024
2 parents aa8d220 + 79578a7 commit 3b39d0c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 8 deletions.
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);
}
}
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

Check failure on line 16 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $adminKitUser of method AdminKit\Localizations\Policies\LocalizationPolicy::viewAny() has invalid type App\Models\AdminKitUser.
{
return $adminKitUser->can('view_any_localization');

Check failure on line 18 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method can() on an unknown class App\Models\AdminKitUser.
}

/**
* Determine whether the adminKitUser can view the model.
*/
public function view(AdminKitUser $adminKitUser, Localization $localization): bool

Check failure on line 24 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $adminKitUser of method AdminKit\Localizations\Policies\LocalizationPolicy::view() has invalid type App\Models\AdminKitUser.
{
return $adminKitUser->can('view_localization');

Check failure on line 26 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method can() on an unknown class App\Models\AdminKitUser.
}

/**
* Determine whether the adminKitUser can create models.
*/
public function create(AdminKitUser $adminKitUser): bool

Check failure on line 32 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $adminKitUser of method AdminKit\Localizations\Policies\LocalizationPolicy::create() has invalid type App\Models\AdminKitUser.
{
return $adminKitUser->can('create_localization');

Check failure on line 34 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method can() on an unknown class App\Models\AdminKitUser.
}

/**
* Determine whether the adminKitUser can update the model.
*/
public function update(AdminKitUser $adminKitUser, Localization $localization): bool

Check failure on line 40 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $adminKitUser of method AdminKit\Localizations\Policies\LocalizationPolicy::update() has invalid type App\Models\AdminKitUser.
{
return $adminKitUser->can('update_localization');

Check failure on line 42 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method can() on an unknown class App\Models\AdminKitUser.
}

/**
* Determine whether the adminKitUser can delete the model.
*/
public function delete(AdminKitUser $adminKitUser, Localization $localization): bool

Check failure on line 48 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $adminKitUser of method AdminKit\Localizations\Policies\LocalizationPolicy::delete() has invalid type App\Models\AdminKitUser.
{
return $adminKitUser->can('delete_localization');

Check failure on line 50 in src/Policies/LocalizationPolicy.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method can() on an unknown class App\Models\AdminKitUser.
}

/**
* 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');
}
}

0 comments on commit 3b39d0c

Please sign in to comment.