optimizations
This commit is contained in:
86
app/Policies/CollectionPolicy.php
Normal file
86
app/Policies/CollectionPolicy.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Collection;
|
||||
use App\Models\User;
|
||||
|
||||
class CollectionPolicy
|
||||
{
|
||||
public function before($user, $ability)
|
||||
{
|
||||
if (! $user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isAdmin($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function view(?User $user, Collection $collection): bool
|
||||
{
|
||||
if ($user && $collection->isOwnedBy($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $collection->isPubliclyAccessible();
|
||||
}
|
||||
|
||||
public function create(?User $user): bool
|
||||
{
|
||||
return (bool) $user;
|
||||
}
|
||||
|
||||
public function update(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canBeManagedBy($user);
|
||||
}
|
||||
|
||||
public function delete(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->isOwnedBy($user);
|
||||
}
|
||||
|
||||
public function manageArtworks(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canManageArtworks($user);
|
||||
}
|
||||
|
||||
public function manageMembers(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canManageMembers($user);
|
||||
}
|
||||
|
||||
public function submit(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canReceiveSubmissionsFrom($user);
|
||||
}
|
||||
|
||||
public function comment(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canReceiveCommentsFrom($user);
|
||||
}
|
||||
|
||||
public function save(User $user, Collection $collection): bool
|
||||
{
|
||||
return $collection->canBeSavedBy($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;
|
||||
}
|
||||
}
|
||||
107
app/Policies/NovaCardPolicy.php
Normal file
107
app/Policies/NovaCardPolicy.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user