You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add policy for managing comment authorization (#23)
* Move existing logic to policy
* Remove allow edits and deletes config options
* Handle guest users in canEdit and canDelete
* Clean up README
* Fix styles after merge
* Remove canEdit and canDelete in favor of policy check
* Hide save form when create comment permission is not granted
* Throw authorization exception when attempting to create comment without permission
* Use resolveAuthenticatedUser
* Check any permissions before rendering comment actions area
* Use comment model in relationships
* Added default value
---------
Co-authored-by: Luís Dalmolin <luis.nh@gmail.com>
Copy file name to clipboardExpand all lines: README.md
+47-22Lines changed: 47 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,16 +49,14 @@ class Project extends Model implements Commentable
49
49
50
50
### Usage with Filament
51
51
52
-
You can register the plugin in your Panel(s) and configure editing and deleting permissions:
52
+
You can register the plugin in your Panel(s) like so:
53
53
54
54
```php
55
55
use Kirschbaum\Commentions\CommentionsPlugin;
56
56
57
57
return $panel
58
58
->plugins([
59
-
CommentionsPlugin::make()
60
-
->disallowEdits() // Prevent users from editing their comments
61
-
->disallowDeletes() // Prevent users from deleting their comments
59
+
CommentionsPlugin::make(),
62
60
])
63
61
```
64
62
@@ -110,36 +108,63 @@ If your `User` model lives in a different namespace than `App\Models\User`, you
110
108
],
111
109
```
112
110
113
-
### Disabling comment editing and deletion
111
+
### Configuring the Comment model
114
112
115
-
By default, users can edit and delete their own comments. You can disable this functionality in two ways:
113
+
If you need to customize the Comment model, you can extend the `\Kirschbaum\Commentions\Comment` class and then update the `comment.model` option in your `config/commentions.php` file:
116
114
117
-
#### 1. Using the plugin configuration
115
+
```php
116
+
'comment' => [
117
+
'model' => \App\Models\Comment::class,
118
+
// ...
119
+
],
120
+
```
121
+
122
+
### Configuring Comment permissions
123
+
124
+
By default, users can create comments, as well as edit and delete their own comments. You can adjust these permissions by implementing your own policy:
125
+
126
+
#### 1) Create a custom policy
118
127
119
128
```php
120
-
use Kirschbaum\Commentions\CommentionsPlugin;
129
+
<?php
121
130
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
-
])
131
+
namespace App\Policies;
132
+
133
+
use Kirschbaum\Commentions\Comment;
134
+
use Kirschbaum\Commentions\Contracts\Commenter;
135
+
use Kirschbaum\Commentions\Policies\CommentPolicy;
136
+
137
+
class CommentPolicy extends CommentPolicy
138
+
{
139
+
public function create(Commenter $user): bool
140
+
{
141
+
// TODO: Implement custom permission logic.
142
+
}
143
+
144
+
public function update($user, Comment $comment): bool
145
+
{
146
+
// TODO: Implement custom permission logic.
147
+
}
148
+
149
+
public function delete($user, Comment $comment): bool
150
+
{
151
+
// TODO: Implement custom permission logic.
152
+
}
153
+
}
128
154
```
129
155
130
-
#### 2. Using the configuration file
156
+
#### 2) Register your policy in the configuration file
131
157
132
-
Set the `allow_edits` and `allow_deletes` options in your `config/commentions.php` file:
158
+
Update the `comment.policy` option in your `config/commentions.php` file:
133
159
134
160
```php
135
-
/**
136
-
* Comment editing/deleting options.
137
-
*/
138
-
'allow_edits' => false,
139
-
'allow_deletes' => false,
161
+
'comment' => [
162
+
// ...
163
+
'policy' => \App\Policies\CommentPolicy::class,
164
+
],
140
165
```
141
166
142
-
> **Note:** The plugin configuration takes precedence over the config file settings.
167
+
### Configuring the Commenter name
143
168
144
169
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.
0 commit comments