Skip to content

Commit 10c5053

Browse files
luisdalmolinclaude
andcommitted
Add ability to disable comment edits and deletes
- Added config options to disable comment editing and deletion - Added plugin configuration methods disallowEdits() and disallowDeletes() - Updated Comment.php to respect these settings - Added tests to verify functionality - Updated documentation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5367d9b commit 10c5053

File tree

7 files changed

+174
-7
lines changed

7 files changed

+174
-7
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ class Project extends Model implements Commentable
4949

5050
### Usage with Filament
5151

52-
You are free to register the plugin in your Panel(s); however, there is currently no configuration required or supported at the Panel level although it is a good habit:
52+
You can register the plugin in your Panel(s) and configure editing and deleting permissions:
5353

5454
```php
5555
use Kirschbaum\Commentions\CommentionsPlugin;
5656

5757
return $panel
5858
->plugins([
5959
CommentionsPlugin::make()
60+
->disallowEdits() // Prevent users from editing their comments
61+
->disallowDeletes() // Prevent users from deleting their comments
6062
])
6163
```
6264

@@ -108,6 +110,37 @@ If your `User` model lives in a different namespace than `App\Models\User`, you
108110
],
109111
```
110112

113+
### Disabling comment editing and deletion
114+
115+
By default, users can edit and delete their own comments. You can disable this functionality in two ways:
116+
117+
#### 1. Using the plugin configuration
118+
119+
```php
120+
use Kirschbaum\Commentions\CommentionsPlugin;
121+
122+
return $panel
123+
->plugins([
124+
CommentionsPlugin::make()
125+
->disallowEdits() // Prevent users from editing their comments
126+
->disallowDeletes() // Prevent users from deleting their comments
127+
])
128+
```
129+
130+
#### 2. Using the configuration file
131+
132+
Set the `allow_edits` and `allow_deletes` options in your `config/commentions.php` file:
133+
134+
```php
135+
/**
136+
* Comment editing/deleting options.
137+
*/
138+
'allow_edits' => false,
139+
'allow_deletes' => false,
140+
```
141+
142+
> **Note:** The plugin configuration takes precedence over the config file settings.
143+
111144
By default, the `name` property will be used to render the mention names. You can customize it either by implementing the Filament `HasName` interface OR by implementing the optional `getCommenterName` method.
112145

113146
```php

config/commentions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
'commenter' => [
1313
'model' => \App\Models\User::class,
1414
],
15+
16+
/**
17+
* Comment editing/deleting options.
18+
*/
19+
'allow_edits' => true,
20+
'allow_deletes' => true,
1521
];

src/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ public function getUpdatedAt(): \DateTime|\Carbon\Carbon
153153

154154
public function canEdit(): bool
155155
{
156-
return $this->isAuthor(Config::resolveAuthenticatedUser());
156+
return Config::allowEdits() && $this->isAuthor(Config::resolveAuthenticatedUser());
157157
}
158158

159159
public function canDelete(): bool
160160
{
161-
return $this->canEdit();
161+
return Config::allowDeletes() && $this->isAuthor(Config::resolveAuthenticatedUser());
162162
}
163163

164164
public function getLabel(): ?string

src/CommentionsPlugin.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77

88
class CommentionsPlugin implements Plugin
99
{
10+
protected bool $allowEdits = true;
11+
12+
protected bool $allowDeletes = true;
13+
1014
public function getId(): string
1115
{
1216
return CommentionsServiceProvider::$name;
1317
}
1418

15-
public function register(Panel $panel): void {}
19+
public function register(Panel $panel): void
20+
{
21+
Config::allowEdits($this->allowEdits);
22+
Config::allowDeletes($this->allowDeletes);
23+
}
1624

1725
public function boot(Panel $panel): void {}
1826

@@ -25,4 +33,18 @@ public static function get(): static
2533
{
2634
return filament(app(static::class)->getId());
2735
}
36+
37+
public function disallowEdits(): static
38+
{
39+
$this->allowEdits = false;
40+
41+
return $this;
42+
}
43+
44+
public function disallowDeletes(): static
45+
{
46+
$this->allowDeletes = false;
47+
48+
return $this;
49+
}
2850
}

src/Config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class Config
1111

1212
protected static ?Closure $resolveAuthenticatedUser = null;
1313

14+
protected static ?bool $allowEdits = null;
15+
16+
protected static ?bool $allowDeletes = null;
17+
1418
public static function resolveAuthenticatedUserUsing(Closure $callback): void
1519
{
1620
static::$resolveAuthenticatedUser = $callback;
@@ -27,4 +31,24 @@ public static function getCommenterModel(): string
2731
{
2832
return config('commentions.commenter.model');
2933
}
34+
35+
public static function allowEdits(?bool $allow = null): bool|null
36+
{
37+
if (is_bool($allow)) {
38+
static::$allowEdits = $allow;
39+
return null;
40+
}
41+
42+
return static::$allowEdits ?? config('commentions.allow_edits', true);
43+
}
44+
45+
public static function allowDeletes(?bool $allow = null): bool|null
46+
{
47+
if (is_bool($allow)) {
48+
static::$allowDeletes = $allow;
49+
return null;
50+
}
51+
52+
return static::$allowDeletes ?? config('commentions.allow_deletes', true);
53+
}
3054
}

src/Livewire/Comment.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Comment extends Component
3030
#[Renderless]
3131
public function delete()
3232
{
33-
if (! $this->comment->isAuthor(Config::resolveAuthenticatedUser())) {
33+
if (! $this->comment->canDelete()) {
3434
return;
3535
}
3636

@@ -67,7 +67,7 @@ public function clear(): void
6767

6868
public function edit(): void
6969
{
70-
if (! $this->comment->isAuthor(Config::resolveAuthenticatedUser())) {
70+
if (! $this->comment->canEdit()) {
7171
return;
7272
}
7373

@@ -79,7 +79,7 @@ public function edit(): void
7979

8080
public function updateComment()
8181
{
82-
if (! $this->comment->isAuthor(Config::resolveAuthenticatedUser())) {
82+
if (! $this->comment->canEdit()) {
8383
return;
8484
}
8585

tests/CommentTest.php

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

55
use Illuminate\Support\Facades\Event;
66
use Kirschbaum\Commentions\Comment;
7+
use Kirschbaum\Commentions\CommentionsPlugin;
8+
use Kirschbaum\Commentions\Config;
79
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
810
use Tests\Models\Post;
911
use Tests\Models\User;
@@ -68,3 +70,83 @@
6870
->toContain($mentionedUser1)
6971
->toContain($mentionedUser2);
7072
});
73+
74+
test('it can disable editing of comments', function () {
75+
$user = User::factory()->create();
76+
$post = Post::factory()->create();
77+
$comment = $post->comment('This is a test comment', $user);
78+
79+
// Set authenticated user to the comment author
80+
Config::resolveAuthenticatedUserUsing(fn () => $user);
81+
82+
// Should be able to edit by default
83+
expect($comment->canEdit())->toBeTrue();
84+
85+
// Disable edits
86+
config(['commentions.allow_edits' => false]);
87+
expect($comment->canEdit())->toBeFalse();
88+
});
89+
90+
test('it can disable deletion of comments', function () {
91+
$user = User::factory()->create();
92+
$post = Post::factory()->create();
93+
$comment = $post->comment('This is a test comment', $user);
94+
95+
// Set authenticated user to the comment author
96+
Config::resolveAuthenticatedUserUsing(fn () => $user);
97+
98+
// Should be able to delete by default
99+
expect($comment->canDelete())->toBeTrue();
100+
101+
// Disable deletes
102+
config(['commentions.allow_deletes' => false]);
103+
expect($comment->canDelete())->toBeFalse();
104+
});
105+
106+
test('plugin can disallow edits and deletes', function () {
107+
$user = User::factory()->create();
108+
$post = Post::factory()->create();
109+
$comment = $post->comment('This is a test comment', $user);
110+
111+
// Set authenticated user to the comment author
112+
Config::resolveAuthenticatedUserUsing(fn () => $user);
113+
114+
// Reset to default values
115+
Config::allowEdits(true);
116+
Config::allowDeletes(true);
117+
118+
// Should be able to edit and delete by default
119+
expect($comment->canEdit())->toBeTrue();
120+
expect($comment->canDelete())->toBeTrue();
121+
122+
// Create a plugin instance and disallow edits
123+
$plugin = new CommentionsPlugin();
124+
$plugin->disallowEdits();
125+
$panel = new \Filament\Panel();
126+
$plugin->register($panel);
127+
128+
expect($comment->canEdit())->toBeFalse();
129+
expect($comment->canDelete())->toBeTrue();
130+
131+
// Reset and test disallowing deletes
132+
Config::allowEdits(true);
133+
Config::allowDeletes(true);
134+
135+
$plugin = new CommentionsPlugin();
136+
$plugin->disallowDeletes();
137+
$plugin->register($panel);
138+
139+
expect($comment->canEdit())->toBeTrue();
140+
expect($comment->canDelete())->toBeFalse();
141+
142+
// Test disallowing both
143+
Config::allowEdits(true);
144+
Config::allowDeletes(true);
145+
146+
$plugin = new CommentionsPlugin();
147+
$plugin->disallowEdits()->disallowDeletes();
148+
$plugin->register($panel);
149+
150+
expect($comment->canEdit())->toBeFalse();
151+
expect($comment->canDelete())->toBeFalse();
152+
});

0 commit comments

Comments
 (0)