|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Saito\Posting\UserPosting; |
|
14
|
|
|
|
|
15
|
|
|
use Cake\Core\Configure; |
|
16
|
|
|
use Saito\Posting\Basic\BasicPostingInterface; |
|
17
|
|
|
use Saito\User\CurrentUser\CurrentUserInterface; |
|
18
|
|
|
use Saito\User\Permission\ResourceAI; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Implements UserPostingInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
trait UserPostingTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_userPostingTraitUnreadCache = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var CurrentUserInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $_CurrentUser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getCurrentUser(): CurrentUserInterface |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->_CurrentUser; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setCurrentUser(CurrentUserInterface $CurrentUser): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_CurrentUser = $CurrentUser; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isAnsweringForbidden() |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->isLocked()) { |
|
|
|
|
|
|
57
|
|
|
return 'locked'; |
|
58
|
|
|
} |
|
59
|
|
|
$permission = $this->_CurrentUser->getCategories()->permission('answer', $this->get('category')); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return !$permission; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function isBookmarked(): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->_CurrentUser->hasBookmarked($this->get('id')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritDoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function isEditingAllowed(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->_isEditingAllowed($this, $this->_CurrentUser); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function isEditingAsUserAllowed(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
$MockedUser = clone $this->_CurrentUser; |
|
86
|
|
|
$MockedUser->set('user_type', 'user'); |
|
87
|
|
|
|
|
88
|
|
|
return $this->_isEditingAllowed($this, $MockedUser); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check if editing on the posting is forbidden. |
|
93
|
|
|
* |
|
94
|
|
|
* @param BasicPostingInterface $posting The posting. |
|
95
|
|
|
* @param CurrentUserInterface $User The user. |
|
96
|
|
|
* @return bool|string string if a reason is available. |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function _isEditingAllowed(BasicPostingInterface $posting, CurrentUserInterface $User) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($User->isLoggedIn() !== true) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($User->permission('saito.core.posting.edit.unrestricted')) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/// Check category |
|
109
|
|
|
$action = $posting->isRoot() ? 'thread' : 'answer'; |
|
110
|
|
|
$categoryAllowed = $User->getCategories() |
|
111
|
|
|
->permission($action, $posting->get('category_id')); |
|
112
|
|
|
if (!$categoryAllowed) { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$editPeriod = Configure::read('Saito.Settings.edit_period') * 60; |
|
117
|
|
|
$timeLimit = $editPeriod + ($posting->get('time')->format('U')); |
|
118
|
|
|
$isOverTime = time() > $timeLimit; |
|
119
|
|
|
|
|
120
|
|
|
$isOwn = $User->permission( |
|
121
|
|
|
'saito.core.posting.edit', |
|
122
|
|
|
(new ResourceAI())->onOwner($posting->get('user_id')) |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
if (!$isOverTime && $isOwn && !$this->isLocked()) { |
|
126
|
|
|
// Normal posting without special conditions. |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($User->permission('saito.core.posting.edit.restricted')) { |
|
131
|
|
|
if (!$isOwn || $posting->isPinned()) { |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritDoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isIgnored(): bool |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->_CurrentUser->ignores($this->get('user_id')); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritDoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function isUnread(): bool |
|
151
|
|
|
{ |
|
152
|
|
|
if (!isset($this->_userPostingTraitUnreadCache['isUnread'])) { |
|
153
|
|
|
$id = $this->get('id'); |
|
154
|
|
|
$time = $this->get('time'); |
|
155
|
|
|
$this->_userPostingTraitUnreadCache['isUnread'] = !$this->getCurrentUser() |
|
156
|
|
|
->getReadPostings()->isRead($id, $time); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->_userPostingTraitUnreadCache['isUnread']; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritDoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function hasNewAnswers(): bool |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$this->isRoot()) { |
|
|
|
|
|
|
168
|
|
|
throw new \RuntimeException( |
|
169
|
|
|
'Posting with id ' . $this->get('id') . ' is no root posting.' |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
if (!$this->_CurrentUser->get('last_refresh')) { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this->_CurrentUser->get('last_refresh_unix') < strtotime( |
|
177
|
|
|
$this->get('last_answer') |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|