1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
use Jitamin\Foundation\Security\Token; |
17
|
|
|
use PicoDb\Database; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* User model. |
21
|
|
|
*/ |
22
|
|
|
class UserModel extends Model |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* SQL table name. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const TABLE = 'users'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Id used for everybody (filtering). |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
const EVERYBODY_ID = -1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return true if the user exists. |
40
|
|
|
* |
41
|
|
|
* @param int $user_id User id |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function exists($user_id) |
46
|
|
|
{ |
47
|
|
|
return $this->db->table(self::TABLE)->eq('id', $user_id)->exists(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return true if the user is active. |
52
|
|
|
* |
53
|
|
|
* @param int $user_id User id |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function isActive($user_id) |
58
|
|
|
{ |
59
|
|
|
return $this->db->table(self::TABLE)->eq('id', $user_id)->eq('is_active', 1)->exists(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get query to fetch all users. |
64
|
|
|
* |
65
|
|
|
* @return \PicoDb\Table |
66
|
|
|
*/ |
67
|
|
|
public function getQuery() |
68
|
|
|
{ |
69
|
|
|
return $this->db->table(self::TABLE); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return true is the given user id is administrator. |
74
|
|
|
* |
75
|
|
|
* @param int $user_id User id |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isAdmin($user_id) |
80
|
|
|
{ |
81
|
|
|
return $this->userSession->isAdmin() || // Avoid SQL query if connected |
|
|
|
|
82
|
|
|
$this->db |
|
|
|
|
83
|
|
|
->table(self::TABLE) |
84
|
|
|
->eq('id', $user_id) |
85
|
|
|
->eq('role', Role::APP_ADMIN) |
86
|
|
|
->exists(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get a specific user by id. |
91
|
|
|
* |
92
|
|
|
* @param int $user_id User id |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getById($user_id) |
97
|
|
|
{ |
98
|
|
|
return $this->db->table(self::TABLE)->eq('id', $user_id)->findOne(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a specific user by the Google id. |
103
|
|
|
* |
104
|
|
|
* @param string $column |
105
|
|
|
* @param string $id |
106
|
|
|
* |
107
|
|
|
* @return array|bool |
108
|
|
|
*/ |
109
|
|
|
public function getByExternalId($column, $id) |
110
|
|
|
{ |
111
|
|
|
if (empty($id)) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->db->table(self::TABLE)->eq($column, $id)->findOne(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get a specific user by the username. |
120
|
|
|
* |
121
|
|
|
* @param string $username Username |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getByUsername($username) |
126
|
|
|
{ |
127
|
|
|
return $this->db->table(self::TABLE)->eq('username', $username)->findOne(); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get user_id by username. |
132
|
|
|
* |
133
|
|
|
* @param string $username Username |
134
|
|
|
* |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
public function getIdByUsername($username) |
138
|
|
|
{ |
139
|
|
|
return $this->db->table(self::TABLE)->eq('username', $username)->findOneColumn('id'); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get a specific user by the email address. |
144
|
|
|
* |
145
|
|
|
* @param string $email Email |
146
|
|
|
* |
147
|
|
|
* @return array|bool |
148
|
|
|
*/ |
149
|
|
|
public function getByEmail($email) |
150
|
|
|
{ |
151
|
|
|
if (empty($email)) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->db->table(self::TABLE)->eq('email', $email)->findOne(); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Fetch user by using the token. |
160
|
|
|
* |
161
|
|
|
* @param string $token Token |
162
|
|
|
* |
163
|
|
|
* @return array|bool |
164
|
|
|
*/ |
165
|
|
|
public function getByToken($token) |
166
|
|
|
{ |
167
|
|
|
if (empty($token)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->db->table(self::TABLE)->eq('token', $token)->findOne(); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get all users. |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function getAll() |
180
|
|
|
{ |
181
|
|
|
return $this->getQuery()->asc('username')->findAll(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the number of users. |
186
|
|
|
* |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
|
|
public function count() |
190
|
|
|
{ |
191
|
|
|
return $this->db->table(self::TABLE)->count(); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* List all users (key-value pairs with id/username). |
196
|
|
|
* |
197
|
|
|
* @param bool $prepend Prepend "All users" |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getActiveUsersList($prepend = false) |
202
|
|
|
{ |
203
|
|
|
$users = $this->db->table(self::TABLE)->eq('is_active', 1)->columns('id', 'username', 'name')->findAll(); |
|
|
|
|
204
|
|
|
$listing = $this->prepareList($users); |
205
|
|
|
|
206
|
|
|
if ($prepend) { |
207
|
|
|
return [self::EVERYBODY_ID => t('Everybody')] + $listing; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $listing; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Common method to prepare a user list. |
215
|
|
|
* |
216
|
|
|
* @param array $users Users list (from database) |
217
|
|
|
* |
218
|
|
|
* @return array Formated list |
219
|
|
|
*/ |
220
|
|
|
public function prepareList(array $users) |
221
|
|
|
{ |
222
|
|
|
$result = []; |
223
|
|
|
|
224
|
|
|
foreach ($users as $user) { |
225
|
|
|
$result[$user['id']] = $this->helper->user->getFullname($user); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
asort($result); |
229
|
|
|
|
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Prepare values before an update or a create. |
235
|
|
|
* |
236
|
|
|
* @param array $values Form values |
237
|
|
|
*/ |
238
|
|
|
public function prepare(array &$values) |
239
|
|
|
{ |
240
|
|
|
if (isset($values['password'])) { |
241
|
|
|
if (!empty($values['password'])) { |
242
|
|
|
$values['password'] = bcrypt($values['password']); |
243
|
|
|
} else { |
244
|
|
|
unset($values['password']); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->helper->model->removeFields($values, ['confirmation', 'current_password']); |
|
|
|
|
249
|
|
|
$this->helper->model->resetFields($values, ['is_ldap_user', 'disable_login_form']); |
|
|
|
|
250
|
|
|
$this->helper->model->convertNullFields($values, ['gitlab_id']); |
|
|
|
|
251
|
|
|
$this->helper->model->convertIntegerFields($values, ['gitlab_id']); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Add a new user in the database. |
256
|
|
|
* |
257
|
|
|
* @param array $values Form values |
258
|
|
|
* |
259
|
|
|
* @return bool|int |
260
|
|
|
*/ |
261
|
|
|
public function create(array $values) |
262
|
|
|
{ |
263
|
|
|
$this->prepare($values); |
264
|
|
|
|
265
|
|
|
return $this->db->table(self::TABLE)->persist($values); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Modify a new user. |
270
|
|
|
* |
271
|
|
|
* @param array $values Form values |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function update(array $values) |
276
|
|
|
{ |
277
|
|
|
$this->prepare($values); |
278
|
|
|
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values); |
|
|
|
|
279
|
|
|
$this->userSession->refresh($values['id']); |
|
|
|
|
280
|
|
|
|
281
|
|
|
return $result; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Disable a specific user. |
286
|
|
|
* |
287
|
|
|
* @param int $user_id |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function disable($user_id) |
292
|
|
|
{ |
293
|
|
|
return $this->db->table(self::TABLE)->eq('id', $user_id)->update(['is_active' => 0]); |
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Enable a specific user. |
298
|
|
|
* |
299
|
|
|
* @param int $user_id |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
public function enable($user_id) |
304
|
|
|
{ |
305
|
|
|
return $this->db->table(self::TABLE)->eq('id', $user_id)->update(['is_active' => 1]); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Remove a specific user. |
310
|
|
|
* |
311
|
|
|
* @param int $user_id User id |
312
|
|
|
* |
313
|
|
|
* @return bool |
314
|
|
|
*/ |
315
|
|
|
public function remove($user_id) |
316
|
|
|
{ |
317
|
|
|
$this->avatarModel->remove($user_id); |
|
|
|
|
318
|
|
|
|
319
|
|
|
return $this->db->transaction(function (Database $db) use ($user_id) { |
|
|
|
|
320
|
|
|
|
321
|
|
|
// All assigned tasks are now unassigned (no foreign key) |
322
|
|
|
if (!$db->table(TaskModel::TABLE)->eq('owner_id', $user_id)->update(['owner_id' => 0])) { |
323
|
|
|
return false; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// All assigned subtasks are now unassigned (no foreign key) |
327
|
|
View Code Duplication |
if (!$db->table(SubtaskModel::TABLE)->eq('user_id', $user_id)->update(['user_id' => 0])) { |
|
|
|
|
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// All comments are not assigned anymore (no foreign key) |
332
|
|
View Code Duplication |
if (!$db->table(CommentModel::TABLE)->eq('user_id', $user_id)->update(['user_id' => 0])) { |
|
|
|
|
333
|
|
|
return false; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
// All private projects are removed |
337
|
|
|
$project_ids = $db->table(ProjectModel::TABLE) |
338
|
|
|
->eq('is_private', 1) |
339
|
|
|
->eq(ProjectUserRoleModel::TABLE.'.user_id', $user_id) |
340
|
|
|
->join(ProjectUserRoleModel::TABLE, 'project_id', 'id') |
341
|
|
|
->findAllByColumn(ProjectModel::TABLE.'.id'); |
342
|
|
|
|
343
|
|
|
if (!empty($project_ids)) { |
344
|
|
|
$db->table(ProjectModel::TABLE)->in('id', $project_ids)->remove(); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// Finally remove the user |
348
|
|
|
if (!$db->table(UserModel::TABLE)->eq('id', $user_id)->remove()) { |
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
}); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Enable public access for a user. |
356
|
|
|
* |
357
|
|
|
* @param int $user_id User id |
358
|
|
|
* |
359
|
|
|
* @return bool |
360
|
|
|
*/ |
361
|
|
|
public function enablePublicAccess($user_id) |
362
|
|
|
{ |
363
|
|
|
return $this->db |
|
|
|
|
364
|
|
|
->table(self::TABLE) |
365
|
|
|
->eq('id', $user_id) |
366
|
|
|
->save(['token' => Token::getToken()]); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Disable public access for a user. |
371
|
|
|
* |
372
|
|
|
* @param int $user_id User id |
373
|
|
|
* |
374
|
|
|
* @return bool |
375
|
|
|
*/ |
376
|
|
|
public function disablePublicAccess($user_id) |
377
|
|
|
{ |
378
|
|
|
return $this->db |
|
|
|
|
379
|
|
|
->table(self::TABLE) |
380
|
|
|
->eq('id', $user_id) |
381
|
|
|
->save(['token' => '']); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.