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
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implements UserPostingInterface |
21
|
|
|
*/ |
22
|
|
|
trait UserPostingTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $_cache = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CurrentUserInterface |
31
|
|
|
*/ |
32
|
|
|
protected $_CurrentUser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get current-user. |
36
|
|
|
* |
37
|
|
|
* @return CurrentUserInterface |
38
|
|
|
*/ |
39
|
|
|
public function getCurrentUser() |
40
|
|
|
{ |
41
|
|
|
return $this->_CurrentUser; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set current user. |
46
|
|
|
* |
47
|
|
|
* @param CurrentUserInterface $CurrentUser current user |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function setCurrentUser($CurrentUser) |
51
|
|
|
{ |
52
|
|
|
$this->_CurrentUser = $CurrentUser; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function isAnsweringForbidden() |
59
|
|
|
{ |
60
|
|
|
if ($this->isLocked()) { |
|
|
|
|
61
|
|
|
return 'locked'; |
62
|
|
|
} |
63
|
|
|
$permission = $this->_CurrentUser->getCategories()->permission('answer', $this->get('category')); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return !$permission; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function isBookmarked(): bool |
72
|
|
|
{ |
73
|
|
|
return $this->_CurrentUser->hasBookmarked($this->get('id')); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function isEditingAllowed(): bool |
80
|
|
|
{ |
81
|
|
|
return $this->_isEditingAllowed($this, $this->_CurrentUser); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function isEditingAsUserAllowed(): bool |
88
|
|
|
{ |
89
|
|
|
$MockedUser = clone $this->_CurrentUser; |
90
|
|
|
$MockedUser->set('user_type', 'user'); |
91
|
|
|
|
92
|
|
|
return $this->_isEditingAllowed($this, $MockedUser); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if editing on the posting is forbidden. |
97
|
|
|
* |
98
|
|
|
* @param BasicPostingInterface $posting The posting. |
99
|
|
|
* @param CurrentUserInterface $User The user. |
100
|
|
|
* @return bool|string string if a reason is available. |
101
|
|
|
*/ |
102
|
|
|
protected function _isEditingAllowed(BasicPostingInterface $posting, CurrentUserInterface $User) |
103
|
|
|
{ |
104
|
|
|
if ($User->isLoggedIn() !== true) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($User->permission('saito.core.posting.edit.unrestricted')) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$editPeriod = Configure::read('Saito.Settings.edit_period') * 60; |
113
|
|
|
$timeLimit = $editPeriod + ($posting->get('time')->format('U')); |
114
|
|
|
$isOverTime = time() > $timeLimit; |
115
|
|
|
|
116
|
|
|
$isOwn = $User->getId() === $posting->get('user_id'); |
117
|
|
|
|
118
|
|
|
if (!$isOverTime && $isOwn && !$this->isLocked()) { |
|
|
|
|
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($User->permission('saito.core.posting.edit.restricted')) { |
123
|
|
|
if ($posting->isPinned()) { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
public function isIgnored(): bool |
135
|
|
|
{ |
136
|
|
|
return $this->_CurrentUser->ignores($this->get('user_id')); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
|
|
public function isUnread(): bool |
143
|
|
|
{ |
144
|
|
|
if (!isset($this->_cache['isUnread'])) { |
145
|
|
|
$id = $this->get('id'); |
|
|
|
|
146
|
|
|
$time = $this->get('time'); |
|
|
|
|
147
|
|
|
$this->_cache['isUnread'] = !$this->getCurrentUser() |
148
|
|
|
->getReadPostings()->isRead($id, $time); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->_cache['isUnread']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
|
|
public function hasNewAnswers(): bool |
158
|
|
|
{ |
159
|
|
|
if (!$this->isRoot()) { |
|
|
|
|
160
|
|
|
throw new \RuntimeException( |
161
|
|
|
'Posting with id ' . $this->get('id') . ' is no root posting.' |
|
|
|
|
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
if (!$this->_CurrentUser->get('last_refresh')) { |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->_CurrentUser->get('last_refresh_unix') < strtotime( |
169
|
|
|
$this->get('last_answer') |
|
|
|
|
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.