Files
animethemes-server/app/Policies/Admin/FeaturePolicy.php
T
paranarimasu 088ea287c5 feat: initial migration to pennant features for feature flags and global site config [incremental] (#573)
* feat: initial commit for feature management

* style: fix StyleCI findings

* fix(api): prohibit features of nonnull scope from API & fix(admin): wrong translate key

* style: fix Static Analysis error
2023-04-23 18:54:34 -05:00

82 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Policies\Admin;
use App\Enums\Auth\CrudPermission;
use App\Models\Admin\Feature;
use App\Models\Auth\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Laravel\Nova\Nova;
/**
* Class FeaturePolicy.
*/
class FeaturePolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any models.
*
* @param User|null $user
* @return bool
*/
public function viewAny(?User $user): bool
{
return Nova::whenServing(
fn (): bool => $user !== null && $user->can(CrudPermission::VIEW()->format(Feature::class)),
fn (): bool => true
);
}
/**
* Determine whether the user can view the model.
*
* @param User|null $user
* @param Feature $feature
* @return bool
*/
public function view(?User $user, Feature $feature): bool
{
return Nova::whenServing(
fn (): bool => $user !== null && $user->can(CrudPermission::VIEW()->format(Feature::class)),
fn (): bool => $feature->isNullScope()
);
}
/**
* Determine whether the user can create models.
*
* @param User $user
* @return bool
*/
public function create(User $user): bool
{
return $user->can(CrudPermission::CREATE()->format(Feature::class));
}
/**
* Determine whether the user can update the model.
*
* @param User $user
* @return bool
*/
public function update(User $user): bool
{
return $user->can(CrudPermission::UPDATE()->format(Feature::class));
}
/**
* Determine whether the user can delete the model.
*
* @param User $user
* @return bool
*/
public function delete(User $user): bool
{
return $user->can(CrudPermission::DELETE()->format(Feature::class));
}
}