Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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  }
35 
44  final public static function getExternalAttributes() {
45  return [
46  'name' => null,
47  'username' => null,
48  'password' => null,
49  'salt' => null,
50  'password_hash' => null,
51  'email' => null,
52  'language' => null,
53  'banned' => "no",
54  'admin' => 'no',
55  'prev_last_action' => null,
56  'last_login' => null,
57  'prev_last_login' => null,
58  ];
59  }
60 
71  public function __construct($row = null) {
72  $this->initializeAttributes();
73 
74  if (!empty($row)) {
75  // Is $row is a DB entity row
76  if ($row instanceof \stdClass) {
77  // Load the rest
78  if (!$this->load($row)) {
79  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
80  throw new \IOException($msg);
81  }
82  } else if (is_string($row)) {
83  // $row is a username
84  elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
86  if ($user) {
87  foreach ($user->attributes as $key => $value) {
88  $this->attributes[$key] = $value;
89  }
90  }
91  } else if (is_numeric($row)) {
92  // $row is a GUID so load entity
93  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
94  if (!$this->load($row)) {
95  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
96  }
97  } else {
98  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
99  }
100  }
101  }
102 
110  protected function load($guid) {
111  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'user', $this->attributes);
112  $attr_loader->secondary_loader = 'get_user_entity_as_row';
113 
114  $attrs = $attr_loader->getRequiredAttributes($guid);
115  if (!$attrs) {
116  return false;
117  }
118 
119  $this->attributes = $attrs;
120  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
121  _elgg_services()->entityCache->set($this);
122 
123  return true;
124  }
125 
126 
130  protected function create() {
131  global $CONFIG;
132 
133  $guid = parent::create();
134  $name = sanitize_string($this->name);
136  $password = sanitize_string($this->password);
137  $salt = sanitize_string($this->salt);
138  $password_hash = sanitize_string($this->password_hash);
139  $email = sanitize_string($this->email);
141 
142  $query = "INSERT into {$CONFIG->dbprefix}users_entity
143  (guid, name, username, password, salt, password_hash, email, language)
144  values ($guid, '$name', '$username', '$password', '$salt', '$password_hash', '$email', '$language')";
145 
146  $result = $this->getDatabase()->insertData($query);
147  if ($result === false) {
148  // TODO(evan): Throw an exception here?
149  return false;
150  }
151 
152  return $guid;
153  }
154 
158  protected function update() {
159  global $CONFIG;
160 
161  if (!parent::update()) {
162  return false;
163  }
164 
165  $guid = (int)$this->guid;
166  $name = sanitize_string($this->name);
168  $password = sanitize_string($this->password);
169  $salt = sanitize_string($this->salt);
170  $password_hash = sanitize_string($this->password_hash);
171  $email = sanitize_string($this->email);
173 
174  $query = "UPDATE {$CONFIG->dbprefix}users_entity
175  SET name='$name', username='$username', password='$password', salt='$salt',
176  password_hash='$password_hash', email='$email', language='$language'
177  WHERE guid = $guid";
178 
179  return $this->getDatabase()->updateData($query) !== false;
180  }
181 
187  public function delete() {
188  global $USERNAME_TO_GUID_MAP_CACHE;
189 
190  // clear cache
191  if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
192  unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
193  }
194 
195  // Delete entity
196  return parent::delete();
197  }
198 
202  public function getDisplayName() {
203  return $this->name;
204  }
205 
209  public function setDisplayName($displayName) {
210  $this->name = $displayName;
211  }
212 
216  public function __set($name, $value) {
217  if (!array_key_exists($name, $this->attributes)) {
218  parent::__set($name, $value);
219  return;
220  }
221 
222  switch ($name) {
223  case 'prev_last_action':
224  case 'last_login':
225  case 'prev_last_login':
226  if ($value !== null) {
227  $this->attributes[$name] = (int)$value;
228  } else {
229  $this->attributes[$name] = null;
230  }
231  break;
232 
233  case 'salt':
234  case 'password':
235  elgg_deprecated_notice("Setting salt/password directly is deprecated. Use ElggUser::setPassword().", "1.10");
236  $this->attributes[$name] = $value;
237 
238  // this is emptied so that the user is not left with two usable hashes
239  $this->attributes['password_hash'] = '';
240 
241  break;
242 
243  // setting this not supported
244  case 'password_hash':
245  _elgg_services()->logger->error("password_hash is now an attribute of ElggUser and cannot be set.");
246  return;
247  break;
248 
249  default:
250  parent::__set($name, $value);
251  break;
252  }
253  }
254 
258  public function set($name, $value) {
259  elgg_deprecated_notice("Use -> instead of set()", 1.9);
260  $this->__set($name, $value);
261 
262  return true;
263  }
264 
272  public function ban($reason = "") {
273  return ban_user($this->guid, $reason);
274  }
275 
281  public function unban() {
282  return unban_user($this->guid);
283  }
284 
290  public function isBanned() {
291  return $this->banned == 'yes';
292  }
293 
299  public function isAdmin() {
300 
301  // for backward compatibility we need to pull this directly
302  // from the attributes instead of using the magic methods.
303  // this can be removed in 1.9
304  // return $this->admin == 'yes';
305  return $this->attributes['admin'] == 'yes';
306  }
307 
313  public function makeAdmin() {
314  // If already saved, use the standard function.
315  if ($this->guid && !make_user_admin($this->guid)) {
316  return false;
317  }
318 
319  // need to manually set attributes since they've already been loaded.
320  $this->attributes['admin'] = 'yes';
321 
322  return true;
323  }
324 
330  public function removeAdmin() {
331  // If already saved, use the standard function.
332  if ($this->guid && !remove_user_admin($this->guid)) {
333  return false;
334  }
335 
336  // need to manually set attributes since they've already been loaded.
337  $this->attributes['admin'] = 'no';
338 
339  return true;
340  }
341 
350  public function addFriend($friend_guid, $create_river_item = false) {
351  if (!get_user($friend_guid)) {
352  return false;
353  }
354 
355  if (!add_entity_relationship($this->guid, "friend", $friend_guid)) {
356  return false;
357  }
358 
359  if ($create_river_item) {
361  'view' => 'river/relationship/friend/create',
362  'action_type' => 'friend',
363  'subject_guid' => $this->guid,
364  'object_guid' => $friend_guid,
365  ));
366  }
367 
368  return true;
369  }
370 
378  public function removeFriend($friend_guid) {
379  if (!get_user($friend_guid)) {
380  return false;
381  }
382 
383  // @todo this should be done with a plugin hook handler on the delete relationship
384  // perform cleanup for access lists.
385  $collections = get_user_access_collections($this->guid);
386  if ($collections) {
387  foreach ($collections as $collection) {
389  }
390  }
391 
392  return remove_entity_relationship($this->guid, "friend", $friend_guid);
393  }
394 
400  public function isFriend() {
401  return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
402  }
403 
411  public function isFriendsWith($user_guid) {
412  return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
413  }
414 
422  public function isFriendOf($user_guid) {
423  return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
424  }
425 
437  public function getFriends($options = array(), $limit = 10, $offset = 0) {
438  if (is_array($options)) {
439  $options['relationship'] = 'friend';
440  $options['relationship_guid'] = $this->getGUID();
441  $options['type'] = 'user';
443  } else {
444  elgg_deprecated_notice("\ElggUser::getFriends takes an options array", 1.9);
446  'relationship' => 'friend',
447  'relationship_guid' => $this->guid,
448  'type' => 'user',
449  'subtype' => $options,
450  'limit' => $limit,
451  'offset' => $offset,
452  ));
453  }
454  }
455 
468  public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
469  if (is_array($options)) {
470  $options['relationship'] = 'friend';
471  $options['relationship_guid'] = $this->getGUID();
472  $options['inverse_relationship'] = true;
473  $options['type'] = 'user';
475  } else {
476  elgg_deprecated_notice("\ElggUser::getFriendsOf takes an options array", 1.9);
478  'relationship' => 'friend',
479  'relationship_guid' => $this->guid,
480  'type' => 'user',
481  'subtype' => $options,
482  'limit' => $limit,
483  'offset' => $offset,
484  ));
485  }
486  }
487 
497  public function getGroups($options = "", $limit = 10, $offset = 0) {
498  if (is_string($options)) {
499  elgg_deprecated_notice('\ElggUser::getGroups() takes an options array', 1.9);
500  $subtype = $options;
501  $options = array(
502  'type' => 'group',
503  'relationship' => 'member',
504  'relationship_guid' => $this->guid,
505  'limit' => $limit,
506  'offset' => $offset,
507  );
508 
509  if ($subtype) {
510  $options['subtype'] = $subtype;
511  }
512  } else {
513  $options['type'] = 'group';
514  $options['relationship'] = 'member';
515  $options['relationship_guid'] = $this->guid;
516  }
517 
519  }
520 
531  public function getObjects($options = array(), $limit = 10, $offset = 0) {
532  if (is_array($options)) {
533  $options['type'] = 'object';
534  $options['owner_guid'] = $this->getGUID();
535  return elgg_get_entities($options);
536  } else {
537  elgg_deprecated_notice("\ElggUser::getObjects takes an options array", 1.9);
538  return elgg_get_entities(array(
539  'type' => 'object',
540  'subtype' => $options,
541  'owner_guid' => $this->getGUID(),
542  'limit' => $limit,
543  'offset' => $offset
544  ));
545  }
546  }
547 
560  public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
561  if (is_array($options)) {
562  $options['type'] = 'object';
563  $options['relationship'] = 'friend';
564  $options['relationship_guid'] = $this->getGUID();
565  $options['relationship_join_on'] = 'container_guid';
567  } else {
568  elgg_deprecated_notice("\ElggUser::getFriendsObjects takes an options array", 1.9);
570  'type' => 'object',
571  'subtype' => $options,
572  'limit' => $limit,
573  'offset' => $offset,
574  'relationship' => 'friend',
575  'relationship_guid' => $this->getGUID(),
576  'relationship_join_on' => 'container_guid',
577  ));
578  }
579  }
580 
589  public function countObjects($subtype = "") {
590  elgg_deprecated_notice("\ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
591  $options = [
592  'count' => true,
593  ];
594  if ($subtype) {
595  $options['subtype'] = $subtype;
596  }
597  return (int)$this->getObjects($options);
598  }
599 
607  public function getOwnerGUID() {
608  if ($this->owner_guid == 0) {
609  return $this->guid;
610  }
611 
612  return $this->owner_guid;
613  }
614 
618  protected function prepareObject($object) {
619  $object = parent::prepareObject($object);
620  $object->name = $this->getDisplayName();
621  $object->username = $this->username;
622  $object->language = $this->language;
623  unset($object->read_access);
624  return $object;
625  }
626 
627  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
628 
635  public function getExportableValues() {
636  return array_merge(parent::getExportableValues(), array(
637  'name',
638  'username',
639  'language',
640  ));
641  }
642 
652  public function canComment($user_guid = 0) {
653  $result = parent::canComment($user_guid);
654  if ($result !== null) {
655  return $result;
656  }
657  return false;
658  }
659 
670  public function setPassword($password) {
671  $this->attributes['salt'] = "";
672  $this->attributes['password'] = "";
673  $this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password);
674  }
675 }
load($guid)
Load the data from the database.
Definition: ElggUser.php:110
addFriend($friend_guid, $create_river_item=false)
Adds a user as a friend.
Definition: ElggUser.php:350
set($name, $value)
{}
Definition: ElggUser.php:258
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
update()
{}
Definition: ElggUser.php:158
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:46
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:44
$CONFIG language
The current language for either the site or the user.
Definition: config.php:51
makeAdmin()
Make the user an admin.
Definition: ElggUser.php:313
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's password.
Definition: ElggUser.php:670
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.
$value
Definition: longtext.php:26
$site name
getDisplayName()
{}
Definition: ElggUser.php:202
$subtype
Definition: delete.php:28
if(!$count) $offset
Definition: pagination.php:26
getGUID()
Returns the guid.
$guid
Removes an admin notice.
getGroups($options="", $limit=10, $offset=0)
Gets the user's groups.
Definition: ElggUser.php:497
$collection
$email
Definition: register.php:15
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggUser.php:635
getFriends($options=array(), $limit=10, $offset=0)
Gets this user's friends.
Definition: ElggUser.php:437
unban_user($user_guid)
Unban a user.
Definition: users.php:52
__construct($row=null)
Construct a new user entity.
Definition: ElggUser.php:71
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: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
$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:272
unban()
Unban this user.
Definition: ElggUser.php:281
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
$language
Definition: useradd.php:20
$comment owner_guid
Definition: save.php:58
isFriend()
Determines whether or not this user is a friend of the currently logged in user.
Definition: ElggUser.php:400
global $CONFIG
$user
Definition: ban.php:13
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:316
isFriendOf($user_guid)
Determines whether or not this user is another user's friend.
Definition: ElggUser.php:422
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:1070
$friend_guid
Definition: add.php:10
getObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user.
Definition: ElggUser.php:531
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
getOwnerGUID()
Get a user's owner GUID.
Definition: ElggUser.php:607
$password
Definition: login.php:25
canComment($user_guid=0)
Can a user comment on this user?
Definition: ElggUser.php:652
getFriendsOf($options=array(), $limit=10, $offset=0)
Gets users who have made this user a friend.
Definition: ElggUser.php:468
$attrs
Definition: ajax_loader.php:30
removeFriend($friend_guid)
Removes a user as a friend.
Definition: ElggUser.php:378
getFriendsObjects($options=array(), $limit=10, $offset=0)
Get an array of owned by this user's friends.
Definition: ElggUser.php:560
isAdmin()
Is this user admin?
Definition: ElggUser.php:299
username
Definition: contents.php:36
removeAdmin()
Remove the admin flag for user.
Definition: ElggUser.php:330
$row
create()
{}
Definition: ElggUser.php:130
isBanned()
Is this user banned or not?
Definition: ElggUser.php:290
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:589
$user_guid
Avatar remove action.
Definition: remove.php:6
prepareObject($object)
{}
Definition: ElggUser.php:618
__set($name, $value)
{}
Definition: ElggUser.php:216
$site email
remove_user_admin($user_guid)
Removes user $guid's admin flag.
Definition: users.php:74
isFriendsWith($user_guid)
Determines whether this user is friends with another user.
Definition: ElggUser.php:411
setDisplayName($displayName)
{}
Definition: ElggUser.php:209