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 App\Model\Table; |
14
|
|
|
|
15
|
|
|
use App\Model\Table\UsersTable; |
16
|
|
|
use Cake\ORM\Query; |
17
|
|
|
use Cake\ORM\Table; |
18
|
|
|
use Cake\Validation\Validator; |
19
|
|
|
use Saito\User\Blocker\BlockerAbstract; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* UserBlock table |
23
|
|
|
* |
24
|
|
|
* @property UsersTable $Users |
25
|
|
|
*/ |
26
|
|
|
class UserBlocksTable extends Table |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function initialize(array $config) |
33
|
|
|
{ |
34
|
|
|
$this->addBehavior('Timestamp'); |
35
|
|
|
|
36
|
|
|
// Blocked user. |
37
|
|
|
$this->belongsTo('Users', ['foreignKey' => 'user_id']); |
38
|
|
|
// User responsible for the blocking. |
39
|
|
|
$this->belongsTo( |
40
|
|
|
'BlockedBy', |
41
|
|
|
['className' => 'Users', 'foreignKey' => 'blocked_by_user_id'] |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function validationDefault(Validator $validator) |
49
|
|
|
{ |
50
|
|
|
$validator |
|
|
|
|
51
|
|
|
->allowEmpty('ends') |
52
|
|
|
->add('ends', 'datetime', ['rule' => ['datetime']]); |
53
|
|
|
$validator->notEmpty('user_id'); |
|
|
|
|
54
|
|
|
$validator->notEmpty('reason'); |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $validator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* block user |
61
|
|
|
* |
62
|
|
|
* @param BlockerAbstract $Blocker blocker |
63
|
|
|
* @param int $userId user-ID |
64
|
|
|
* @return bool success |
65
|
|
|
*/ |
66
|
|
|
public function block(BlockerAbstract $Blocker, int $userId): bool |
67
|
|
|
{ |
68
|
|
|
$Blocker->setUserBlockTable($this); |
69
|
|
|
$success = $Blocker->block($userId); |
70
|
|
|
if ($success) { |
71
|
|
|
$this->_updateIsBlocked($userId); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $success; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* get block ending for user |
79
|
|
|
* |
80
|
|
|
* @param int $userId user-ID |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function getBlockEndsForUser($userId) |
84
|
|
|
{ |
85
|
|
|
$block = $this->find( |
86
|
|
|
'all', |
87
|
|
|
[ |
88
|
|
|
'conditions' => ['user_id' => $userId, 'ended IS' => null], |
89
|
|
|
'sort' => ['ends' => 'asc'], |
90
|
|
|
] |
91
|
|
|
)->first(); |
92
|
|
|
|
93
|
|
|
return $block->get('ends'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* unblock |
98
|
|
|
* |
99
|
|
|
* @param int $id id |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function unblock($id) |
103
|
|
|
{ |
104
|
|
|
$block = $this->find()->where(['id' => $id, 'ended IS' => null])->first( |
105
|
|
|
); |
106
|
|
|
if (!$block) { |
107
|
|
|
throw new \InvalidArgumentException(); |
108
|
|
|
} |
109
|
|
|
$this->patchEntity( |
110
|
|
|
$block, |
|
|
|
|
111
|
|
|
['ended' => bDate(), 'ends' => null] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
if (!$this->save($block)) { |
|
|
|
|
115
|
|
|
throw new \RuntimeException( |
116
|
|
|
"Couldn't unblock block with id $id.", |
117
|
|
|
1420540471 |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->_updateIsBlocked($block->get('user_id')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Garbage collection |
126
|
|
|
* |
127
|
|
|
* called hourly from User model |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function gc() |
132
|
|
|
{ |
133
|
|
|
$expired = $this->find('toGc')->all(); |
134
|
|
|
foreach ($expired as $block) { |
135
|
|
|
$this->unblock($block->get('id')); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get all |
141
|
|
|
* |
142
|
|
|
* @return Query |
143
|
|
|
*/ |
144
|
|
|
public function getAll() |
145
|
|
|
{ |
146
|
|
|
$blocklist = $this->find('assocUsers') |
147
|
|
|
->order(['UserBlocks.id' => 'DESC']); |
148
|
|
|
|
149
|
|
|
return $blocklist; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* gc finder |
154
|
|
|
* |
155
|
|
|
* @param Query $query query |
156
|
|
|
* @param array $options options |
157
|
|
|
* @return Query |
158
|
|
|
*/ |
159
|
|
|
public function findToGc(Query $query, array $options) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$query->where( |
162
|
|
|
[ |
163
|
|
|
'ends IS NOT' => null, |
164
|
|
|
'ends <' => bDate(), |
165
|
|
|
'ended IS' => null, |
166
|
|
|
] |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
return $query; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Select required fields from associated Users. |
174
|
|
|
* |
175
|
|
|
* Don't hydrate full user entities. |
176
|
|
|
* |
177
|
|
|
* @param Query $query query |
178
|
|
|
* @return Query |
179
|
|
|
*/ |
180
|
|
|
public function findAssocUsers(Query $query) |
181
|
|
|
{ |
182
|
|
|
$callback = function (Query $query) { |
183
|
|
|
return $query->select(['id', 'username']); |
184
|
|
|
}; |
185
|
|
|
$query->contain(['BlockedBy' => $callback, 'Users' => $callback]); |
186
|
|
|
|
187
|
|
|
return $query; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* update is blocked |
192
|
|
|
* |
193
|
|
|
* @param int $userId user-ID |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
|
|
protected function _updateIsBlocked($userId) |
197
|
|
|
{ |
198
|
|
|
$blocks = $this->find( |
199
|
|
|
'all', |
200
|
|
|
[ |
201
|
|
|
'conditions' => [ |
202
|
|
|
'ended IS' => null, |
203
|
|
|
'user_id' => $userId, |
204
|
|
|
], |
205
|
|
|
] |
206
|
|
|
)->first(); |
207
|
|
|
$user = $this->Users->get($userId, ['fields' => ['id', 'user_lock']]); |
208
|
|
|
$user->set('user_lock', !empty($blocks)); |
209
|
|
|
|
210
|
|
|
return $this->Users->save($user); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.