Files
SkinbaseNova/app/Policies/NovaCardPolicy.php
2026-03-28 19:15:39 +01:00

108 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Policies;
use App\Models\NovaCard;
use App\Models\User;
class NovaCardPolicy
{
public function before($user, $ability)
{
if (! $user) {
return null;
}
if ($this->isAdmin($user)) {
return true;
}
return null;
}
public function view(?User $user, NovaCard $card): bool
{
return $card->canBeViewedBy($user);
}
public function create(?User $user): bool
{
return (bool) $user;
}
public function update(User $user, NovaCard $card): bool
{
return $card->isOwnedBy($user) && in_array($card->status, [NovaCard::STATUS_DRAFT, NovaCard::STATUS_PROCESSING, NovaCard::STATUS_PUBLISHED], true);
}
public function delete(User $user, NovaCard $card): bool
{
return $card->isOwnedBy($user);
}
public function publish(User $user, NovaCard $card): bool
{
return $card->isOwnedBy($user);
}
public function comment(User $user, NovaCard $card): bool
{
return $card->canReceiveCommentsFrom($user);
}
public function allowExport(?User $user, NovaCard $card): bool
{
if ($card->isOwnedBy($user)) {
return true;
}
return (bool) $card->allow_export;
}
public function allowBackgroundReuse(?User $user, NovaCard $card): bool
{
if ($card->isOwnedBy($user)) {
return true;
}
return (bool) $card->allow_background_reuse;
}
public function moderate(User $user): bool
{
return $this->isModerator($user);
}
private function isAdmin(User $user): bool
{
if (method_exists($user, 'isAdmin')) {
return (bool) $user->isAdmin();
}
if (method_exists($user, 'hasRole')) {
return (bool) $user->hasRole('admin');
}
return false;
}
private function isModerator(User $user): bool
{
if ($this->isAdmin($user)) {
return true;
}
if (method_exists($user, 'isModerator')) {
return (bool) $user->isModerator();
}
if (method_exists($user, 'hasRole')) {
return (bool) $user->hasRole('moderator');
}
return false;
}
}