44 lines
965 B
PHP
44 lines
965 B
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Models\Comment;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
|
|
class CommentPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
public function update(User $user, Comment $comment): bool
|
|
{
|
|
return $user->id === $comment->user_id;
|
|
}
|
|
|
|
public function destroy(User $user, Comment $comment): bool
|
|
{
|
|
if ($user->hasRole(UserRole::ADMINISTRATOR) ||
|
|
$user->hasRole(UserRole::MODERATOR)) {
|
|
return true;
|
|
}
|
|
|
|
return $user->id === $comment->user_id;
|
|
}
|
|
|
|
public function restore(User $user, Comment $comment): bool
|
|
{
|
|
// Comment not deleted
|
|
if ($comment->deleted_by_moderator_id === null) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->hasRole(UserRole::ADMINISTRATOR) ||
|
|
$user->hasRole(UserRole::MODERATOR)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|