mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-11-13 01:28:51 +01:00
* 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
82 lines
1.9 KiB
PHP
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));
|
|
}
|
|
}
|