Skip to content

Commit 5aa52d3

Browse files
committed
Remove canEdit and canDelete in favor of policy check
1 parent 9916845 commit 5aa52d3

File tree

7 files changed

+203
-110
lines changed

7 files changed

+203
-110
lines changed

resources/views/comment.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class="text-xs text-gray-300 ml-1"
3232
@endif
3333
</div>
3434

35-
@if ($comment->isComment() && $comment->canEdit())
35+
@if ($comment->isComment() && auth()->user()?->can('update', $comment))
3636
<div class="flex gap-x-1">
3737
<x-filament::icon-button
3838
icon="heroicon-s-pencil-square"
@@ -41,7 +41,7 @@ class="text-xs text-gray-300 ml-1"
4141
color="gray"
4242
/>
4343

44-
@if ($comment->canDelete())
44+
@can('delete', $comment)
4545
<x-filament::icon-button
4646
icon="heroicon-s-trash"
4747
wire:click="$dispatch('open-modal', { id: 'delete-comment-modal-{{ $comment->getId() }}' })"
@@ -90,7 +90,7 @@ class="text-xs text-gray-300 ml-1"
9090
@endif
9191
</div>
9292

93-
@if ($comment->isComment() && $comment->canDelete())
93+
@if ($comment->isComment() && auth()->user()?->can('delete', $comment))
9494
<x-filament::modal
9595
id="delete-comment-modal-{{ $comment->getId() }}"
9696
wire:model="showDeleteModal"

src/Comment.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,6 @@ public function reactions(): HasMany
163163
return $this->hasMany(CommentReaction::class);
164164
}
165165

166-
public function canEdit(): bool
167-
{
168-
$user = Config::resolveAuthenticatedUser();
169-
170-
return $user && $user->can('update', $this);
171-
}
172-
173-
public function canDelete(): bool
174-
{
175-
$user = Config::resolveAuthenticatedUser();
176-
177-
return $user && $user->can('delete', $this);
178-
}
179-
180166
public function toggleReaction(string $reaction): void
181167
{
182168
ToggleCommentReaction::run($this, $reaction, Config::resolveAuthenticatedUser());

src/Contracts/RenderableComment.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,5 @@ public function getCreatedAt(): \DateTime|\Carbon\Carbon;
2020

2121
public function getUpdatedAt(): \DateTime|\Carbon\Carbon;
2222

23-
public function canEdit(): bool;
24-
25-
public function canDelete(): bool;
26-
2723
public function getLabel(): ?string;
2824
}

src/Livewire/Comment.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function handleReactionToggledEvent(string $reaction, int $commentId): vo
4040
#[Renderless]
4141
public function delete()
4242
{
43-
if (! $this->comment->canDelete()) {
43+
if (! auth()->user()?->can('delete', $this->comment)) {
4444
return;
4545
}
4646

@@ -77,7 +77,7 @@ public function clear(): void
7777

7878
public function edit(): void
7979
{
80-
if (! $this->comment->canEdit()) {
80+
if (! auth()->user()?->can('update', $this->comment)) {
8181
return;
8282
}
8383

@@ -89,7 +89,7 @@ public function edit(): void
8989

9090
public function updateComment()
9191
{
92-
if (! $this->comment->canEdit()) {
92+
if (! auth()->user()?->can('update', $this->comment)) {
9393
return;
9494
}
9595

src/RenderableComment.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ class RenderableComment implements RenderableCommentContract, Wireable
2727

2828
protected DateTime|Carbon $updatedAt;
2929

30-
protected bool $canEdit;
31-
32-
protected bool $canDelete;
33-
3430
public function __construct(
3531
string|int $id,
3632
?string $authorName,
3733
string $body,
3834
?string $authorAvatar = null,
3935
DateTime|Carbon $createdAt = new Carbon(),
4036
DateTime|Carbon $updatedAt = new Carbon(),
41-
bool $canEdit = false,
42-
bool $canDelete = false,
4337
bool $isComment = false,
4438
?string $parsedBody = null,
4539
?string $label = null,
@@ -52,8 +46,6 @@ public function __construct(
5246
$this->parsedBody = $parsedBody;
5347
$this->createdAt = $createdAt;
5448
$this->updatedAt = $updatedAt;
55-
$this->canEdit = $canEdit;
56-
$this->canDelete = $canDelete;
5749
$this->label = $label;
5850
}
5951

@@ -102,16 +94,6 @@ public function getLabel(): ?string
10294
return $this->label;
10395
}
10496

105-
public function canEdit(): bool
106-
{
107-
return $this->canEdit;
108-
}
109-
110-
public function canDelete(): bool
111-
{
112-
return $this->canDelete;
113-
}
114-
11597
public function toLivewire()
11698
{
11799
return [
@@ -123,8 +105,6 @@ public function toLivewire()
123105
'parsedBody' => $this->parsedBody,
124106
'createdAt' => $this->createdAt->format('Y-m-d H:i:s'),
125107
'updatedAt' => $this->updatedAt->format('Y-m-d H:i:s'),
126-
'canEdit' => $this->canEdit,
127-
'canDelete' => $this->canDelete,
128108
'label' => $this->label,
129109
];
130110
}
@@ -140,8 +120,6 @@ public static function fromLivewire($value)
140120
parsedBody: $value['parsedBody'],
141121
createdAt: new Carbon($value['createdAt']),
142122
updatedAt: new Carbon($value['updatedAt']),
143-
canEdit: $value['canEdit'],
144-
canDelete: $value['canDelete'],
145123
label: $value['label'],
146124
);
147125
}

tests/CommentTest.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Facades\Event;
66
use Kirschbaum\Commentions\Comment;
7-
use Kirschbaum\Commentions\Config;
87
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
98
use Tests\Models\Post;
109
use Tests\Models\User;
@@ -80,65 +79,3 @@
8079
->toContain($mentionedUser1)
8180
->toContain($mentionedUser2);
8281
});
83-
84-
test('it allows comment author to edit by default', function () {
85-
$author = User::factory()->create();
86-
$post = Post::factory()->create();
87-
$comment = $post->comment('This is a test comment', $author);
88-
89-
Config::resolveAuthenticatedUserUsing(fn () => $author);
90-
91-
expect($comment->canEdit())->toBeTrue();
92-
});
93-
94-
test('it does not allow non-authors to edit by default', function () {
95-
$user = User::factory()->create();
96-
$author = User::factory()->create();
97-
$post = Post::factory()->create();
98-
$comment = $post->comment('This is a test comment', $author);
99-
100-
Config::resolveAuthenticatedUserUsing(fn () => $user);
101-
102-
expect($comment->canEdit())->toBeFalse();
103-
});
104-
105-
test('it does not allow guests to edit', function () {
106-
$author = User::factory()->create();
107-
$post = Post::factory()->create();
108-
$comment = $post->comment('This is a test comment', $author);
109-
110-
Config::resolveAuthenticatedUserUsing(fn () => null);
111-
112-
expect($comment->canEdit())->toBeFalse();
113-
});
114-
115-
test('it allows comment author to delete by default', function () {
116-
$author = User::factory()->create();
117-
$post = Post::factory()->create();
118-
$comment = $post->comment('This is a test comment', $author);
119-
120-
Config::resolveAuthenticatedUserUsing(fn () => $author);
121-
122-
expect($comment->canDelete())->toBeTrue();
123-
});
124-
125-
test('it does not allow non-authors to delete by default', function () {
126-
$user = User::factory()->create();
127-
$author = User::factory()->create();
128-
$post = Post::factory()->create();
129-
$comment = $post->comment('This is a test comment', $author);
130-
131-
Config::resolveAuthenticatedUserUsing(fn () => $user);
132-
133-
expect($comment->canDelete())->toBeFalse();
134-
});
135-
136-
test('it does not allow guests to delete', function () {
137-
$author = User::factory()->create();
138-
$post = Post::factory()->create();
139-
$comment = $post->comment('This is a test comment', $author);
140-
141-
Config::resolveAuthenticatedUserUsing(fn () => null);
142-
143-
expect($comment->canDelete())->toBeFalse();
144-
});

0 commit comments

Comments
 (0)