Add ability to delete comments by moderators
This commit is contained in:
@@ -17,11 +17,13 @@ class IsModerator
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (Auth::check() && Auth::user()->hasRole(UserRole::MODERATOR)) {
|
||||
if (Auth::check() && (
|
||||
Auth::user()->hasRole(UserRole::MODERATOR) ||
|
||||
Auth::user()->hasRole(UserRole::ADMINISTRATOR))) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
session()->flash('error_msg', 'This resource is restricted to Administrators!');
|
||||
session()->flash('error_msg', 'This resource is restricted to Moderators!');
|
||||
|
||||
return redirect()->route('home.index');
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Episode;
|
||||
use App\Models\User;
|
||||
use App\Notifications\CommentNotification;
|
||||
@@ -43,7 +44,7 @@ class Comment extends Component
|
||||
'replyState.body' => 'reply',
|
||||
];
|
||||
|
||||
public function updatedIsEditing($isEditing)
|
||||
public function updatedIsEditing(bool $isEditing)
|
||||
{
|
||||
if (! $isEditing) {
|
||||
return;
|
||||
@@ -67,6 +68,15 @@ class Comment extends Component
|
||||
{
|
||||
$this->authorize('destroy', $this->comment);
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->hasRole(UserRole::ADMINISTRATOR) || $user->hasRole(UserRole::MODERATOR)) {
|
||||
$this->comment->deleted_by_moderator_id = $user->id;
|
||||
$this->comment->save();
|
||||
$this->dispatch('refresh');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->comment->delete();
|
||||
|
||||
$this->dispatch('refresh');
|
||||
|
||||
@@ -72,4 +72,12 @@ class Comment extends Model
|
||||
{
|
||||
return cache()->remember('commentLikes'.$this->id, 300, fn () => $this->likes->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns wether or not comment has been removed by moderation
|
||||
*/
|
||||
public function isDeletedByModerator(): bool
|
||||
{
|
||||
return $this->deleted_by_moderator_id !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Comment;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
@@ -17,6 +18,11 @@ class CommentPolicy
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user