Elgg  Version 2.3
ElggUser.php
Go to the documentation of this file.
1 <?php
23 class ElggUser extends \ElggEntity
24  implements Friendable {
25 
32  protected function initializeAttributes() {
33  parent::initializeAttributes();
34 
35  $this->attributes['type'] = "user";
36  $this->attributes += self::getExternalAttributes();
37  }
38 
47  final public static function getExternalAttributes() {
48  return [
49  'name' => null,
50  'username' => null,
51  'password' => null,
52  'salt' => null,
53  'password_hash' => null,
54  'email' => null,
55  'language' => null,
56  'banned' => "no",
57  'admin' => 'no',
58  'prev_last_action' => null,
59  'last_login' => null,
60  'prev_last_login' => null,
61  ];
62  }
63 
74  public function __construct($row = null) {
75  $this->initializeAttributes();
76 
77  if (!empty($row)) {
78  // Is $row is a DB entity row
79  if ($row instanceof \stdClass) {
80  // Load the rest
81  if (!$this->load($row)) {
82  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
83  throw new \IOException($msg);
84  }
85  } else if (is_string($row)) {
86  // $row is a username
87  elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
89  if ($user) {
90  foreach ($user->attributes as $key => $value) {
91  $this->attributes[$key] = $value;
92  }
93  }
94  } else if (is_numeric($row)) {
95  // $row is a GUID so load entity
96  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
97  if (!$this->load($row)) {
98  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
99  }
100  } else {
101  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
102  }
103  }
104  }
105 
113  protected function load($guid) {
114  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'user', $this->attributes);
115  $attr_loader->secondary_loader = 'get_user_entity_as_row';
116 
117  $attrs = $attr_loader->getRequiredAttributes($guid);
118  if (!$attrs) {
119  return false;
120  }
121 
122  $this->attributes = $attrs;
123  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
124  _elgg_services()->entityCache->set($this);
125 
126  return true;
127  }
128 
129 
133  protected function create() {
134  global $CONFIG;
135 
136  $guid = parent::create();
137  $name = sanitize_string($this->name);
139  $password = sanitize_string($this->password);
140  $salt = sanitize_string($this->salt);
141  $password_hash = sanitize_string($this->password_hash);
142  $email = sanitize_string($this->email);
144 
145  $query = "INSERT into {$CONFIG->dbprefix}users_entity
146  (guid, name, username, password, salt, password_hash, email, language)
147  values ($guid, '$name', '$username', '$password', '$salt', '$password_hash', '$email', '$language')";
148 
149  $result = $this->getDatabase()->insertData($query);
150  if ($result === false) {
151  // TODO(evan): Throw an exception here?
152  return false;
153  }
154 
155  return $guid;
156  }
157 
161  protected function update() {
162  global $CONFIG;
163 
164  if (!parent::update()) {
165  return false;
166  }
167 
168  $guid = (int)$this->guid;
169  $name = sanitize_string($this->name);
171  $password = sanitize_string($this->password);
172  $salt = sanitize_string($this->salt);
173  $password_hash = sanitize_string($this->password_hash);
174  $email = sanitize_string($this->email);
176 
177  $query = "UPDATE {$CONFIG->dbprefix}users_entity
178  SET name='$name', username='$username', password='$password', salt='$salt',
179  password_hash='$password_hash', email='$email', language='$language'
180  WHERE guid = $guid";
181 
182  return $this->getDatabase()->updateData($query) !== false;
183  }
184 
188  public function getDisplayName() {
189  return $this->name;
190  }
191 
195  public function setDisplayName($displayName) {
196  $this->name = $displayName;
197  }
198 
202  public function __set($name, $value) {
203  if (!array_key_exists($name, $this->attributes)) {
204  parent::__set($name, $value);
205  return;
206  }
207 
208  switch ($name) {
209  case 'prev_last_action':
210  case 'last_login':
211  case 'prev_last_login':
212  if ($value !== null) {
213  $this->attributes[$name] = (int)$value;
214  } else {
215  $this->attributes[$name] = null;
216  }
217  break;
218 
219  case 'salt':
220  case 'password':
221  elgg_deprecated_notice("Setting salt/password directly is deprecated. Use ElggUser::setPassword().", "1.10");
222  $this->attributes[$name] = $value;
223 
224  // this is emptied so that the user is not left with two usable hashes
225  $this->attributes['password_hash'] = '';
226 
227  break;
228 
229  // setting this not supported
230  case 'password_hash':
231  _elgg_services()->logger->error("password_hash is now an attribute of ElggUser and cannot be set.");
232  break;
233 
234  default:
235  parent::__set($name, $value);
236  break;
237  }
238  }
239 
243  public function set($name, $value) {
244  elgg_deprecated_notice("Use -> instead of set()", 1.9);
245  $this->__set($name, $value);
246 
247  return true;
248  }
249 
257  public function ban($reason = "") {
258  return ban_user($this->guid, $reason);
259  }
260 
266  public function unban() {
267  return unban_user($this->guid);
268  }
269 
275  public function isBanned() {
276  return $this->banned == 'yes';
277  }
278 
284  public function isAdmin() {
285 
286  // for backward compatibility we need to pull this directly
287  // from the attributes instead of using the magic methods.
288  // this can be removed in 1.9
289  // return $this->admin == 'yes';
290  return $this->attributes['admin'] == 'yes';
291  }
292 
298  public function makeAdmin() {
299 
300  if ($this->isAdmin()) {
301  return true;
302  }
303 
304  // If already saved, use the standard function.
305  if ($this->guid && !make_user_admin($this->guid)) {
306  return false;
307  }
308 
309  // need to manually set attributes since they've already been loaded.
310  $this->attributes['admin'] = 'yes';
311 
312  return true;
313  }
314 
320  public function removeAdmin() {
321 
322  if (!$this->isAdmin()) {
323  return true;
324  }
325 
326  // If already saved, use the standard function.
327  if ($this->guid && !remove_user_admin($this->guid)) {
328  return false;
329  }
330 
331  // need to manually set attributes since they've already been loaded.
332  $this->attributes['admin'] = 'no';
333 
334  return true;
335  }
336 
345  public function addFriend($friend_guid, $create_river_item = false) {
346  if (!get_user($friend_guid)) {
347  return false;
348  }
349 
350  if (!add_entity_relationship($this->guid, "friend", $friend_guid)) {
351  return false;
352  }
353 
354  if ($create_river_item) {
356  'view' => 'river/relationship/friend/create',
357  'action_type' => 'friend',
358  'subject_guid' => $this->guid,
359  'object_guid' => $friend_guid,
360  ));
361  }
362 
363  return true;
364  }
365 
373  public function removeFriend($friend_guid) {
374  if (!get_user($friend_guid)) {
375  return false;
376  }
377 
378  // @todo this should be done with a plugin hook handler on the delete relationship
379  // perform cleanup for access lists.
380  $collections = get_user_access_collections($this->guid);
381  if ($collections) {
382  foreach ($collections as $collection) {
384  }
385  }
386 
387  return remove_entity_relationship($this->guid, "friend", $friend_guid);
388  }
389 
395  public function isFriend() {
396  return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
397  }
398 
406  public function isFriendsWith($user_guid) {
407  return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
408  }
409 
417  public function isFriendOf($user_guid) {
418  return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
419  }
420 
432  public function getFriends($options = array(), $limit = 10, $offset = 0) {
433  if (is_array($options)) {
434  $options['relationship'] = 'friend';
435  $options['relationship_guid'] = $this->getGUID();
436  $options['type'] = 'user';
438  } else {
439  elgg_deprecated_notice("\ElggUser::getFriends takes an options array", 1.9);
441  'relationship' => 'friend',
442  'relationship_guid' => $this->guid,
443  'type' => 'user',
444  'subtype' => $options,
445  'limit' => $limit,
446  'offset' => $offset,
447  ));
448  }
449  }
450 
463  public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
464  if (is_array($options)) {
465  $options['relationship'] = 'friend';
466  $options['relationship_guid'] = $this->getGUID();
467  $options['inverse_relationship'] = true;
468  $options['type'] = 'user';
470  } else {
471  elgg_deprecated_notice("\ElggUser::getFriendsOf takes an options array", 1.9);
473  'relationship' => 'friend',
474  'relationship_guid' => $this->guid,
475  'type' => 'user',
476  'subtype' => $options,
477  'limit' => $limit,
478  'offset' => $offset,
479  ));
480  }
481  }
482 
492  public function getGroups($options = "", $limit = 10, $offset = 0) {
493  if (is_string($options)) {
494  elgg_deprecated_notice('\ElggUser::getGroups() takes an options array', 1.9);
495  $subtype = $options;
496  $options = array(
497  'type' => 'group',
498  'relationship' => 'member',
499  'relationship_guid' => $this->guid,
500  'limit' => $limit,
501  'offset' => $offset,
502  );
503 
504  if ($subtype) {
505  $options['subtype'] = $subtype;
506  }
507  } else {
508  $options['type'] = 'group';
509  $options['relationship'] = 'member';
510  $options['relationship_guid'] = $this->guid;
511  }
512 
514  }
515 
526  public function getObjects($options = array(), $limit = 10, $offset = 0) {
527  if (is_array($options)) {
528  $options['type'] = 'object';
529  $options['owner_guid'] = $this->getGUID();
530  return elgg_get_entities($options);
531  } else {
532  elgg_deprecated_notice("\ElggUser::getObjects takes an options array", 1.9);
533  return elgg_get_entities(array(
534  'type' => 'object',
535  'subtype' => $options,
536  'owner_guid' => $this->getGUID(),
537  'limit' => $limit,
538  'offset' => $offset
539  ));
540  }
541  }
542 
555  public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
556  if (is_array($options)) {
557  $options['type'] = 'object';
558  $options['relationship'] = 'friend';
559  $options['relationship_guid'] = $this->getGUID();
560  $options['relationship_join_on'] = 'container_guid';
562  } else {
563  elgg_deprecated_notice("\ElggUser::getFriendsObjects takes an options array", 1.9);
565  'type' => 'object',
566  'subtype' => $options,
567  'limit' => $limit,
568  'offset' => $offset,
569  'relationship' => 'friend',
570  'relationship_guid' => $this->getGUID(),
571  'relationship_join_on' => 'container_guid',
572  ));
573  }
574  }
575 
584  public function countObjects($subtype = "") {
585  elgg_deprecated_notice("\ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
586  $options = [
587  'count' => true,
588  ];
589  if ($subtype) {
590  $options['subtype'] = $subtype;
591  }
592  return (int)$this->getObjects($options);
593  }
594 
602  public function getOwnerGUID() {
603  if ($this->owner_guid == 0) {
604  return $this->guid;
605  }
606 
607  return $this->owner_guid;
608  }
609 
613  protected function prepareObject($object) {
614  $object = parent::prepareObject($object);
615  $object->name = $this->getDisplayName();
616  $object->username = $this->username;
617  $object->language = $this->language;
618  unset($object->read_access);
619  return $object;
620  }
621 
622  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
623 
630  public function getExportableValues() {
631  return array_merge(parent::getExportableValues(), array(
632  'name',
633  'username',
634  'language',
635  ));
636  }
637 
648  public function canComment($user_guid = 0, $default = null) {
649  $result = parent::canComment($user_guid, $default);
650  if ($result !== null) {
651  return $result;
652  }
653  return false;
654  }
655 
666  public function setPassword($password) {
667  $this->attributes['salt'] = "";
668  $this->attributes['password'] = "";
669  $this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password);
670  }
671 
679  public function setNotificationSetting($method, $enabled = true) {
680  $this->{"notification:method:$method"} = (int) $enabled;
681  return (bool) $this->save();
682  }
683 
695  public function getNotificationSettings() {
696 
697  $settings = [];
698 
699  $methods = _elgg_services()->notifications->getMethods();
700  foreach ($methods as $method) {
701  $settings[$method] = (bool) $this->{"notification:method:$method"};
702  }
703 
704  return $settings;
705 
706  }
707 }
load($guid)
Load the data from the database.
Definition: ElggUser.php:113
addFriend($friend_guid, $create_river_item=false)
Adds a user as a friend.
Definition: ElggUser.php:345
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
update()
{}
Definition: ElggUser.php:161
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:48
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:47
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:298
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.
setPassword($password)
Set the necessary attributes to store a hash of the user&#39;s password.
Definition: ElggUser.php:666
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.
$method
Definition: form.php:25
setNotificationSetting($method, $enabled=true)
Enable or disable a notification delivery method.
Definition: ElggUser.php:679
save()
Save an entity.
$value
Definition: longtext.php:42
getDisplayName()
{}
Definition: ElggUser.php:188
$subtype
Definition: delete.php:28
if(!$count) $offset
Definition: pagination.php:26
getGUID()
Returns the guid.
$default
Definition: checkbox.php:34
$guid
Removes an admin notice.
getGroups($options="", $limit=10, $offset=0)
Gets the user&#39;s groups.
Definition: ElggUser.php:492
$collection
$email
Definition: register.php:15
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggUser.php:630
getFriends($options=array(), $limit=10, $offset=0)
Gets this user&#39;s friends.
Definition: ElggUser.php:432
unban_user($user_guid)
Unban a user.
Definition: users.php:52
__construct($row=null)
Construct a new user entity.
Definition: ElggUser.php:74
initializeAttributes()
Initialize the attributes array.
Definition: ElggUser.php:32
remove_user_from_access_collection($user_guid, $collection_id)
Removes a user from an access collection.
Definition: access.php:389
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
$options
Elgg admin footer.
Definition: footer.php:6
get_user_by_username($username)
Get user by username.
Definition: users.php:98
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:403
$limit
Definition: userpicker.php:38
ban($reason="")
Ban this user.
Definition: ElggUser.php:257
unban()
Unban this user.
Definition: ElggUser.php:266
elgg_create_river_item(array $options=array())
Adds an item to the river.
Definition: river.php:39
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
$key
Definition: summary.php:34
$language
Definition: useradd.php:20
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:395
global $CONFIG
$user
Definition: ban.php:13
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:326
isFriendOf($user_guid)
Determines whether or not this user is another user&#39;s friend.
Definition: ElggUser.php:417
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:1098
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:526
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
getOwnerGUID()
Get a user&#39;s owner GUID.
Definition: ElggUser.php:602
$password
Definition: login.php:25
getFriendsOf($options=array(), $limit=10, $offset=0)
Gets users who have made this user a friend.
Definition: ElggUser.php:463
getNotificationSettings()
Returns users&#39;s notification settings [ &#39;email&#39; => true, // enabled &#39;ajax&#39; => false, // disabled ]
Definition: ElggUser.php:695
$attrs
Definition: ajax_loader.php:30
$site name
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:373
getFriendsObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user&#39;s friends.
Definition: ElggUser.php:555
isAdmin()
Is this user admin?
Definition: ElggUser.php:284
$CONFIG language
The current language for either the site or the user.
Definition: config.php:51
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:320
$row
create()
{}
Definition: ElggUser.php:133
isBanned()
Is this user banned or not?
Definition: ElggUser.php:275
canComment($user_guid=0, $default=null)
Can a user comment on this user?
Definition: ElggUser.php:648
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:584
$user_guid
Avatar remove action.
Definition: remove.php:6
prepareObject($object)
{}
Definition: ElggUser.php:613
$settings
__set($name, $value)
{}
Definition: ElggUser.php:202
$enabled
CI CLI installer script.
Definition: ci_installer.php:8
$site email
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:406
setDisplayName($displayName)
{}
Definition: ElggUser.php:195
$comment owner_guid
Definition: save.php:58