CommentPolicy.php
4.31 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace FootyRoom\Core\Comment;
use Carbon\Carbon;
use FootyRoom\User\User;
use FootyRoom\Queries\Ban\Ban;
use FootyRoom\Core\AuthException;
use FootyRoom\Core\CoreException;
use FootyRoom\Queries\Ban\ActiveBanQuery;
use FootyRoom\Queries\Ban\BanQueryHandler;
use FootyRoom\Queries\Comment\CommentQueryHandler;
use FootyRoom\Queries\Comment\CommentsInPeriodQuery;
class CommentPolicy
{
/**
* \FootyRoom\Queries\Ban\BanQueryHandler.
*/
protected $banQueryHandler;
/**
* \FootyRoom\Queries\Comment\CommentQueryHandler.
*/
protected $commentQueryHandler;
/**
* Constructor.
*
* @param \FootyRoom\Queries\Ban\BanQueryHandler $banQueryHandler
* @param \FootyRoom\Queries\Comment\CommentQueryHandler $commentQueryHandler
*/
public function __construct(
BanQueryHandler $banQueryHandler,
CommentQueryHandler $commentQueryHandler
) {
$this->banQueryHandler = $banQueryHandler;
$this->commentQueryHandler = $commentQueryHandler;
}
/**
* Determine if user can comment in a discussion.
*
* @param \FootyRoom\User\User $user
* @param bool $throw
*
* @return bool
*/
public function canComment(User $user, $throw = false)
{
// Make sure user is not permanently baned.
if ($user->isBanned()) {
if ($throw) {
throw new CoreException('Your account is permanently banned.');
}
return false;
}
$ban = $this->banQueryHandler->findActiveBan(
new ActiveBanQuery('user_id', $user->getUserId(), 'comment')
);
if ($throw && $ban) {
$this->throwBannedException($ban);
}
if ($ban) {
return false;
}
if ($user->getStatus() === 'newborn') {
// Count comments in last 24 hours.
$count = $this->commentQueryHandler->commentsInPeriod(
new CommentsInPeriodQuery(60 * 60 * 24, $user->getUserId())
);
if ($throw && $count >= 5) {
throw new CoreException('New members can comment/post 5 times per day. You have already made 5 comments/posts today. Please wait until tomorrow.');
}
if ($count >= 5) {
return false;
}
}
return true;
}
/**
* Decides whether user can moderate comments.
*
* @param \FootyRoom\User\User $user
* @param bool $throw
*
* @return bool
*/
public static function canModerate(User $user, $throw = false)
{
if ($user->getRole() >= 20) {
return true;
}
if ($throw) {
throw new AuthException();
}
return false;
}
/**
* Checks whether user can edit specific comment.
*/
public function canEdit(User $user, int $commentUserId, string $discussionId, $throw = false): bool
{
if ($commentUserId === $user->getUserId()) {
return true;
}
if (substr($discussionId, 0, 5) !== 'wall:' && $user->getRole() >= 20) {
return true;
}
if ($throw) {
throw new AuthException();
}
return false;
}
/**
* Throws exception that says that user has been banned from commenting.
*
* @param \FootyRoom\Queries\Ban\Ban $ban (description)
*
* @throws \FootyRoom\Core\CoreException
*/
protected function throwBannedException(Ban $ban)
{
throw new CoreException(
'You are banned from commenting. Your ban will be lifted in '
.Carbon::instance($ban->getCreatedAt())
->addSeconds($ban->getDuration())
->diffForHumans(Carbon::now(), true)
.'.'
);
}
/**
* Returns which meta fields are visible to for specific user role.
*
* @param int $userRole
*
* @return array
*/
public static function getVisibleMeta($userRole)
{
return $userRole >= 25 ? ['edited'] : ['edited'];
}
/**
* Determine if specific user role can see unpublished comments.
*
* @param int $userRole
*
* @return bool
*/
public static function seeUnpublished($userRole)
{
return $userRole >= 25 ? true : false;
}
}