Elgg  Version 1.11
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 += self::getExternalAttributes();
34  $this->tables_split = 2;
35  }
36 
45  final public static function getExternalAttributes() {
46  return [
47  'name' => null,
48  'username' => null,
49  'password' => null,
50  'salt' => null,
51  'password_hash' => null,
52  'email' => null,
53  'language' => null,
54  'banned' => "no",
55  'admin' => 'no',
56  'prev_last_action' => null,
57  'last_login' => null,
58  'prev_last_login' => null,
59  ];
60  }
61 
72  public function __construct($row = null) {
73  $this->initializeAttributes();
74 
75  // compatibility for 1.7 api.
76  $this->initialise_attributes(false);
77 
78  if (!empty($row)) {
79  // Is $row is a DB entity row
80  if ($row instanceof \stdClass) {
81  // Load the rest
82  if (!$this->load($row)) {
83  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
84  throw new \IOException($msg);
85  }
86  } else if (is_string($row)) {
87  // $row is a username
88  elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
90  if ($user) {
91  foreach ($user->attributes as $key => $value) {
92  $this->attributes[$key] = $value;
93  }
94  }
95  } else if ($row instanceof \ElggUser) {
96  // $row is an \ElggUser so this is a copy constructor
97  elgg_deprecated_notice('This type of usage of the \ElggUser constructor was deprecated. Please use the clone method.', 1.7);
98  foreach ($row->attributes as $key => $value) {
99  $this->attributes[$key] = $value;
100  }
101  } else if (is_numeric($row)) {
102  // $row is a GUID so load entity
103  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
104  if (!$this->load($row)) {
105  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
106  }
107  } else {
108  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
109  }
110  }
111  }
112 
120  protected function load($guid) {
121  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'user', $this->attributes);
122  $attr_loader->secondary_loader = 'get_user_entity_as_row';
123 
124  $attrs = $attr_loader->getRequiredAttributes($guid);
125  if (!$attrs) {
126  return false;
127  }
128 
129  $this->attributes = $attrs;
130  $this->tables_loaded = 2;
131  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
132  _elgg_cache_entity($this);
133 
134  return true;
135  }
136 
137 
141  protected function create() {
142  global $CONFIG;
143 
144  $guid = parent::create();
145  $name = sanitize_string($this->name);
147  $password = sanitize_string($this->password);
148  $salt = sanitize_string($this->salt);
149  $password_hash = sanitize_string($this->password_hash);
150  $email = sanitize_string($this->email);
152 
153  $query = "INSERT into {$CONFIG->dbprefix}users_entity
154  (guid, name, username, password, salt, password_hash, email, language)
155  values ($guid, '$name', '$username', '$password', '$salt', '$password_hash', '$email', '$language')";
156 
157  $result = $this->getDatabase()->insertData($query);
158  if ($result === false) {
159  // TODO(evan): Throw an exception here?
160  return false;
161  }
162 
163  return $guid;
164  }
165 
169  protected function update() {
170  global $CONFIG;
171 
172  if (!parent::update()) {
173  return false;
174  }
175 
176  $guid = (int)$this->guid;
177  $name = sanitize_string($this->name);
179  $password = sanitize_string($this->password);
180  $salt = sanitize_string($this->salt);
181  $password_hash = sanitize_string($this->password_hash);
182  $email = sanitize_string($this->email);
184 
185  $query = "UPDATE {$CONFIG->dbprefix}users_entity
186  SET name='$name', username='$username', password='$password', salt='$salt',
187  password_hash='$password_hash', email='$email', language='$language'
188  WHERE guid = $guid";
189 
190  return $this->getDatabase()->updateData($query) !== false;
191  }
192 
198  public function delete() {
199  global $USERNAME_TO_GUID_MAP_CACHE;
200 
201  // clear cache
202  if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
203  unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
204  }
205 
206  // Delete entity
207  return parent::delete();
208  }
209 
213  public function getDisplayName() {
214  return $this->name;
215  }
216 
220  public function setDisplayName($displayName) {
221  $this->name = $displayName;
222  }
223 
227  public function __set($name, $value) {
228  if (!array_key_exists($name, $this->attributes)) {
229  parent::__set($name, $value);
230  return;
231  }
232 
233  switch ($name) {
234  case 'prev_last_action':
235  case 'last_login':
236  case 'prev_last_login':
237  if ($value !== null) {
238  $this->attributes[$name] = (int)$value;
239  } else {
240  $this->attributes[$name] = null;
241  }
242  break;
243 
244  case 'salt':
245  case 'password':
246  elgg_deprecated_notice("Setting salt/password directly is deprecated. Use ElggUser::setPassword().", "1.10");
247  $this->attributes[$name] = $value;
248 
249  // this is emptied so that the user is not left with two usable hashes
250  $this->attributes['password_hash'] = '';
251 
252  break;
253 
254  // setting this not supported
255  case 'password_hash':
256  _elgg_services()->logger->error("password_hash is now an attribute of ElggUser and cannot be set.");
257  return;
258  break;
259 
260  default:
261  parent::__set($name, $value);
262  break;
263  }
264  }
265 
269  public function set($name, $value) {
270  elgg_deprecated_notice("Use -> instead of set()", 1.9);
271  $this->__set($name, $value);
272 
273  return true;
274  }
275 
283  public function ban($reason = "") {
284  return ban_user($this->guid, $reason);
285  }
286 
292  public function unban() {
293  return unban_user($this->guid);
294  }
295 
301  public function isBanned() {
302  return $this->banned == 'yes';
303  }
304 
310  public function isAdmin() {
311 
312  // for backward compatibility we need to pull this directly
313  // from the attributes instead of using the magic methods.
314  // this can be removed in 1.9
315  // return $this->admin == 'yes';
316  return $this->attributes['admin'] == 'yes';
317  }
318 
324  public function makeAdmin() {
325  // If already saved, use the standard function.
326  if ($this->guid && !make_user_admin($this->guid)) {
327  return false;
328  }
329 
330  // need to manually set attributes since they've already been loaded.
331  $this->attributes['admin'] = 'yes';
332 
333  return true;
334  }
335 
341  public function removeAdmin() {
342  // If already saved, use the standard function.
343  if ($this->guid && !remove_user_admin($this->guid)) {
344  return false;
345  }
346 
347  // need to manually set attributes since they've already been loaded.
348  $this->attributes['admin'] = 'no';
349 
350  return true;
351  }
352 
362  public function getSites($options = "", $limit = 10, $offset = 0) {
363  if (is_string($options)) {
364  elgg_deprecated_notice('\ElggUser::getSites() takes an options array', 1.9);
365  return get_user_sites($this->getGUID(), $limit, $offset);
366  }
367 
368  return parent::getSites($options);
369  }
370 
378  public function addToSite($site) {
379  if (is_numeric($site)) {
380  elgg_deprecated_notice('\ElggUser::addToSite() takes a site entity', 1.9);
381  return add_site_user($site, $this->getGUID());
382  }
383 
384  return parent::addToSite($site);
385  }
386 
394  public function removeFromSite($site) {
395  if (is_numeric($site)) {
396  elgg_deprecated_notice('\ElggUser::removeFromSite() takes a site entity', 1.9);
397  return remove_site_user($site, $this->guid);
398  }
399 
400  return parent::removeFromSite($site);
401  }
402 
411  public function addFriend($friend_guid, $create_river_item = false) {
412  if (!get_user($friend_guid)) {
413  return false;
414  }
415 
416  if (!add_entity_relationship($this->guid, "friend", $friend_guid)) {
417  return false;
418  }
419 
420  if ($create_river_item) {
422  'view' => 'river/relationship/friend/create',
423  'action_type' => 'friend',
424  'subject_guid' => $this->guid,
425  'object_guid' => $friend_guid,
426  ));
427  }
428 
429  return true;
430  }
431 
439  public function removeFriend($friend_guid) {
440  if (!get_user($friend_guid)) {
441  return false;
442  }
443 
444  // @todo this should be done with a plugin hook handler on the delete relationship
445  // perform cleanup for access lists.
446  $collections = get_user_access_collections($this->guid);
447  if ($collections) {
448  foreach ($collections as $collection) {
450  }
451  }
452 
453  return remove_entity_relationship($this->guid, "friend", $friend_guid);
454  }
455 
461  public function isFriend() {
462  return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
463  }
464 
472  public function isFriendsWith($user_guid) {
473  return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
474  }
475 
483  public function isFriendOf($user_guid) {
484  return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
485  }
486 
498  public function getFriends($options = array(), $limit = 10, $offset = 0) {
499  if (is_array($options)) {
500  $options['relationship'] = 'friend';
501  $options['relationship_guid'] = $this->getGUID();
502  $options['type'] = 'user';
504  } else {
505  elgg_deprecated_notice("\ElggUser::getFriends takes an options array", 1.9);
507  'relationship' => 'friend',
508  'relationship_guid' => $this->guid,
509  'type' => 'user',
510  'subtype' => $options,
511  'limit' => $limit,
512  'offset' => $offset,
513  ));
514  }
515  }
516 
529  public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
530  if (is_array($options)) {
531  $options['relationship'] = 'friend';
532  $options['relationship_guid'] = $this->getGUID();
533  $options['inverse_relationship'] = true;
534  $options['type'] = 'user';
536  } else {
537  elgg_deprecated_notice("\ElggUser::getFriendsOf takes an options array", 1.9);
539  'relationship' => 'friend',
540  'relationship_guid' => $this->guid,
541  'type' => 'user',
542  'subtype' => $options,
543  'limit' => $limit,
544  'offset' => $offset,
545  ));
546  }
547  }
548 
560  public function listFriends($subtype = "", $limit = 10, array $vars = array()) {
561  elgg_deprecated_notice('\ElggUser::listFriends() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
562  $defaults = array(
563  'type' => 'user',
564  'relationship' => 'friend',
565  'relationship_guid' => $this->guid,
566  'limit' => $limit,
567  'full_view' => false,
568  );
569 
570  $options = array_merge($defaults, $vars);
571 
572  if ($subtype) {
573  $options['subtype'] = $subtype;
574  }
575 
577  }
578 
588  public function getGroups($options = "", $limit = 10, $offset = 0) {
589  if (is_string($options)) {
590  elgg_deprecated_notice('\ElggUser::getGroups() takes an options array', 1.9);
591  $subtype = $options;
592  $options = array(
593  'type' => 'group',
594  'relationship' => 'member',
595  'relationship_guid' => $this->guid,
596  'limit' => $limit,
597  'offset' => $offset,
598  );
599 
600  if ($subtype) {
601  $options['subtype'] = $subtype;
602  }
603  } else {
604  $options['type'] = 'group';
605  $options['relationship'] = 'member';
606  $options['relationship_guid'] = $this->guid;
607  }
608 
610  }
611 
622  public function listGroups($subtype = "", $limit = 10, $offset = 0) {
623  elgg_deprecated_notice('Elgg::listGroups is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
624  $options = array(
625  'type' => 'group',
626  'relationship' => 'member',
627  'relationship_guid' => $this->guid,
628  'limit' => $limit,
629  'offset' => $offset,
630  'full_view' => false,
631  );
632 
633  if ($subtype) {
634  $options['subtype'] = $subtype;
635  }
636 
638  }
639 
650  public function getObjects($options = array(), $limit = 10, $offset = 0) {
651  if (is_array($options)) {
652  $options['type'] = 'object';
653  $options['owner_guid'] = $this->getGUID();
654  return elgg_get_entities($options);
655  } else {
656  elgg_deprecated_notice("\ElggUser::getObjects takes an options array", 1.9);
657  return elgg_get_entities(array(
658  'type' => 'object',
659  'subtype' => $options,
660  'owner_guid' => $this->getGUID(),
661  'limit' => $limit,
662  'offset' => $offset
663  ));
664  }
665  }
666 
679  public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
680  if (is_array($options)) {
681  $options['type'] = 'object';
682  $options['relationship'] = 'friend';
683  $options['relationship_guid'] = $this->getGUID();
684  $options['relationship_join_on'] = 'container_guid';
686  } else {
687  elgg_deprecated_notice("\ElggUser::getFriendsObjects takes an options array", 1.9);
689  'type' => 'object',
690  'subtype' => $options,
691  'limit' => $limit,
692  'offset' => $offset,
693  'relationship' => 'friend',
694  'relationship_guid' => $this->getGUID(),
695  'relationship_join_on' => 'container_guid',
696  ));
697  }
698  }
699 
708  public function countObjects($subtype = "") {
709  elgg_deprecated_notice("\ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
710  return count_user_objects($this->getGUID(), $subtype);
711  }
712 
722  public function getCollections($subtype = "", $limit = 10, $offset = 0) {
723  elgg_deprecated_notice("\ElggUser::getCollections() has been deprecated", 1.8);
724  return false;
725  }
726 
734  public function getOwnerGUID() {
735  if ($this->owner_guid == 0) {
736  return $this->guid;
737  }
738 
739  return $this->owner_guid;
740  }
741 
748  public function getOwner() {
749  elgg_deprecated_notice("\ElggUser::getOwner deprecated for \ElggUser::getOwnerGUID", 1.8);
750  $this->getOwnerGUID();
751  }
752 
756  protected function prepareObject($object) {
757  $object = parent::prepareObject($object);
758  $object->name = $this->getDisplayName();
759  $object->username = $this->username;
760  $object->language = $this->language;
761  unset($object->read_access);
762  return $object;
763  }
764 
765  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
766 
773  public function getExportableValues() {
774  return array_merge(parent::getExportableValues(), array(
775  'name',
776  'username',
777  'language',
778  ));
779  }
780 
790  public function canComment($user_guid = 0) {
791  $result = parent::canComment($user_guid);
792  if ($result !== null) {
793  return $result;
794  }
795  return false;
796  }
797 
808  public function setPassword($password) {
809  $this->attributes['salt'] = "";
810  $this->attributes['password'] = "";
811  $this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password);
812  }
813 }
load($guid)
Load the data from the database.
Definition: ElggUser.php:120
addFriend($friend_guid, $create_river_item=false)
Adds a user as a friend.
Definition: ElggUser.php:411
listFriends($subtype="", $limit=10, array $vars=array())
Lists the user&#39;s friends.
Definition: ElggUser.php:560
update()
{}
Definition: ElggUser.php:169
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
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggUser.php:45
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:324
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:378
setPassword($password)
Set the necessary attributes to store a hash of the user&#39;s password.
Definition: ElggUser.php:808
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
$defaults
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
$value
Definition: longtext.php:26
getDisplayName()
{}
Definition: ElggUser.php:213
if(!$count) $offset
Definition: pagination.php:25
getGUID()
Returns the guid.
$guid
Removes an admin notice.
getGroups($options="", $limit=10, $offset=0)
Gets the user&#39;s groups.
Definition: ElggUser.php:588
$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:773
getFriends($options=array(), $limit=10, $offset=0)
Gets this user&#39;s friends.
Definition: ElggUser.php:498
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:72
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:391
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
username
Definition: contents.php:36
$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:405
$limit
Definition: userpicker.php:31
ban($reason="")
Ban this user.
Definition: ElggUser.php:283
$filehandler owner_guid
Definition: crop.php:21
unban()
Unban this user.
Definition: ElggUser.php:292
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:1101
_elgg_services()
Definition: autoloader.php:14
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:461
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:483
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)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
getOwner()
If a user&#39;s owner is blank, return its own GUID as the owner.
Definition: ElggUser.php:748
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:650
getOwnerGUID()
Get a user&#39;s owner GUID.
Definition: ElggUser.php:734
$password
Definition: login.php:25
canComment($user_guid=0)
Can a user comment on this user?
Definition: ElggUser.php:790
getFriendsOf($options=array(), $limit=10, $offset=0)
Gets users who have made this user a friend.
Definition: ElggUser.php:529
$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:622
$site name
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:439
getFriendsObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user&#39;s friends.
Definition: ElggUser.php:679
isAdmin()
Is this user admin?
Definition: ElggUser.php:310
removeFromSite($site)
Remove this user from a particular site.
Definition: ElggUser.php:394
$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:362
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:341
$row
create()
{}
Definition: ElggUser.php:141
isBanned()
Is this user banned or not?
Definition: ElggUser.php:301
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:708
$user_guid
Avatar remove action.
Definition: remove.php:6
$language
$vars[&#39;language&#39;]
Definition: languages.php:6
$subtype
Definition: river.php:12
getCollections($subtype="", $limit=10, $offset=0)
Get the collections associated with a user.
Definition: ElggUser.php:722
prepareObject($object)
{}
Definition: ElggUser.php:756
__set($name, $value)
{}
Definition: ElggUser.php:227
$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:472
setDisplayName($displayName)
{}
Definition: ElggUser.php:220
if(file_exists($welcome)) $vars
Definition: upgrade.php:93