Elgg  Version 1.10
ElggUser.php
Go to the documentation of this file.
1 <?php
20 class ElggUser extends \ElggEntity
21  implements Friendable {
22 
29  protected function initializeAttributes() {
30  parent::initializeAttributes();
31 
32  $this->attributes['type'] = "user";
33  $this->attributes['name'] = null;
34  $this->attributes['username'] = null;
35  $this->attributes['password'] = null;
36  $this->attributes['salt'] = null;
37  $this->attributes['password_hash'] = null;
38  $this->attributes['email'] = null;
39  $this->attributes['language'] = null;
40  $this->attributes['banned'] = "no";
41  $this->attributes['admin'] = 'no';
42  $this->attributes['prev_last_action'] = null;
43  $this->attributes['last_login'] = null;
44  $this->attributes['prev_last_login'] = null;
45  $this->tables_split = 2;
46  }
47 
58  public function __construct($row = null) {
59  $this->initializeAttributes();
60 
61  // compatibility for 1.7 api.
62  $this->initialise_attributes(false);
63 
64  if (!empty($row)) {
65  // Is $row is a DB entity row
66  if ($row instanceof \stdClass) {
67  // Load the rest
68  if (!$this->load($row)) {
69  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
70  throw new \IOException($msg);
71  }
72  } else if (is_string($row)) {
73  // $row is a username
74  elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
76  if ($user) {
77  foreach ($user->attributes as $key => $value) {
78  $this->attributes[$key] = $value;
79  }
80  }
81  } else if ($row instanceof \ElggUser) {
82  // $row is an \ElggUser so this is a copy constructor
83  elgg_deprecated_notice('This type of usage of the \ElggUser constructor was deprecated. Please use the clone method.', 1.7);
84  foreach ($row->attributes as $key => $value) {
85  $this->attributes[$key] = $value;
86  }
87  } else if (is_numeric($row)) {
88  // $row is a GUID so load entity
89  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
90  if (!$this->load($row)) {
91  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
92  }
93  } else {
94  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
95  }
96  }
97  }
98 
106  protected function load($guid) {
107  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'user', $this->attributes);
108  $attr_loader->secondary_loader = 'get_user_entity_as_row';
109 
110  $attrs = $attr_loader->getRequiredAttributes($guid);
111  if (!$attrs) {
112  return false;
113  }
114 
115  $this->attributes = $attrs;
116  $this->tables_loaded = 2;
117  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
118  _elgg_cache_entity($this);
119 
120  return true;
121  }
122 
123 
127  protected function create() {
128  global $CONFIG;
129 
130  $guid = parent::create();
131  $name = sanitize_string($this->name);
132  $username = sanitize_string($this->username);
133  $password = sanitize_string($this->password);
134  $salt = sanitize_string($this->salt);
135  $password_hash = sanitize_string($this->password_hash);
136  $email = sanitize_string($this->email);
138 
139  $query = "INSERT into {$CONFIG->dbprefix}users_entity
140  (guid, name, username, password, salt, password_hash, email, language)
141  values ($guid, '$name', '$username', '$password', '$salt', '$password_hash', '$email', '$language')";
142 
143  $result = $this->getDatabase()->insertData($query);
144  if ($result === false) {
145  // TODO(evan): Throw an exception here?
146  return false;
147  }
148 
149  return $guid;
150  }
151 
155  protected function update() {
156  global $CONFIG;
157 
158  if (!parent::update()) {
159  return false;
160  }
161 
162  $guid = (int)$this->guid;
163  $name = sanitize_string($this->name);
164  $username = sanitize_string($this->username);
165  $password = sanitize_string($this->password);
166  $salt = sanitize_string($this->salt);
167  $password_hash = sanitize_string($this->password_hash);
168  $email = sanitize_string($this->email);
170 
171  $query = "UPDATE {$CONFIG->dbprefix}users_entity
172  SET name='$name', username='$username', password='$password', salt='$salt',
173  password_hash='$password_hash', email='$email', language='$language'
174  WHERE guid = $guid";
175 
176  return $this->getDatabase()->updateData($query) !== false;
177  }
178 
184  public function delete() {
185  global $USERNAME_TO_GUID_MAP_CACHE;
186 
187  // clear cache
188  if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
189  unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
190  }
191 
192  // Delete entity
193  return parent::delete();
194  }
195 
199  public function getDisplayName() {
200  return $this->name;
201  }
202 
206  public function setDisplayName($displayName) {
207  $this->name = $displayName;
208  }
209 
213  public function __set($name, $value) {
214  if (!array_key_exists($name, $this->attributes)) {
215  parent::__set($name, $value);
216  return;
217  }
218 
219  switch ($name) {
220  case 'prev_last_action':
221  case 'last_login':
222  case 'prev_last_login':
223  if ($value !== null) {
224  $this->attributes[$name] = (int)$value;
225  } else {
226  $this->attributes[$name] = null;
227  }
228  break;
229 
230  case 'salt':
231  case 'password':
232  elgg_deprecated_notice("Setting salt/password directly is deprecated. Use ElggUser::setPassword().", "1.10");
233  $this->attributes[$name] = $value;
234 
235  // this is emptied so that the user is not left with two usable hashes
236  $this->attributes['password_hash'] = '';
237 
238  break;
239 
240  // setting this not supported
241  case 'password_hash':
242  _elgg_services()->logger->error("password_hash is now an attribute of ElggUser and cannot be set.");
243  return;
244  break;
245 
246  default:
247  parent::__set($name, $value);
248  break;
249  }
250  }
251 
255  public function set($name, $value) {
256  elgg_deprecated_notice("Use -> instead of set()", 1.9);
257  $this->__set($name, $value);
258 
259  return true;
260  }
261 
269  public function ban($reason = "") {
270  return ban_user($this->guid, $reason);
271  }
272 
278  public function unban() {
279  return unban_user($this->guid);
280  }
281 
287  public function isBanned() {
288  return $this->banned == 'yes';
289  }
290 
296  public function isAdmin() {
297 
298  // for backward compatibility we need to pull this directly
299  // from the attributes instead of using the magic methods.
300  // this can be removed in 1.9
301  // return $this->admin == 'yes';
302  return $this->attributes['admin'] == 'yes';
303  }
304 
310  public function makeAdmin() {
311  // If already saved, use the standard function.
312  if ($this->guid && !make_user_admin($this->guid)) {
313  return false;
314  }
315 
316  // need to manually set attributes since they've already been loaded.
317  $this->attributes['admin'] = 'yes';
318 
319  return true;
320  }
321 
327  public function removeAdmin() {
328  // If already saved, use the standard function.
329  if ($this->guid && !remove_user_admin($this->guid)) {
330  return false;
331  }
332 
333  // need to manually set attributes since they've already been loaded.
334  $this->attributes['admin'] = 'no';
335 
336  return true;
337  }
338 
348  public function getSites($options = "", $limit = 10, $offset = 0) {
349  if (is_string($options)) {
350  elgg_deprecated_notice('\ElggUser::getSites() takes an options array', 1.9);
351  return get_user_sites($this->getGUID(), $limit, $offset);
352  }
353 
354  return parent::getSites($options);
355  }
356 
364  public function addToSite($site) {
365  if (is_numeric($site)) {
366  elgg_deprecated_notice('\ElggUser::addToSite() takes a site entity', 1.9);
367  return add_site_user($site, $this->getGUID());
368  }
369 
370  return parent::addToSite($site);
371  }
372 
380  public function removeFromSite($site) {
381  if (is_numeric($site)) {
382  elgg_deprecated_notice('\ElggUser::removeFromSite() takes a site entity', 1.9);
383  return remove_site_user($site, $this->guid);
384  }
385 
386  return parent::removeFromSite($site);
387  }
388 
397  public function addFriend($friend_guid, $create_river_item = false) {
398  if (!get_user($friend_guid)) {
399  return false;
400  }
401 
402  if (!add_entity_relationship($this->guid, "friend", $friend_guid)) {
403  return false;
404  }
405 
406  if ($create_river_item) {
408  'view' => 'river/relationship/friend/create',
409  'action_type' => 'friend',
410  'subject_guid' => $this->guid,
411  'object_guid' => $friend_guid,
412  ));
413  }
414 
415  return true;
416  }
417 
425  public function removeFriend($friend_guid) {
426  if (!get_user($friend_guid)) {
427  return false;
428  }
429 
430  // @todo this should be done with a plugin hook handler on the delete relationship
431  // perform cleanup for access lists.
432  $collections = get_user_access_collections($this->guid);
433  if ($collections) {
434  foreach ($collections as $collection) {
436  }
437  }
438 
439  return remove_entity_relationship($this->guid, "friend", $friend_guid);
440  }
441 
447  public function isFriend() {
448  return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
449  }
450 
458  public function isFriendsWith($user_guid) {
459  return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
460  }
461 
469  public function isFriendOf($user_guid) {
470  return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
471  }
472 
484  public function getFriends($options = array(), $limit = 10, $offset = 0) {
485  if (is_array($options)) {
486  $options['relationship'] = 'friend';
487  $options['relationship_guid'] = $this->getGUID();
488  $options['type'] = 'user';
490  } else {
491  elgg_deprecated_notice("\ElggUser::getFriends takes an options array", 1.9);
493  'relationship' => 'friend',
494  'relationship_guid' => $this->guid,
495  'type' => 'user',
496  'subtype' => $options,
497  'limit' => $limit,
498  'offset' => $offset,
499  ));
500  }
501  }
502 
515  public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
516  if (is_array($options)) {
517  $options['relationship'] = 'friend';
518  $options['relationship_guid'] = $this->getGUID();
519  $options['inverse_relationship'] = true;
520  $options['type'] = 'user';
522  } else {
523  elgg_deprecated_notice("\ElggUser::getFriendsOf takes an options array", 1.9);
525  'relationship' => 'friend',
526  'relationship_guid' => $this->guid,
527  'type' => 'user',
528  'subtype' => $options,
529  'limit' => $limit,
530  'offset' => $offset,
531  ));
532  }
533  }
534 
546  public function listFriends($subtype = "", $limit = 10, array $vars = array()) {
547  elgg_deprecated_notice('\ElggUser::listFriends() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
548  $defaults = array(
549  'type' => 'user',
550  'relationship' => 'friend',
551  'relationship_guid' => $this->guid,
552  'limit' => $limit,
553  'full_view' => false,
554  );
555 
556  $options = array_merge($defaults, $vars);
557 
558  if ($subtype) {
559  $options['subtype'] = $subtype;
560  }
561 
563  }
564 
574  public function getGroups($options = "", $limit = 10, $offset = 0) {
575  if (is_string($options)) {
576  elgg_deprecated_notice('\ElggUser::getGroups() takes an options array', 1.9);
577  $subtype = $options;
578  $options = array(
579  'type' => 'group',
580  'relationship' => 'member',
581  'relationship_guid' => $this->guid,
582  'limit' => $limit,
583  'offset' => $offset,
584  );
585 
586  if ($subtype) {
587  $options['subtype'] = $subtype;
588  }
589  } else {
590  $options['type'] = 'group';
591  $options['relationship'] = 'member';
592  $options['relationship_guid'] = $this->guid;
593  }
594 
596  }
597 
608  public function listGroups($subtype = "", $limit = 10, $offset = 0) {
609  elgg_deprecated_notice('Elgg::listGroups is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
610  $options = array(
611  'type' => 'group',
612  'relationship' => 'member',
613  'relationship_guid' => $this->guid,
614  'limit' => $limit,
615  'offset' => $offset,
616  'full_view' => false,
617  );
618 
619  if ($subtype) {
620  $options['subtype'] = $subtype;
621  }
622 
624  }
625 
636  public function getObjects($options = array(), $limit = 10, $offset = 0) {
637  if (is_array($options)) {
638  $options['type'] = 'object';
639  $options['owner_guid'] = $this->getGUID();
640  return elgg_get_entities($options);
641  } else {
642  elgg_deprecated_notice("\ElggUser::getObjects takes an options array", 1.9);
643  return elgg_get_entities(array(
644  'type' => 'object',
645  'subtype' => $options,
646  'owner_guid' => $this->getGUID(),
647  'limit' => $limit,
648  'offset' => $offset
649  ));
650  }
651  }
652 
665  public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
666  if (is_array($options)) {
667  $options['type'] = 'object';
668  $options['relationship'] = 'friend';
669  $options['relationship_guid'] = $this->getGUID();
670  $options['relationship_join_on'] = 'container_guid';
672  } else {
673  elgg_deprecated_notice("\ElggUser::getFriendsObjects takes an options array", 1.9);
675  'type' => 'object',
676  'subtype' => $options,
677  'limit' => $limit,
678  'offset' => $offset,
679  'relationship' => 'friend',
680  'relationship_guid' => $this->getGUID(),
681  'relationship_join_on' => 'container_guid',
682  ));
683  }
684  }
685 
694  public function countObjects($subtype = "") {
695  elgg_deprecated_notice("\ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
696  return count_user_objects($this->getGUID(), $subtype);
697  }
698 
708  public function getCollections($subtype = "", $limit = 10, $offset = 0) {
709  elgg_deprecated_notice("\ElggUser::getCollections() has been deprecated", 1.8);
710  return false;
711  }
712 
720  public function getOwnerGUID() {
721  if ($this->owner_guid == 0) {
722  return $this->guid;
723  }
724 
725  return $this->owner_guid;
726  }
727 
734  public function getOwner() {
735  elgg_deprecated_notice("\ElggUser::getOwner deprecated for \ElggUser::getOwnerGUID", 1.8);
736  $this->getOwnerGUID();
737  }
738 
742  protected function prepareObject($object) {
743  $object = parent::prepareObject($object);
744  $object->name = $this->getDisplayName();
745  $object->username = $this->username;
746  $object->language = $this->language;
747  unset($object->read_access);
748  return $object;
749  }
750 
751  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
752 
759  public function getExportableValues() {
760  return array_merge(parent::getExportableValues(), array(
761  'name',
762  'username',
763  'language',
764  ));
765  }
766 
776  public function canComment($user_guid = 0) {
777  $result = parent::canComment($user_guid);
778  if ($result !== null) {
779  return $result;
780  }
781  return false;
782  }
783 
794  public function setPassword($password) {
795  $this->attributes['salt'] = "";
796  $this->attributes['password'] = "";
797  $this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password);
798  }
799 }
load($guid)
Load the data from the database.
Definition: ElggUser.php:106
addFriend($friend_guid, $create_river_item=false)
Adds a user as a friend.
Definition: ElggUser.php:397
listFriends($subtype="", $limit=10, array $vars=array())
Lists the user&#39;s friends.
Definition: ElggUser.php:546
update()
{}
Definition: ElggUser.php:155
remove_site_user($site_guid, $user_guid)
Remove a user from a site.
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:67
make_user_admin($user_guid)
Makes user $guid an admin.
Definition: users.php:63
$username
Definition: delete.php:22
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:310
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
addToSite($site)
Add this user to a particular site.
Definition: ElggUser.php:364
setPassword($password)
Set the necessary attributes to store a hash of the user&#39;s password.
Definition: ElggUser.php:794
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
$object
Definition: upgrade.php:12
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
$value
Definition: longtext.php:29
getDisplayName()
{}
Definition: ElggUser.php:199
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
getGUID()
Returns the guid.
$guid
Removes an admin notice.
getGroups($options="", $limit=10, $offset=0)
Gets the user&#39;s groups.
Definition: ElggUser.php:574
$collection
add_site_user($site_guid, $user_guid)
Add a user to a site.
$email
Definition: register.php:15
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggUser.php:759
getFriends($options=array(), $limit=10, $offset=0)
Gets this user&#39;s friends.
Definition: ElggUser.php:484
count_user_objects($user_guid, $subtype=ELGG_ENTITIES_ANY_VALUE, $timelower=0, $timeupper=0)
Counts the objects (optionally of a particular subtype) owned by a user.
unban_user($user_guid)
Unban a user.
Definition: users.php:52
__construct($row=null)
Construct a new user entity.
Definition: ElggUser.php:58
initializeAttributes()
Initialize the attributes array.
Definition: ElggUser.php:29
remove_user_from_access_collection($user_guid, $collection_id)
Removes a user from an access collection.
Definition: access.php:388
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
get_user_by_username($username)
Get user by username.
Definition: users.php:98
$options
Definition: index.php:14
ban_user($user_guid, $reason="")
Ban a user.
Definition: users.php:41
$owner_guid
get_user_access_collections($owner_guid, $site_guid=0)
Returns an array of database row objects of the access collections owned by $owner_guid.
Definition: access.php:402
$limit
Definition: userpicker.php:33
ban($reason="")
Ban this user.
Definition: ElggUser.php:269
$filehandler owner_guid
Definition: crop.php:21
unban()
Unban this user.
Definition: ElggUser.php:278
elgg_create_river_item(array $options=array())
Adds an item to the river.
Definition: river.php:37
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
$key
Definition: summary.php:34
elgg menu widget elgg menu item delete
Definition: admin.php:1098
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:447
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
$user
Definition: ban.php:13
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:490
isFriendOf($user_guid)
Determines whether or not this user is another user&#39;s friend.
Definition: ElggUser.php:469
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1055
getOwner()
If a user&#39;s owner is blank, return its own GUID as the owner.
Definition: ElggUser.php:734
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$friend_guid
Definition: add.php:10
getObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user.
Definition: ElggUser.php:636
getOwnerGUID()
Get a user&#39;s owner GUID.
Definition: ElggUser.php:720
$password
Definition: login.php:25
canComment($user_guid=0)
Can a user comment on this user?
Definition: ElggUser.php:776
getFriendsOf($options=array(), $limit=10, $offset=0)
Gets users who have made this user a friend.
Definition: ElggUser.php:515
$attrs
Definition: ajax_loader.php:30
elgg_list_entities_from_relationship(array $options=array())
Returns a viewable list of entities by relationship.
listGroups($subtype="", $limit=10, $offset=0)
Lists the user&#39;s groups.
Definition: ElggUser.php:608
$site name
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:425
getFriendsObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user&#39;s friends.
Definition: ElggUser.php:665
isAdmin()
Is this user admin?
Definition: ElggUser.php:296
removeFromSite($site)
Remove this user from a particular site.
Definition: ElggUser.php:380
$CONFIG language
The current language for either the site or the user.
Definition: config.php:108
getSites($options="", $limit=10, $offset=0)
Get sites that this user is a member of.
Definition: ElggUser.php:348
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:327
$row
create()
{}
Definition: ElggUser.php:127
isBanned()
Is this user banned or not?
Definition: ElggUser.php:287
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
countObjects($subtype="")
Counts the number of owned by this user.
Definition: ElggUser.php:694
$user_guid
Avatar remove action.
Definition: remove.php:6
$defaults
Definition: access.php:19
$language
$vars[&#39;language&#39;] $vars[&#39;lc&#39;] if present, client will be sent long expires headers ...
Definition: languages.php:7
$subtype
Definition: river.php:12
getCollections($subtype="", $limit=10, $offset=0)
Get the collections associated with a user.
Definition: ElggUser.php:708
prepareObject($object)
{}
Definition: ElggUser.php:742
__set($name, $value)
{}
Definition: ElggUser.php:213
$site email
get_user_sites($user_guid, $limit=10, $offset=0)
Get the sites this user is part of.
remove_user_admin($user_guid)
Removes user $guid&#39;s admin flag.
Definition: users.php:74
isFriendsWith($user_guid)
Determines whether this user is friends with another user.
Definition: ElggUser.php:458
setDisplayName($displayName)
{}
Definition: ElggUser.php:206
if(file_exists($welcome)) $vars
Definition: upgrade.php:93