Elgg  Version 1.9
ElggUser.php
Go to the documentation of this file.
1 <?php
19 class ElggUser extends ElggEntity
20  implements Friendable {
21 
28  protected function initializeAttributes() {
29  parent::initializeAttributes();
30 
31  $this->attributes['type'] = "user";
32  $this->attributes['name'] = null;
33  $this->attributes['username'] = null;
34  $this->attributes['password'] = null;
35  $this->attributes['salt'] = null;
36  $this->attributes['email'] = null;
37  $this->attributes['language'] = null;
38  $this->attributes['banned'] = "no";
39  $this->attributes['admin'] = 'no';
40  $this->attributes['prev_last_action'] = null;
41  $this->attributes['last_login'] = null;
42  $this->attributes['prev_last_login'] = null;
43  $this->tables_split = 2;
44  }
45 
56  public function __construct($row = null) {
57  $this->initializeAttributes();
58 
59  // compatibility for 1.7 api.
60  $this->initialise_attributes(false);
61 
62  if (!empty($row)) {
63  // Is $row is a DB entity row
64  if ($row instanceof stdClass) {
65  // Load the rest
66  if (!$this->load($row)) {
67  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
68  throw new IOException($msg);
69  }
70  } else if (is_string($row)) {
71  // $row is a username
72  elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
74  if ($user) {
75  foreach ($user->attributes as $key => $value) {
76  $this->attributes[$key] = $value;
77  }
78  }
79  } else if ($row instanceof ElggUser) {
80  // $row is an ElggUser so this is a copy constructor
81  elgg_deprecated_notice('This type of usage of the ElggUser constructor was deprecated. Please use the clone method.', 1.7);
82  foreach ($row->attributes as $key => $value) {
83  $this->attributes[$key] = $value;
84  }
85  } else if (is_numeric($row)) {
86  // $row is a GUID so load entity
87  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
88  if (!$this->load($row)) {
89  throw new IOException("Failed to load new " . get_class() . " from GUID:" . $row);
90  }
91  } else {
92  throw new InvalidParameterException("Unrecognized value passed to constuctor.");
93  }
94  }
95  }
96 
104  protected function load($guid) {
105  $attr_loader = new Elgg_AttributeLoader(get_class(), 'user', $this->attributes);
106  $attr_loader->secondary_loader = 'get_user_entity_as_row';
107 
108  $attrs = $attr_loader->getRequiredAttributes($guid);
109  if (!$attrs) {
110  return false;
111  }
112 
113  $this->attributes = $attrs;
114  $this->tables_loaded = 2;
115  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
116  _elgg_cache_entity($this);
117 
118  return true;
119  }
120 
121 
125  protected function create() {
126  global $CONFIG;
127 
128  $guid = parent::create();
129  $name = sanitize_string($this->name);
130  $username = sanitize_string($this->username);
131  $password = sanitize_string($this->password);
132  $salt = sanitize_string($this->salt);
133  $email = sanitize_string($this->email);
135 
136  $query = "INSERT into {$CONFIG->dbprefix}users_entity
137  (guid, name, username, password, salt, email, language)
138  values ($guid, '$name', '$username', '$password', '$salt', '$email', '$language')";
139 
140  $result = $this->getDatabase()->insertData($query);
141  if ($result === false) {
142  // TODO(evan): Throw an exception here?
143  return false;
144  }
145 
146  return $guid;
147  }
148 
152  protected function update() {
153  global $CONFIG;
154 
155  if (!parent::update()) {
156  return false;
157  }
158 
159  $guid = (int)$this->guid;
160  $name = sanitize_string($this->name);
161  $username = sanitize_string($this->username);
162  $password = sanitize_string($this->password);
163  $salt = sanitize_string($this->salt);
164  $email = sanitize_string($this->email);
166 
167  $query = "UPDATE {$CONFIG->dbprefix}users_entity
168  SET name='$name', username='$username', password='$password', salt='$salt',
169  email='$email', language='$language'
170  WHERE guid = $guid";
171 
172  return $this->getDatabase()->updateData($query) !== false;
173  }
174 
180  public function delete() {
182 
183  // clear cache
184  if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
185  unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
186  }
187 
188  clear_user_files($this);
189 
190  // Delete entity
191  return parent::delete();
192  }
193 
197  public function getDisplayName() {
198  return $this->name;
199  }
200 
204  public function setDisplayName($displayName) {
205  $this->name = $displayName;
206  }
207 
211  public function __set($name, $value) {
212  if (array_key_exists($name, $this->attributes)) {
213  switch ($name) {
214  case 'prev_last_action':
215  case 'last_login':
216  case 'prev_last_login':
217  if ($value !== null) {
218  $this->attributes[$name] = (int)$value;
219  } else {
220  $this->attributes[$name] = null;
221  }
222  break;
223  default:
224  parent::__set($name, $value);
225  break;
226  }
227  } else {
228  parent::__set($name, $value);
229  }
230  }
231 
235  public function set($name, $value) {
236  elgg_deprecated_notice("Use -> instead of set()", 1.9);
237  $this->__set($name, $value);
238 
239  return true;
240  }
241 
249  public function ban($reason = "") {
250  return ban_user($this->guid, $reason);
251  }
252 
258  public function unban() {
259  return unban_user($this->guid);
260  }
261 
267  public function isBanned() {
268  return $this->banned == 'yes';
269  }
270 
276  public function isAdmin() {
277 
278  // for backward compatibility we need to pull this directly
279  // from the attributes instead of using the magic methods.
280  // this can be removed in 1.9
281  // return $this->admin == 'yes';
282  return $this->attributes['admin'] == 'yes';
283  }
284 
290  public function makeAdmin() {
291  // If already saved, use the standard function.
292  if ($this->guid && !make_user_admin($this->guid)) {
293  return false;
294  }
295 
296  // need to manually set attributes since they've already been loaded.
297  $this->attributes['admin'] = 'yes';
298 
299  return true;
300  }
301 
307  public function removeAdmin() {
308  // If already saved, use the standard function.
309  if ($this->guid && !remove_user_admin($this->guid)) {
310  return false;
311  }
312 
313  // need to manually set attributes since they've already been loaded.
314  $this->attributes['admin'] = 'no';
315 
316  return true;
317  }
318 
328  public function getSites($options = "", $limit = 10, $offset = 0) {
329  if (is_string($options)) {
330  elgg_deprecated_notice('ElggUser::getSites() takes an options array', 1.9);
331  return get_user_sites($this->getGUID(), $limit, $offset);
332  }
333 
334  return parent::getSites($options);
335  }
336 
344  public function addToSite($site) {
345  if (is_numeric($site)) {
346  elgg_deprecated_notice('ElggUser::addToSite() takes a site entity', 1.9);
347  return add_site_user($site, $this->getGUID());
348  }
349 
350  return parent::addToSite($site);
351  }
352 
360  public function removeFromSite($site) {
361  if (is_numeric($site)) {
362  elgg_deprecated_notice('ElggUser::removeFromSite() takes a site entity', 1.9);
363  return remove_site_user($site, $this->guid);
364  }
365 
366  return parent::removeFromSite($site);
367  }
368 
377  public function addFriend($friend_guid) {
378  if (!get_user($friend_guid)) {
379  return false;
380  }
381 
382  return add_entity_relationship($this->guid, "friend", $friend_guid);
383  }
384 
393  public function removeFriend($friend_guid) {
394  if (!get_user($friend_guid)) {
395  return false;
396  }
397 
398  // @todo this should be done with a plugin hook handler on the delete relationship
399  // perform cleanup for access lists.
400  $collections = get_user_access_collections($this->guid);
401  if ($collections) {
402  foreach ($collections as $collection) {
404  }
405  }
406 
407  return remove_entity_relationship($this->guid, "friend", $friend_guid);
408  }
409 
415  public function isFriend() {
416  return $this->isFriendOf(elgg_get_logged_in_user_guid());
417  }
418 
426  public function isFriendsWith($user_guid) {
427  return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
428  }
429 
437  public function isFriendOf($user_guid) {
438  return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
439  }
440 
452  public function getFriends($options = array(), $limit = 10, $offset = 0) {
453  if (is_array($options)) {
454  $options['relationship'] = 'friend';
455  $options['relationship_guid'] = $this->getGUID();
456  $options['type'] = 'user';
458  } else {
459  elgg_deprecated_notice("ElggUser::getFriends takes an options array", 1.9);
461  'relationship' => 'friend',
462  'relationship_guid' => $this->guid,
463  'type' => 'user',
464  'subtype' => $options,
465  'limit' => $limit,
466  'offset' => $offset,
467  ));
468  }
469  }
470 
483  public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
484  if (is_array($options)) {
485  $options['relationship'] = 'friend';
486  $options['relationship_guid'] = $this->getGUID();
487  $options['inverse_relationship'] = true;
488  $options['type'] = 'user';
490  } else {
491  elgg_deprecated_notice("ElggUser::getFriendsOf 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 
514  public function listFriends($subtype = "", $limit = 10, array $vars = array()) {
515  elgg_deprecated_notice('ElggUser::listFriends() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
516  $defaults = array(
517  'type' => 'user',
518  'relationship' => 'friend',
519  'relationship_guid' => $this->guid,
520  'limit' => $limit,
521  'full_view' => false,
522  );
523 
524  $options = array_merge($defaults, $vars);
525 
526  if ($subtype) {
527  $options['subtype'] = $subtype;
528  }
529 
531  }
532 
542  public function getGroups($options = "", $limit = 10, $offset = 0) {
543  if (is_string($options)) {
544  elgg_deprecated_notice('ElggUser::getGroups() takes an options array', 1.9);
545  $subtype = $options;
546  $options = array(
547  'type' => 'group',
548  'relationship' => 'member',
549  'relationship_guid' => $this->guid,
550  'limit' => $limit,
551  'offset' => $offset,
552  );
553 
554  if ($subtype) {
555  $options['subtype'] = $subtype;
556  }
557  } else {
558  $options['type'] = 'group';
559  $options['relationship'] = 'member';
560  $options['relationship_guid'] = $this->guid;
561  }
562 
564  }
565 
576  public function listGroups($subtype = "", $limit = 10, $offset = 0) {
577  elgg_deprecated_notice('Elgg::listGroups is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
578  $options = array(
579  'type' => 'group',
580  'relationship' => 'member',
581  'relationship_guid' => $this->guid,
582  'limit' => $limit,
583  'offset' => $offset,
584  'full_view' => false,
585  );
586 
587  if ($subtype) {
588  $options['subtype'] = $subtype;
589  }
590 
592  }
593 
604  public function getObjects($options = array(), $limit = 10, $offset = 0) {
605  if (is_array($options)) {
606  $options['type'] = 'object';
607  $options['owner_guid'] = $this->getGUID();
608  return elgg_get_entities($options);
609  } else {
610  elgg_deprecated_notice("ElggUser::getObjects takes an options array", 1.9);
611  return elgg_get_entities(array(
612  'type' => 'object',
613  'subtype' => $options,
614  'owner_guid' => $this->getGUID(),
615  'limit' => $limit,
616  'offset' => $offset
617  ));
618  }
619  }
620 
633  public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
634  if (is_array($options)) {
635  $options['type'] = 'object';
636  $options['relationship'] = 'friend';
637  $options['relationship_guid'] = $this->getGUID();
638  $options['relationship_join_on'] = 'container_guid';
640  } else {
641  elgg_deprecated_notice("ElggUser::getFriendsObjects takes an options array", 1.9);
643  'type' => 'object',
644  'subtype' => $options,
645  'limit' => $limit,
646  'offset' => $offset,
647  'relationship' => 'friend',
648  'relationship_guid' => $this->getGUID(),
649  'relationship_join_on' => 'container_guid',
650  ));
651  }
652  }
653 
662  public function countObjects($subtype = "") {
663  elgg_deprecated_notice("ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
664  return count_user_objects($this->getGUID(), $subtype);
665  }
666 
676  public function getCollections($subtype = "", $limit = 10, $offset = 0) {
677  elgg_deprecated_notice("ElggUser::getCollections() has been deprecated", 1.8);
678  return false;
679  }
680 
688  public function getOwnerGUID() {
689  if ($this->owner_guid == 0) {
690  return $this->guid;
691  }
692 
693  return $this->owner_guid;
694  }
695 
702  public function getOwner() {
703  elgg_deprecated_notice("ElggUser::getOwner deprecated for ElggUser::getOwnerGUID", 1.8);
704  $this->getOwnerGUID();
705  }
706 
710  protected function prepareObject($object) {
711  $object = parent::prepareObject($object);
712  $object->name = $this->getDisplayName();
713  $object->username = $this->username;
714  $object->language = $this->language;
715  unset($object->read_access);
716  return $object;
717  }
718 
719  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
720 
727  public function getExportableValues() {
728  return array_merge(parent::getExportableValues(), array(
729  'name',
730  'username',
731  'language',
732  ));
733  }
734 
744  public function canComment($user_guid = 0) {
745  $result = parent::canComment($user_guid);
746  if ($result !== null) {
747  return $result;
748  }
749  return false;
750  }
751 }
load($guid)
Load the ElggUser data from the database.
Definition: ElggUser.php:104
listFriends($subtype="", $limit=10, array $vars=array())
Lists the user&#39;s friends.
Definition: ElggUser.php:514
update()
{}
Definition: ElggUser.php:152
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:148
$username
Definition: delete.php:22
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:290
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:344
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
clear_user_files($user)
Removes all user files.
Definition: filestore.php:448
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
$value
Definition: longtext.php:29
getDisplayName()
{}
Definition: ElggUser.php:197
getGUID()
Returns the guid.
$guid
Removes an admin notice.
getGroups($options="", $limit=10, $offset=0)
Gets the user&#39;s groups.
Definition: ElggUser.php:542
$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:727
getFriends($options=array(), $limit=10, $offset=0)
Gets this user&#39;s friends.
Definition: ElggUser.php:452
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:109
__construct($row=null)
Construct a new user entity.
Definition: ElggUser.php:56
initializeAttributes()
Initialize the attributes array.
Definition: ElggUser.php:28
remove_user_from_access_collection($user_guid, $collection_id)
Removes a user from an access collection.
Definition: access.php:784
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:246
$options
Definition: index.php:14
ban_user($user_guid, $reason="")
Ban a user.
Definition: users.php:67
$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:823
$limit
Definition: userpicker.php:33
ban($reason="")
Ban this user.
Definition: ElggUser.php:249
$filehandler owner_guid
Definition: crop.php:21
unban()
Unban this user.
Definition: ElggUser.php:258
get_user($guid)
Get a user object from a GUID.
Definition: users.php:222
$key
Definition: summary.php:34
elgg menu widget elgg menu item delete
Definition: admin.php:1075
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:415
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
$user
Definition: ban.php:13
addFriend($friend_guid)
Adds a user as a friend.
Definition: ElggUser.php:377
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:777
isFriendOf($user_guid)
Determines whether or not this user is another user&#39;s friend.
Definition: ElggUser.php:437
global $USERNAME_TO_GUID_MAP_CACHE
Definition: users.php:15
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:1171
getOwner()
If a user&#39;s owner is blank, return its own GUID as the owner.
Definition: ElggUser.php:702
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 ElggObject owned by this user.
Definition: ElggUser.php:604
getOwnerGUID()
Get a user&#39;s owner GUID.
Definition: ElggUser.php:688
$password
Definition: login.php:25
canComment($user_guid=0)
Can a user comment on this user?
Definition: ElggUser.php:744
getFriendsOf($options=array(), $limit=10, $offset=0)
Gets users who have made this user a friend.
Definition: ElggUser.php:483
$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:576
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:393
getFriendsObjects($options=array(), $limit=10, $offset=0)
Get an array of ElggObjects owned by this user&#39;s friends.
Definition: ElggUser.php:633
isAdmin()
Is this user admin?
Definition: ElggUser.php:276
removeFromSite($site)
Remove this user from a particular site.
Definition: ElggUser.php:360
$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:328
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:307
$row
create()
{}
Definition: ElggUser.php:125
isBanned()
Is this user banned or not?
Definition: ElggUser.php:267
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
countObjects($subtype="")
Counts the number of ElggObjects owned by this user.
Definition: ElggUser.php:662
$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:10
getCollections($subtype="", $limit=10, $offset=0)
Get the collections associated with a user.
Definition: ElggUser.php:676
prepareObject($object)
{}
Definition: ElggUser.php:710
__set($name, $value)
{}
Definition: ElggUser.php:211
_elgg_cache_entity(ElggEntity $entity)
Cache an entity.
Definition: entities.php:101
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
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:184
isFriendsWith($user_guid)
Determines whether this user is friends with another user.
Definition: ElggUser.php:426
setDisplayName($displayName)
{}
Definition: ElggUser.php:204
if(file_exists($welcome)) $vars
Definition: upgrade.php:93