-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
PostPolicy.php
66 lines (54 loc) · 2.15 KB
/
PostPolicy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Post\Access;
use Carbon\Carbon;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Access\AbstractPolicy;
use Flarum\User\User;
class PostPolicy extends AbstractPolicy
{
public function __construct(
protected SettingsRepositoryInterface $settings
) {
}
public function can(User $actor, string $ability, Post $post): ?string
{
if ($actor->can($ability.'Posts', $post->discussion)) {
return $this->allow();
}
return null;
}
public function edit(User $actor, Post $post): ?string
{
// A post is allowed to be edited if the user is the author, the post
// hasn't been deleted by someone else, and the user is allowed to
// create new replies in the discussion.
if ($post->user_id == $actor->id && (! $post->hidden_at || $post->hidden_user_id == $actor->id) && $actor->can('reply', $post->discussion)) {
$allowEditing = $this->settings->get('allow_post_editing');
if ($allowEditing === '-1'
|| ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number)
|| (is_numeric($allowEditing) && $post->created_at->diffInMinutes(new Carbon) < $allowEditing)) {
return $this->allow();
}
}
return null;
}
public function hide(User $actor, Post $post): ?string
{
if ($post->user_id == $actor->id && (! $post->hidden_at || $post->hidden_user_id == $actor->id) && $actor->can('reply', $post->discussion)) {
$allowHiding = $this->settings->get('allow_hide_own_posts');
if ($allowHiding === '-1'
|| ($allowHiding === 'reply' && $post->number >= $post->discussion->last_post_number)
|| (is_numeric($allowHiding) && $post->created_at->diffInMinutes(new Carbon) < $allowHiding)) {
return $this->allow();
}
}
return null;
}
}