1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \ElggUser |
4
|
|
|
* |
5
|
|
|
* Representation of a "user" in the system. |
6
|
|
|
* |
7
|
|
|
* @package Elgg.Core |
8
|
|
|
* @subpackage DataModel.User |
9
|
|
|
* |
10
|
|
|
* @property string $name The display name that the user will be known by in the network |
11
|
|
|
* @property string $username The short, reference name for the user in the network |
12
|
|
|
* @property string $email The email address to which Elgg will send email notifications |
13
|
|
|
* @property string $language The language preference of the user (ISO 639-1 formatted) |
14
|
|
|
* @property string $banned 'yes' if the user is banned from the network, 'no' otherwise |
15
|
|
|
* @property string $admin 'yes' if the user is an administrator of the network, 'no' otherwise |
16
|
|
|
* @property-read string $password The legacy (salted MD5) password hash of the user |
17
|
|
|
* @property-read string $salt The salt used to create the legacy password hash |
18
|
|
|
* @property-read string $password_hash The hashed password of the user |
19
|
|
|
* @property-read int $prev_last_action A UNIX timestamp of the previous last action |
20
|
|
|
* @property-read int $last_login A UNIX timestamp of the last login |
21
|
|
|
* @property-read int $prev_last_login A UNIX timestamp of the previous login |
22
|
|
|
*/ |
23
|
|
|
class ElggUser extends \ElggEntity |
|
|
|
|
24
|
|
|
implements Friendable { |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initialize the attributes array. |
28
|
|
|
* This is vital to distinguish between metadata and base attributes. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
220 |
|
protected function initializeAttributes() { |
33
|
220 |
|
parent::initializeAttributes(); |
34
|
|
|
|
35
|
220 |
|
$this->attributes['type'] = "user"; |
36
|
220 |
|
$this->attributes += self::getExternalAttributes(); |
37
|
220 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get default values for attributes stored in a separate table |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
* @access private |
44
|
|
|
* |
45
|
|
|
* @see \Elgg\Database\EntityTable::getEntities |
46
|
|
|
*/ |
47
|
220 |
|
final public static function getExternalAttributes() { |
48
|
|
|
return [ |
49
|
220 |
|
'name' => null, |
50
|
220 |
|
'username' => null, |
51
|
220 |
|
'password_hash' => null, |
52
|
220 |
|
'email' => null, |
53
|
220 |
|
'language' => null, |
54
|
220 |
|
'banned' => "no", |
55
|
220 |
|
'admin' => 'no', |
56
|
220 |
|
'prev_last_action' => null, |
57
|
220 |
|
'last_login' => null, |
58
|
220 |
|
'prev_last_login' => null, |
59
|
220 |
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Construct a new user entity |
64
|
|
|
* |
65
|
|
|
* Plugin developers should only use the constructor to create a new entity. |
66
|
|
|
* To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
67
|
|
|
* |
68
|
|
|
* @param \stdClass $row Database row result. Default is null to create a new user. |
69
|
|
|
* |
70
|
|
|
* @throws IOException|InvalidParameterException if there was a problem creating the user. |
71
|
|
|
*/ |
72
|
220 |
|
public function __construct(\stdClass $row = null) { |
73
|
220 |
|
$this->initializeAttributes(); |
74
|
|
|
|
75
|
220 |
|
if ($row) { |
76
|
|
|
// Load the rest |
77
|
217 |
|
if (!$this->load($row)) { |
78
|
|
|
$msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid; |
79
|
|
|
throw new \IOException($msg); |
80
|
|
|
} |
81
|
217 |
|
} |
82
|
220 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Load the \ElggUser data from the database |
86
|
|
|
* |
87
|
|
|
* @param mixed $guid \ElggUser GUID or \stdClass database row from entity table |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
217 |
|
protected function load($guid) { |
92
|
217 |
|
$attr_loader = new \Elgg\AttributeLoader(get_class(), 'user', $this->attributes); |
93
|
217 |
|
$attr_loader->secondary_loader = 'get_user_entity_as_row'; |
94
|
|
|
|
95
|
217 |
|
$attrs = $attr_loader->getRequiredAttributes($guid); |
96
|
217 |
|
if (!$attrs) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
217 |
|
$this->attributes = $attrs; |
101
|
217 |
|
$this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues()); |
102
|
217 |
|
_elgg_services()->entityCache->set($this); |
103
|
|
|
|
104
|
217 |
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
protected function create() { |
112
|
|
|
global $CONFIG; |
|
|
|
|
113
|
|
|
|
114
|
|
|
$guid = parent::create(); |
115
|
|
|
$name = sanitize_string($this->name); |
|
|
|
|
116
|
|
|
$username = sanitize_string($this->username); |
|
|
|
|
117
|
|
|
$password_hash = sanitize_string($this->password_hash); |
|
|
|
|
118
|
|
|
$email = sanitize_string($this->email); |
|
|
|
|
119
|
|
|
$language = sanitize_string($this->language); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$query = "INSERT into {$CONFIG->dbprefix}users_entity |
122
|
|
|
(guid, name, username, password_hash, email, language) |
123
|
|
|
values ($guid, '$name', '$username', '$password_hash', '$email', '$language')"; |
124
|
|
|
|
125
|
|
|
$result = $this->getDatabase()->insertData($query); |
126
|
|
|
if ($result === false) { |
127
|
|
|
// TODO(evan): Throw an exception here? |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $guid; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
protected function update() { |
138
|
|
|
global $CONFIG; |
|
|
|
|
139
|
|
|
|
140
|
|
|
if (!parent::update()) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$guid = (int)$this->guid; |
145
|
|
|
$name = sanitize_string($this->name); |
|
|
|
|
146
|
|
|
$username = sanitize_string($this->username); |
|
|
|
|
147
|
|
|
$password_hash = sanitize_string($this->password_hash); |
|
|
|
|
148
|
|
|
$email = sanitize_string($this->email); |
|
|
|
|
149
|
|
|
$language = sanitize_string($this->language); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$query = "UPDATE {$CONFIG->dbprefix}users_entity |
152
|
|
|
SET name='$name', username='$username', |
153
|
|
|
password_hash='$password_hash', email='$email', language='$language' |
154
|
|
|
WHERE guid = $guid"; |
155
|
|
|
|
156
|
|
|
return $this->getDatabase()->updateData($query) !== false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get user language or default to site language |
161
|
|
|
* |
162
|
|
|
* @param string $fallback If this is provided, it will be returned if the user doesn't have a language set. |
163
|
|
|
* If null, the site language will be returned. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getLanguage($fallback = null) { |
168
|
|
|
if (!empty($this->language)) { |
169
|
|
|
return $this->language; |
170
|
|
|
} |
171
|
|
|
if ($fallback !== null) { |
172
|
|
|
return $fallback; |
173
|
|
|
} |
174
|
|
|
return elgg_get_config('language'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
3 |
|
public function getDisplayName() { |
181
|
3 |
|
return $this->name; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function setDisplayName($displayName) { |
188
|
|
|
$this->name = $displayName; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
219 |
|
public function __set($name, $value) { |
195
|
219 |
|
if (!array_key_exists($name, $this->attributes)) { |
196
|
1 |
|
parent::__set($name, $value); |
197
|
1 |
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
switch ($name) { |
201
|
218 |
|
case 'prev_last_action': |
202
|
218 |
|
case 'last_login': |
203
|
218 |
|
case 'prev_last_login': |
204
|
218 |
|
if ($value !== null) { |
205
|
1 |
|
$this->attributes[$name] = (int)$value; |
206
|
1 |
|
} else { |
207
|
217 |
|
$this->attributes[$name] = null; |
208
|
|
|
} |
209
|
218 |
|
break; |
210
|
|
|
|
211
|
|
|
case 'salt': |
212
|
|
|
case 'password': |
213
|
|
|
_elgg_services()->logger->error("User entities no longer contain salt/password"); |
214
|
|
|
break; |
215
|
|
|
|
216
|
|
|
// setting this not supported |
217
|
|
|
case 'password_hash': |
218
|
|
|
_elgg_services()->logger->error("password_hash is a readonly attribute."); |
219
|
|
|
break; |
220
|
|
|
|
221
|
|
|
default: |
222
|
|
|
parent::__set($name, $value); |
223
|
|
|
break; |
224
|
|
|
} |
225
|
218 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Ban this user. |
229
|
|
|
* |
230
|
|
|
* @param string $reason Optional reason |
231
|
|
|
* |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function ban($reason = "") { |
235
|
|
|
return ban_user($this->guid, $reason); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Unban this user. |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function unban() { |
244
|
|
|
return unban_user($this->guid); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Is this user banned or not? |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
18 |
|
public function isBanned() { |
253
|
18 |
|
return $this->banned == 'yes'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Is this user admin? |
258
|
|
|
* |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
24 |
|
public function isAdmin() { |
262
|
|
|
|
263
|
|
|
// for backward compatibility we need to pull this directly |
264
|
|
|
// from the attributes instead of using the magic methods. |
265
|
|
|
// this can be removed in 1.9 |
266
|
|
|
// return $this->admin == 'yes'; |
267
|
24 |
|
return $this->attributes['admin'] == 'yes'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Make the user an admin |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function makeAdmin() { |
276
|
|
|
|
277
|
|
|
if ($this->isAdmin()) { |
278
|
|
|
return true; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// If already saved, use the standard function. |
282
|
|
|
if ($this->guid && !make_user_admin($this->guid)) { |
283
|
|
|
return false; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// need to manually set attributes since they've already been loaded. |
287
|
|
|
$this->attributes['admin'] = 'yes'; |
288
|
|
|
|
289
|
|
|
return true; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Remove the admin flag for user |
294
|
|
|
* |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
public function removeAdmin() { |
298
|
|
|
|
299
|
|
|
if (!$this->isAdmin()) { |
300
|
|
|
return true; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// If already saved, use the standard function. |
304
|
|
|
if ($this->guid && !remove_user_admin($this->guid)) { |
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// need to manually set attributes since they've already been loaded. |
309
|
|
|
$this->attributes['admin'] = 'no'; |
310
|
|
|
|
311
|
|
|
return true; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Adds a user as a friend |
316
|
|
|
* |
317
|
|
|
* @param int $friend_guid The GUID of the user to add |
318
|
|
|
* @param bool $create_river_item Create the river item announcing this friendship |
319
|
|
|
* |
320
|
|
|
* @return bool |
321
|
|
|
*/ |
322
|
|
|
public function addFriend($friend_guid, $create_river_item = false) { |
323
|
|
|
if (!get_user($friend_guid)) { |
324
|
|
|
return false; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if (!add_entity_relationship($this->guid, "friend", $friend_guid)) { |
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if ($create_river_item) { |
332
|
|
|
elgg_create_river_item(array( |
333
|
|
|
'view' => 'river/relationship/friend/create', |
334
|
|
|
'action_type' => 'friend', |
335
|
|
|
'subject_guid' => $this->guid, |
336
|
|
|
'object_guid' => $friend_guid, |
337
|
|
|
)); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return true; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Removes a user as a friend |
345
|
|
|
* |
346
|
|
|
* @param int $friend_guid The GUID of the user to remove |
347
|
|
|
* @return bool |
348
|
|
|
*/ |
349
|
|
|
public function removeFriend($friend_guid) { |
350
|
|
|
return $this->removeRelationship($friend_guid, 'friend'); |
|
|
|
|
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Determines whether or not this user is a friend of the currently logged in user |
355
|
|
|
* |
356
|
|
|
* @return bool |
357
|
|
|
*/ |
358
|
|
|
public function isFriend() { |
359
|
|
|
return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid()); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Determines whether this user is friends with another user |
364
|
|
|
* |
365
|
|
|
* @param int $user_guid The GUID of the user to check against |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
|
public function isFriendsWith($user_guid) { |
370
|
|
|
return (bool)check_entity_relationship($this->guid, "friend", $user_guid); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Determines whether or not this user is another user's friend |
375
|
|
|
* |
376
|
|
|
* @param int $user_guid The GUID of the user to check against |
377
|
|
|
* |
378
|
|
|
* @return bool |
379
|
|
|
*/ |
380
|
|
|
public function isFriendOf($user_guid) { |
381
|
|
|
return (bool)check_entity_relationship($user_guid, "friend", $this->guid); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* {@inheritdoc} |
386
|
|
|
*/ |
387
|
|
|
public function getFriends(array $options = []) { |
388
|
|
|
$options['relationship'] = 'friend'; |
389
|
|
|
$options['relationship_guid'] = $this->getGUID(); |
390
|
|
|
$options['type'] = 'user'; |
391
|
|
|
|
392
|
|
|
return elgg_get_entities_from_relationship($options); |
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritdoc} |
397
|
|
|
*/ |
398
|
|
|
public function getFriendsOf(array $options = []) { |
399
|
|
|
$options['relationship'] = 'friend'; |
400
|
|
|
$options['relationship_guid'] = $this->getGUID(); |
401
|
|
|
$options['inverse_relationship'] = true; |
402
|
|
|
$options['type'] = 'user'; |
403
|
|
|
|
404
|
|
|
return elgg_get_entities_from_relationship($options); |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Gets the user's groups |
409
|
|
|
* |
410
|
|
|
* @param array $options Options array. |
411
|
|
|
* |
412
|
|
|
* @return array|false Array of \ElggGroup, or false, depending on success |
413
|
|
|
*/ |
414
|
|
|
public function getGroups(array $options = []) { |
415
|
|
|
$options['type'] = 'group'; |
416
|
|
|
$options['relationship'] = 'member'; |
417
|
|
|
$options['relationship_guid'] = $this->guid; |
418
|
|
|
|
419
|
|
|
return elgg_get_entities_from_relationship($options); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
425
|
|
|
public function getObjects(array $options = []) { |
426
|
|
|
$options['type'] = 'object'; |
427
|
|
|
$options['owner_guid'] = $this->getGUID(); |
428
|
|
|
|
429
|
|
|
return elgg_get_entities($options); |
|
|
|
|
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* {@inheritdoc} |
434
|
|
|
*/ |
435
|
|
|
public function getFriendsObjects(array $options = []) { |
436
|
|
|
$options['type'] = 'object'; |
437
|
|
|
$options['relationship'] = 'friend'; |
438
|
|
|
$options['relationship_guid'] = $this->getGUID(); |
439
|
|
|
$options['relationship_join_on'] = 'container_guid'; |
440
|
|
|
|
441
|
|
|
return elgg_get_entities_from_relationship($options); |
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Get a user's owner GUID |
446
|
|
|
* |
447
|
|
|
* Returns it's own GUID if the user is not owned. |
448
|
|
|
* |
449
|
|
|
* @return int |
450
|
|
|
*/ |
451
|
|
|
public function getOwnerGUID() { |
452
|
|
|
if ($this->owner_guid == 0) { |
453
|
|
|
return $this->guid; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
return $this->owner_guid; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* {@inheritdoc} |
461
|
|
|
*/ |
462
|
|
|
protected function prepareObject($object) { |
463
|
|
|
$object = parent::prepareObject($object); |
464
|
|
|
$object->name = $this->getDisplayName(); |
465
|
|
|
$object->username = $this->username; |
466
|
|
|
$object->language = $this->language; |
467
|
|
|
unset($object->read_access); |
468
|
|
|
return $object; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Can a user comment on this user? |
473
|
|
|
* |
474
|
|
|
* @see \ElggEntity::canComment() |
475
|
|
|
* |
476
|
|
|
* @param int $user_guid User guid (default is logged in user) |
477
|
|
|
* @param bool $default Default permission |
478
|
|
|
* @return bool |
479
|
|
|
* @since 1.8.0 |
480
|
|
|
*/ |
481
|
1 |
|
public function canComment($user_guid = 0, $default = null) { |
482
|
1 |
|
$result = parent::canComment($user_guid, $default); |
483
|
1 |
|
if ($result !== null) { |
484
|
|
|
return $result; |
485
|
|
|
} |
486
|
1 |
|
return false; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Set the necessary attribute to store a hash of the user's password. |
491
|
|
|
* |
492
|
|
|
* @tip You must save() to persist the attribute |
493
|
|
|
* |
494
|
|
|
* @param string $password The password to be hashed |
495
|
|
|
* @return void |
496
|
|
|
* @since 1.10.0 |
497
|
|
|
*/ |
498
|
|
|
public function setPassword($password) { |
499
|
|
|
$this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Enable or disable a notification delivery method |
504
|
|
|
* |
505
|
|
|
* @param string $method Method name |
506
|
|
|
* @param bool $enabled Enabled or disabled |
507
|
|
|
* @return bool |
508
|
|
|
*/ |
509
|
1 |
|
public function setNotificationSetting($method, $enabled = true) { |
510
|
1 |
|
$this->{"notification:method:$method"} = (int) $enabled; |
511
|
1 |
|
return (bool) $this->save(); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Returns users's notification settings |
516
|
|
|
* <code> |
517
|
|
|
* [ |
518
|
|
|
* 'email' => true, // enabled |
519
|
|
|
* 'ajax' => false, // disabled |
520
|
|
|
* ] |
521
|
|
|
* </code> |
522
|
|
|
* |
523
|
|
|
* @return array |
524
|
|
|
*/ |
525
|
19 |
|
public function getNotificationSettings() { |
526
|
|
|
|
527
|
19 |
|
$settings = []; |
528
|
|
|
|
529
|
19 |
|
$methods = _elgg_services()->notifications->getMethods(); |
530
|
19 |
|
foreach ($methods as $method) { |
531
|
19 |
|
$settings[$method] = (bool) $this->{"notification:method:$method"}; |
532
|
19 |
|
} |
533
|
|
|
|
534
|
19 |
|
return $settings; |
535
|
|
|
|
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|