Elgg  Version 3.0
ElggUser.php
Go to the documentation of this file.
1 <?php
2 
20 class ElggUser extends \ElggEntity
21  implements Friendable {
22 
26  protected function initializeAttributes() {
27  parent::initializeAttributes();
28  $this->attributes['subtype'] = 'user';
29 
30  // Before Elgg 3.0 this was handled by database logic
31  $this->banned = 'no';
32  $this->admin = 'no';
33  $this->language = elgg_get_config('language');
34  $this->prev_last_action = 0;
35  $this->last_login = 0;
36  $this->prev_last_login = 0;
37  }
38 
42  public function getType() {
43  return 'user';
44  }
45 
54  public function getLanguage($fallback = null) {
55  if (!empty($this->language)) {
56  return $this->language;
57  }
58  if ($fallback !== null) {
59  return $fallback;
60  }
61  return elgg_get_config('language');
62  }
63 
67  public function __set($name, $value) {
68  switch ($name) {
69  case 'salt':
70  case 'password':
71  _elgg_services()->logger->error("User entities no longer contain {$name}");
72  return;
73  case 'password_hash':
74  _elgg_services()->logger->error("password_hash is a readonly attribute.");
75  return;
76  case 'email':
77  try {
78  elgg()->accounts->assertValidEmail($value);
79  } catch (RegistrationException $ex) {
80  throw new InvalidParameterException($ex->getCode());
81  }
82  break;
83  case 'username':
84  try {
85  elgg()->accounts->assertValidUsername($value);
86  } catch (RegistrationException $ex) {
87  throw new InvalidParameterException($ex->getCode());
88  }
89  $existing_user = get_user_by_username($value);
90  if ($existing_user && ($existing_user->guid !== $this->guid)) {
91  throw new InvalidParameterException("{$name} is supposed to be unique for ElggUser");
92  }
93  break;
94  case 'admin':
95  case 'banned':
96  if (!in_array($value, ['yes', 'no'], true)) {
97  throw new InvalidArgumentException("{$name} only supports 'yes' or 'no' value");
98  }
99  break;
100  }
101 
102  parent::__set($name, $value);
103  }
104 
108  public function getURL() {
109 
110  $result = parent::getURL();
111  if ($result !== '') {
112  return $result;
113  }
114 
115  return elgg_normalize_url("user/view/{$this->guid}");
116  }
117 
125  public function ban($reason = '') {
126 
127  if (!$this->canEdit()) {
128  return false;
129  }
130 
131  if (!_elgg_services()->events->trigger('ban', 'user', $this)) {
132  return false;
133  }
134 
135  $this->ban_reason = $reason;
136  $this->banned = 'yes';
137 
138  $this->invalidateCache();
139 
140  return true;
141  }
142 
148  public function unban() {
149 
150  if (!$this->canEdit()) {
151  return false;
152  }
153 
154  if (!_elgg_services()->events->trigger('unban', 'user', $this)) {
155  return false;
156  }
157 
158  unset($this->ban_reason);
159  $this->banned = 'no';
160 
161  $this->invalidateCache();
162 
163  return true;
164  }
165 
171  public function isBanned() {
172  return $this->banned == 'yes';
173  }
174 
180  public function isAdmin() {
181  $ia = _elgg_services()->session->setIgnoreAccess(true);
182  $is_admin = ($this->admin == 'yes');
183  _elgg_services()->session->setIgnoreAccess($ia);
184 
185  return $is_admin;
186  }
187 
193  public function makeAdmin() {
194 
195  if ($this->isAdmin()) {
196  return true;
197  }
198 
199  if (!_elgg_services()->events->trigger('make_admin', 'user', $this)) {
200  return false;
201  }
202 
203  $this->admin = 'yes';
204 
205  $this->invalidateCache();
206 
207  return true;
208  }
209 
215  public function removeAdmin() {
216 
217  if (!$this->isAdmin()) {
218  return true;
219  }
220 
221  if (!_elgg_services()->events->trigger('remove_admin', 'user', $this)) {
222  return false;
223  }
224 
225  $this->admin = 'no';
226 
227  $this->invalidateCache();
228 
229  return true;
230  }
231 
237  public function setLastLogin() {
238 
239  $time = $this->getCurrentTime()->getTimestamp();
240 
241  if ($this->last_login == $time) {
242  // no change required
243  return;
244  }
245 
246  // these writes actually work, we just type hint read-only.
247  $this->prev_last_login = $this->last_login;
248  $this->last_login = $time;
249  }
250 
258  public function setLastAction() {
259 
260  $time = $this->getCurrentTime()->getTimestamp();
261 
262  if ($this->last_action == $time) {
263  // no change required
264  return;
265  }
266 
267  $user = $this;
268 
269  elgg_register_event_handler('shutdown', 'system', function () use ($user, $time) {
270  // these writes actually work, we just type hint read-only.
271  $user->prev_last_action = $user->last_action;
272 
273  $user->updateLastAction($time);
274  });
275  }
276 
282  public function isValidated() {
283  if (!isset($this->validated)) {
284  return null;
285  }
286  return (bool) $this->validated;
287  }
288 
296  public function setValidationStatus($status, $method = '') {
297 
298  $this->validated = $status;
299  $this->validated_method = $method;
300 
301  if ((bool) $status) {
302  // make sure the user is enabled
303  if (!$this->isEnabled()) {
304  $this->enable();
305  }
306 
307  // let the system know the user is validated
308  _elgg_services()->events->triggerAfter('validate', 'user', $this);
309  } else {
310  _elgg_services()->events->triggerAfter('invalidate', 'user', $this);
311  }
312  }
313 
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) {
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 
349  public function removeFriend($friend_guid) {
350  return $this->removeRelationship($friend_guid, 'friend');
351  }
352 
358  public function isFriend() {
359  return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
360  }
361 
369  public function isFriendsWith($user_guid) {
370  return (bool) check_entity_relationship($this->guid, "friend", $user_guid);
371  }
372 
380  public function isFriendOf($user_guid) {
381  return (bool) check_entity_relationship($user_guid, "friend", $this->guid);
382  }
383 
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($options);
393  }
394 
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($options);
405  }
406 
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($options);
420  }
421 
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 
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($options);
442  }
443 
451  public function getOwnerGUID() {
452  if ($this->owner_guid == 0) {
453  return $this->guid;
454  }
455 
456  return $this->owner_guid;
457  }
458 
462  protected function prepareObject(\Elgg\Export\Entity $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 
481  public function canComment($user_guid = 0, $default = null) {
482  return false;
483  }
484 
492  public function setPassword($password) {
493  $this->setMetadata('password_hash', _elgg_services()->passwords->generateHash($password));
494  }
495 
503  public function setNotificationSetting($method, $enabled = true) {
504  $this->{"notification:method:$method"} = (int) $enabled;
505  return (bool) $this->save();
506  }
507 
519  public function getNotificationSettings() {
520 
521  $settings = [];
522 
523  $methods = _elgg_services()->notifications->getMethods();
524  foreach ($methods as $method) {
525  $settings[$method] = (bool) $this->{"notification:method:$method"};
526  }
527 
528  return $settings;
529 
530  }
531 
539  public function cache($persist = true) {
540  if ($persist && $this->username) {
541  $tmp = $this->volatile;
542 
543  // don't store volatile data
544  $this->volatile = [];
545 
546  _elgg_services()->dataCache->usernames->save($this->username, $this);
547 
548  $this->volatile = $tmp;
549  }
550 
551  parent::cache($persist);
552  }
553 
560  public function invalidateCache() {
561  if ($this->username) {
562  _elgg_services()->dataCache->usernames->delete($this->username);
563  }
564 
565  parent::invalidateCache();
566  }
567 
571  public function delete($recursive = true) {
572  $result = parent::delete($recursive);
573  if ($result) {
574  // cleanup remember me cookie records
575  _elgg_services()->persistentLogin->removeAllHashes($this);
576  }
577 
578  return $result;
579  }
580 }
getFriends(array $options=[])
{Gets this entity&#39;s friends.Options array. See elgg_get_entities() for a list of options. &#39;relationship_guid&#39; is set to this entity, relationship name to &#39;friend&#39; and type to &#39;user&#39;.[]|int|mixed}
Definition: ElggUser.php:387
addFriend($friend_guid, $create_river_item=false)
Adds a user as a friend.
Definition: ElggUser.php:322
enable($recursive=true)
Enable the entity.
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
getLanguage($fallback=null)
Get user language or default to site language.
Definition: ElggUser.php:54
$last_login
Definition: online.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$username
Definition: delete.php:23
get_user($guid)
Get a user object from a GUID.
Definition: users.php:40
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:193
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
elgg_normalize_url($url)
Definition: output.php:186
setPassword($password)
Set the necessary metadata to store a hash of the user&#39;s password.
Definition: ElggUser.php:492
setNotificationSetting($method, $enabled=true)
Enable or disable a notification delivery method.
Definition: ElggUser.php:503
save()
Save an entity.
getGUID()
Returns the guid.
invalidateCache()
Invalidate cache for entity.
Definition: ElggUser.php:560
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:27
setLastLogin()
Sets the last logon time of the user to right now.
Definition: ElggUser.php:237
$guid
Removes an admin notice.
elgg_create_river_item(array $options=[])
Adds an item to the river.
Definition: river.php:33
initializeAttributes()
{}
Definition: ElggUser.php:26
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggUser.php:462
canEdit($user_guid=0)
Can a user edit this entity?
$options
Elgg admin footer.
Definition: footer.php:6
$new_user language
Definition: useradd.php:57
get_user_by_username($username)
Get user by username.
Definition: users.php:61
removeRelationship($guid_two, $relationship)
Remove a relationship.
Definition: ElggEntity.php:571
$user_guid
Validate a user.
Definition: validate.php:6
$owner_guid
Configuration exception.
getURL()
{}
Definition: ElggUser.php:108
getObjects(array $options=[])
{Get an array of owned by this entity.Options array. See elgg_get_entities() for a list of options...
Definition: ElggUser.php:425
if(!$user||!$user->canEdit()) $password
setLastAction()
Sets the last action time of the given user to right now.
Definition: ElggUser.php:258
unban()
Unban this user.
Definition: ElggUser.php:148
$language
Definition: useradd.php:17
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:358
$enabled
Sample cli installer script.
$user
Definition: ban.php:7
isFriendOf($user_guid)
Determines whether or not this user is another user&#39;s friend.
Definition: ElggUser.php:380
ban($reason= '')
Ban this user.
Definition: ElggUser.php:125
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
getFriendsOf(array $options=[])
{Gets users who have made this entity a friend.Options array. See elgg_get_entities() for a list of o...
Definition: ElggUser.php:398
cache($persist=true)
Cache the entity in a session and persisted caches.
Definition: ElggUser.php:539
$default
Definition: checkbox.php:35
getOwnerGUID()
Get a user&#39;s owner GUID.
Definition: ElggUser.php:451
isEnabled()
Is this entity enabled?
getType()
{}
Definition: ElggUser.php:42
getNotificationSettings()
Returns users&#39;s notification settings [ &#39;email&#39; => true, // enabled &#39;ajax&#39; => false, // disabled ]
Definition: ElggUser.php:519
setMetadata($name, $value, $value_type= '', $multiple=false)
Set metadata on this entity.
Definition: ElggEntity.php:366
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
getGroups(array $options=[])
Gets the user&#39;s groups.
Definition: ElggUser.php:414
$value
Definition: debugging.php:7
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:349
isAdmin()
Is this user admin?
Definition: ElggUser.php:180
isValidated()
Gets the validation status of a user.
Definition: ElggUser.php:282
setValidationStatus($status, $method= '')
Set the validation status for a user.
Definition: ElggUser.php:296
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:215
if(elgg_get_config('allow_registration')==false) $friend_guid
Definition: register.php:20
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
isBanned()
Is this user banned or not?
Definition: ElggUser.php:171
canComment($user_guid=0, $default=null)
Can a user comment on this user?
Definition: ElggUser.php:481
$settings
Definition: settings.php:3
__set($name, $value)
{}
Definition: ElggUser.php:67
getFriendsObjects(array $options=[])
{Get an array of owned by this entity&#39;s friends.Options array. See elgg_get_entities() for a list of...
Definition: ElggUser.php:435
var elgg
Definition: elgglib.js:4
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:307
isFriendsWith($user_guid)
Determines whether this user is friends with another user.
Definition: ElggUser.php:369
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
$comment owner_guid
Definition: save.php:45