ElggUser.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <?php
  2. /**
  3. * \ElggUser
  4. *
  5. * Representation of a "user" in the system.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage DataModel.User
  9. *
  10. * @property string $name The display name that the user will be known by in the network
  11. * @property string $username The short, reference name for the user in the network
  12. * @property string $email The email address to which Elgg will send email notifications
  13. * @property string $language The language preference of the user (ISO 639-1 formatted)
  14. * @property string $banned 'yes' if the user is banned from the network, 'no' otherwise
  15. * @property string $admin 'yes' if the user is an administrator of the network, 'no' otherwise
  16. * @property-read string $password The legacy (salted MD5) password hash of the user
  17. * @property-read string $salt The salt used to create the legacy password hash
  18. * @property-read string $password_hash The hashed password of the user
  19. */
  20. class ElggUser extends \ElggEntity
  21. implements Friendable {
  22. /**
  23. * Initialize the attributes array.
  24. * This is vital to distinguish between metadata and base attributes.
  25. *
  26. * @return void
  27. */
  28. protected function initializeAttributes() {
  29. parent::initializeAttributes();
  30. $this->attributes['type'] = "user";
  31. $this->attributes += self::getExternalAttributes();
  32. $this->tables_split = 2;
  33. }
  34. /**
  35. * Get default values for attributes stored in a separate table
  36. *
  37. * @return array
  38. * @access private
  39. *
  40. * @see \Elgg\Database\EntityTable::getEntities
  41. */
  42. final public static function getExternalAttributes() {
  43. return [
  44. 'name' => null,
  45. 'username' => null,
  46. 'password' => null,
  47. 'salt' => null,
  48. 'password_hash' => null,
  49. 'email' => null,
  50. 'language' => null,
  51. 'banned' => "no",
  52. 'admin' => 'no',
  53. 'prev_last_action' => null,
  54. 'last_login' => null,
  55. 'prev_last_login' => null,
  56. ];
  57. }
  58. /**
  59. * Construct a new user entity
  60. *
  61. * Plugin developers should only use the constructor to create a new entity.
  62. * To retrieve entities, use get_entity() and the elgg_get_entities* functions.
  63. *
  64. * @param \stdClass $row Database row result. Default is null to create a new user.
  65. *
  66. * @throws IOException|InvalidParameterException if there was a problem creating the user.
  67. */
  68. public function __construct($row = null) {
  69. $this->initializeAttributes();
  70. // compatibility for 1.7 api.
  71. $this->initialise_attributes(false);
  72. if (!empty($row)) {
  73. // Is $row is a DB entity row
  74. if ($row instanceof \stdClass) {
  75. // Load the rest
  76. if (!$this->load($row)) {
  77. $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
  78. throw new \IOException($msg);
  79. }
  80. } else if (is_string($row)) {
  81. // $row is a username
  82. elgg_deprecated_notice('Passing a username to constructor is deprecated. Use get_user_by_username()', 1.9);
  83. $user = get_user_by_username($row);
  84. if ($user) {
  85. foreach ($user->attributes as $key => $value) {
  86. $this->attributes[$key] = $value;
  87. }
  88. }
  89. } else if ($row instanceof \ElggUser) {
  90. // $row is an \ElggUser so this is a copy constructor
  91. elgg_deprecated_notice('This type of usage of the \ElggUser constructor was deprecated. Please use the clone method.', 1.7);
  92. foreach ($row->attributes as $key => $value) {
  93. $this->attributes[$key] = $value;
  94. }
  95. } else if (is_numeric($row)) {
  96. // $row is a GUID so load entity
  97. elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
  98. if (!$this->load($row)) {
  99. throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
  100. }
  101. } else {
  102. throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
  103. }
  104. }
  105. }
  106. /**
  107. * Load the \ElggUser data from the database
  108. *
  109. * @param mixed $guid \ElggUser GUID or \stdClass database row from entity table
  110. *
  111. * @return bool
  112. */
  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. $attrs = $attr_loader->getRequiredAttributes($guid);
  117. if (!$attrs) {
  118. return false;
  119. }
  120. $this->attributes = $attrs;
  121. $this->tables_loaded = 2;
  122. $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
  123. _elgg_cache_entity($this);
  124. return true;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. protected function create() {
  130. global $CONFIG;
  131. $guid = parent::create();
  132. $name = sanitize_string($this->name);
  133. $username = sanitize_string($this->username);
  134. $password = sanitize_string($this->password);
  135. $salt = sanitize_string($this->salt);
  136. $password_hash = sanitize_string($this->password_hash);
  137. $email = sanitize_string($this->email);
  138. $language = sanitize_string($this->language);
  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. $result = $this->getDatabase()->insertData($query);
  143. if ($result === false) {
  144. // TODO(evan): Throw an exception here?
  145. return false;
  146. }
  147. return $guid;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. protected function update() {
  153. global $CONFIG;
  154. if (!parent::update()) {
  155. return false;
  156. }
  157. $guid = (int)$this->guid;
  158. $name = sanitize_string($this->name);
  159. $username = sanitize_string($this->username);
  160. $password = sanitize_string($this->password);
  161. $salt = sanitize_string($this->salt);
  162. $password_hash = sanitize_string($this->password_hash);
  163. $email = sanitize_string($this->email);
  164. $language = sanitize_string($this->language);
  165. $query = "UPDATE {$CONFIG->dbprefix}users_entity
  166. SET name='$name', username='$username', password='$password', salt='$salt',
  167. password_hash='$password_hash', email='$email', language='$language'
  168. WHERE guid = $guid";
  169. return $this->getDatabase()->updateData($query) !== false;
  170. }
  171. /**
  172. * User specific override of the entity delete method.
  173. *
  174. * @return bool
  175. */
  176. public function delete() {
  177. global $USERNAME_TO_GUID_MAP_CACHE;
  178. // clear cache
  179. if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
  180. unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
  181. }
  182. // Delete entity
  183. return parent::delete();
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getDisplayName() {
  189. return $this->name;
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function setDisplayName($displayName) {
  195. $this->name = $displayName;
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function __set($name, $value) {
  201. if (!array_key_exists($name, $this->attributes)) {
  202. parent::__set($name, $value);
  203. return;
  204. }
  205. switch ($name) {
  206. case 'prev_last_action':
  207. case 'last_login':
  208. case 'prev_last_login':
  209. if ($value !== null) {
  210. $this->attributes[$name] = (int)$value;
  211. } else {
  212. $this->attributes[$name] = null;
  213. }
  214. break;
  215. case 'salt':
  216. case 'password':
  217. elgg_deprecated_notice("Setting salt/password directly is deprecated. Use ElggUser::setPassword().", "1.10");
  218. $this->attributes[$name] = $value;
  219. // this is emptied so that the user is not left with two usable hashes
  220. $this->attributes['password_hash'] = '';
  221. break;
  222. // setting this not supported
  223. case 'password_hash':
  224. _elgg_services()->logger->error("password_hash is now an attribute of ElggUser and cannot be set.");
  225. return;
  226. break;
  227. default:
  228. parent::__set($name, $value);
  229. break;
  230. }
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function set($name, $value) {
  236. elgg_deprecated_notice("Use -> instead of set()", 1.9);
  237. $this->__set($name, $value);
  238. return true;
  239. }
  240. /**
  241. * Ban this user.
  242. *
  243. * @param string $reason Optional reason
  244. *
  245. * @return bool
  246. */
  247. public function ban($reason = "") {
  248. return ban_user($this->guid, $reason);
  249. }
  250. /**
  251. * Unban this user.
  252. *
  253. * @return bool
  254. */
  255. public function unban() {
  256. return unban_user($this->guid);
  257. }
  258. /**
  259. * Is this user banned or not?
  260. *
  261. * @return bool
  262. */
  263. public function isBanned() {
  264. return $this->banned == 'yes';
  265. }
  266. /**
  267. * Is this user admin?
  268. *
  269. * @return bool
  270. */
  271. public function isAdmin() {
  272. // for backward compatibility we need to pull this directly
  273. // from the attributes instead of using the magic methods.
  274. // this can be removed in 1.9
  275. // return $this->admin == 'yes';
  276. return $this->attributes['admin'] == 'yes';
  277. }
  278. /**
  279. * Make the user an admin
  280. *
  281. * @return bool
  282. */
  283. public function makeAdmin() {
  284. // If already saved, use the standard function.
  285. if ($this->guid && !make_user_admin($this->guid)) {
  286. return false;
  287. }
  288. // need to manually set attributes since they've already been loaded.
  289. $this->attributes['admin'] = 'yes';
  290. return true;
  291. }
  292. /**
  293. * Remove the admin flag for user
  294. *
  295. * @return bool
  296. */
  297. public function removeAdmin() {
  298. // If already saved, use the standard function.
  299. if ($this->guid && !remove_user_admin($this->guid)) {
  300. return false;
  301. }
  302. // need to manually set attributes since they've already been loaded.
  303. $this->attributes['admin'] = 'no';
  304. return true;
  305. }
  306. /**
  307. * Get sites that this user is a member of
  308. *
  309. * @param array $options Options array. Used to be $subtype
  310. * @param int $limit The number of results to return (deprecated)
  311. * @param int $offset Any indexing offset (deprecated)
  312. *
  313. * @return array
  314. */
  315. public function getSites($options = "", $limit = 10, $offset = 0) {
  316. if (is_string($options)) {
  317. elgg_deprecated_notice('\ElggUser::getSites() takes an options array', 1.9);
  318. return get_user_sites($this->getGUID(), $limit, $offset);
  319. }
  320. return parent::getSites($options);
  321. }
  322. /**
  323. * Add this user to a particular site
  324. *
  325. * @param \ElggSite $site The site to add this user to. This used to be the
  326. * the site guid (still supported by deprecated)
  327. * @return bool
  328. */
  329. public function addToSite($site) {
  330. if (is_numeric($site)) {
  331. elgg_deprecated_notice('\ElggUser::addToSite() takes a site entity', 1.9);
  332. return add_site_user($site, $this->getGUID());
  333. }
  334. return parent::addToSite($site);
  335. }
  336. /**
  337. * Remove this user from a particular site
  338. *
  339. * @param \ElggSite $site The site to remove the user from. Used to be site GUID
  340. *
  341. * @return bool
  342. */
  343. public function removeFromSite($site) {
  344. if (is_numeric($site)) {
  345. elgg_deprecated_notice('\ElggUser::removeFromSite() takes a site entity', 1.9);
  346. return remove_site_user($site, $this->guid);
  347. }
  348. return parent::removeFromSite($site);
  349. }
  350. /**
  351. * Adds a user as a friend
  352. *
  353. * @param int $friend_guid The GUID of the user to add
  354. * @param bool $create_river_item Create the river item announcing this friendship
  355. *
  356. * @return bool
  357. */
  358. public function addFriend($friend_guid, $create_river_item = false) {
  359. if (!get_user($friend_guid)) {
  360. return false;
  361. }
  362. if (!add_entity_relationship($this->guid, "friend", $friend_guid)) {
  363. return false;
  364. }
  365. if ($create_river_item) {
  366. elgg_create_river_item(array(
  367. 'view' => 'river/relationship/friend/create',
  368. 'action_type' => 'friend',
  369. 'subject_guid' => $this->guid,
  370. 'object_guid' => $friend_guid,
  371. ));
  372. }
  373. return true;
  374. }
  375. /**
  376. * Removes a user as a friend
  377. *
  378. * @param int $friend_guid The GUID of the user to remove
  379. *
  380. * @return bool
  381. */
  382. public function removeFriend($friend_guid) {
  383. if (!get_user($friend_guid)) {
  384. return false;
  385. }
  386. // @todo this should be done with a plugin hook handler on the delete relationship
  387. // perform cleanup for access lists.
  388. $collections = get_user_access_collections($this->guid);
  389. if ($collections) {
  390. foreach ($collections as $collection) {
  391. remove_user_from_access_collection($friend_guid, $collection->id);
  392. }
  393. }
  394. return remove_entity_relationship($this->guid, "friend", $friend_guid);
  395. }
  396. /**
  397. * Determines whether or not this user is a friend of the currently logged in user
  398. *
  399. * @return bool
  400. */
  401. public function isFriend() {
  402. return $this->isFriendOf(_elgg_services()->session->getLoggedInUserGuid());
  403. }
  404. /**
  405. * Determines whether this user is friends with another user
  406. *
  407. * @param int $user_guid The GUID of the user to check against
  408. *
  409. * @return bool
  410. */
  411. public function isFriendsWith($user_guid) {
  412. return (bool)check_entity_relationship($this->guid, "friend", $user_guid);
  413. }
  414. /**
  415. * Determines whether or not this user is another user's friend
  416. *
  417. * @param int $user_guid The GUID of the user to check against
  418. *
  419. * @return bool
  420. */
  421. public function isFriendOf($user_guid) {
  422. return (bool)check_entity_relationship($user_guid, "friend", $this->guid);
  423. }
  424. /**
  425. * Gets this user's friends
  426. *
  427. * @param array $options Options array. See elgg_get_entities_from_relationship()
  428. * for a list of options. 'relationship_guid' is set to
  429. * this entity, relationship name to 'friend' and type to 'user'.
  430. * @param int $limit The number of users to retrieve (deprecated)
  431. * @param int $offset Indexing offset, if any (deprecated)
  432. *
  433. * @return array|false Array of \ElggUser, or false, depending on success
  434. */
  435. public function getFriends($options = array(), $limit = 10, $offset = 0) {
  436. if (is_array($options)) {
  437. $options['relationship'] = 'friend';
  438. $options['relationship_guid'] = $this->getGUID();
  439. $options['type'] = 'user';
  440. return elgg_get_entities_from_relationship($options);
  441. } else {
  442. elgg_deprecated_notice("\ElggUser::getFriends takes an options array", 1.9);
  443. return elgg_get_entities_from_relationship(array(
  444. 'relationship' => 'friend',
  445. 'relationship_guid' => $this->guid,
  446. 'type' => 'user',
  447. 'subtype' => $options,
  448. 'limit' => $limit,
  449. 'offset' => $offset,
  450. ));
  451. }
  452. }
  453. /**
  454. * Gets users who have made this user a friend
  455. *
  456. * @param array $options Options array. See elgg_get_entities_from_relationship()
  457. * for a list of options. 'relationship_guid' is set to
  458. * this entity, relationship name to 'friend', type to 'user'
  459. * and inverse_relationship to true.
  460. * @param int $limit The number of users to retrieve (deprecated)
  461. * @param int $offset Indexing offset, if any (deprecated)
  462. *
  463. * @return array|false Array of \ElggUser, or false, depending on success
  464. */
  465. public function getFriendsOf($options = array(), $limit = 10, $offset = 0) {
  466. if (is_array($options)) {
  467. $options['relationship'] = 'friend';
  468. $options['relationship_guid'] = $this->getGUID();
  469. $options['inverse_relationship'] = true;
  470. $options['type'] = 'user';
  471. return elgg_get_entities_from_relationship($options);
  472. } else {
  473. elgg_deprecated_notice("\ElggUser::getFriendsOf takes an options array", 1.9);
  474. return elgg_get_entities_from_relationship(array(
  475. 'relationship' => 'friend',
  476. 'relationship_guid' => $this->guid,
  477. 'type' => 'user',
  478. 'subtype' => $options,
  479. 'limit' => $limit,
  480. 'offset' => $offset,
  481. ));
  482. }
  483. }
  484. /**
  485. * Lists the user's friends
  486. *
  487. * @param string $subtype Optionally, the user subtype (leave blank for all)
  488. * @param int $limit The number of users to retrieve
  489. * @param array $vars Display variables for the user view
  490. *
  491. * @return string Rendered list of friends
  492. * @since 1.8.0
  493. * @deprecated 1.9 Use elgg_list_entities_from_relationship()
  494. */
  495. public function listFriends($subtype = "", $limit = 10, array $vars = array()) {
  496. elgg_deprecated_notice('\ElggUser::listFriends() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
  497. $defaults = array(
  498. 'type' => 'user',
  499. 'relationship' => 'friend',
  500. 'relationship_guid' => $this->guid,
  501. 'limit' => $limit,
  502. 'full_view' => false,
  503. );
  504. $options = array_merge($defaults, $vars);
  505. if ($subtype) {
  506. $options['subtype'] = $subtype;
  507. }
  508. return elgg_list_entities_from_relationship($options);
  509. }
  510. /**
  511. * Gets the user's groups
  512. *
  513. * @param array $options Options array. Used to be the subtype string.
  514. * @param int $limit The number of groups to retrieve (deprecated)
  515. * @param int $offset Indexing offset, if any (deprecated)
  516. *
  517. * @return array|false Array of \ElggGroup, or false, depending on success
  518. */
  519. public function getGroups($options = "", $limit = 10, $offset = 0) {
  520. if (is_string($options)) {
  521. elgg_deprecated_notice('\ElggUser::getGroups() takes an options array', 1.9);
  522. $subtype = $options;
  523. $options = array(
  524. 'type' => 'group',
  525. 'relationship' => 'member',
  526. 'relationship_guid' => $this->guid,
  527. 'limit' => $limit,
  528. 'offset' => $offset,
  529. );
  530. if ($subtype) {
  531. $options['subtype'] = $subtype;
  532. }
  533. } else {
  534. $options['type'] = 'group';
  535. $options['relationship'] = 'member';
  536. $options['relationship_guid'] = $this->guid;
  537. }
  538. return elgg_get_entities_from_relationship($options);
  539. }
  540. /**
  541. * Lists the user's groups
  542. *
  543. * @param string $subtype Optionally, the user subtype (leave blank for all)
  544. * @param int $limit The number of users to retrieve
  545. * @param int $offset Indexing offset, if any
  546. *
  547. * @return string
  548. * @deprecated 1.9 Use elgg_list_entities_from_relationship()
  549. */
  550. public function listGroups($subtype = "", $limit = 10, $offset = 0) {
  551. elgg_deprecated_notice('Elgg::listGroups is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
  552. $options = array(
  553. 'type' => 'group',
  554. 'relationship' => 'member',
  555. 'relationship_guid' => $this->guid,
  556. 'limit' => $limit,
  557. 'offset' => $offset,
  558. 'full_view' => false,
  559. );
  560. if ($subtype) {
  561. $options['subtype'] = $subtype;
  562. }
  563. return elgg_list_entities_from_relationship($options);
  564. }
  565. /**
  566. * Get an array of \ElggObject owned by this user.
  567. *
  568. * @param array $options Options array. See elgg_get_entities() for a list of options.
  569. * 'type' is set to object and owner_guid to this entity.
  570. * @param int $limit Number of results to return (deprecated)
  571. * @param int $offset Any indexing offset (deprecated)
  572. *
  573. * @return array|false
  574. */
  575. public function getObjects($options = array(), $limit = 10, $offset = 0) {
  576. if (is_array($options)) {
  577. $options['type'] = 'object';
  578. $options['owner_guid'] = $this->getGUID();
  579. return elgg_get_entities($options);
  580. } else {
  581. elgg_deprecated_notice("\ElggUser::getObjects takes an options array", 1.9);
  582. return elgg_get_entities(array(
  583. 'type' => 'object',
  584. 'subtype' => $options,
  585. 'owner_guid' => $this->getGUID(),
  586. 'limit' => $limit,
  587. 'offset' => $offset
  588. ));
  589. }
  590. }
  591. /**
  592. * Get an array of \ElggObjects owned by this user's friends.
  593. *
  594. * @param array $options Options array. See elgg_get_entities_from_relationship()
  595. * for a list of options. 'relationship_guid' is set to
  596. * this entity, type to 'object', relationship name to 'friend'
  597. * and relationship_join_on to 'container_guid'.
  598. * @param int $limit Number of results to return (deprecated)
  599. * @param int $offset Any indexing offset (deprecated)
  600. *
  601. * @return array|false
  602. */
  603. public function getFriendsObjects($options = array(), $limit = 10, $offset = 0) {
  604. if (is_array($options)) {
  605. $options['type'] = 'object';
  606. $options['relationship'] = 'friend';
  607. $options['relationship_guid'] = $this->getGUID();
  608. $options['relationship_join_on'] = 'container_guid';
  609. return elgg_get_entities_from_relationship($options);
  610. } else {
  611. elgg_deprecated_notice("\ElggUser::getFriendsObjects takes an options array", 1.9);
  612. return elgg_get_entities_from_relationship(array(
  613. 'type' => 'object',
  614. 'subtype' => $options,
  615. 'limit' => $limit,
  616. 'offset' => $offset,
  617. 'relationship' => 'friend',
  618. 'relationship_guid' => $this->getGUID(),
  619. 'relationship_join_on' => 'container_guid',
  620. ));
  621. }
  622. }
  623. /**
  624. * Counts the number of \ElggObjects owned by this user
  625. *
  626. * @param string $subtype The subtypes of the objects, if any
  627. *
  628. * @return int The number of \ElggObjects
  629. * @deprecated 1.9 Use elgg_get_entities()
  630. */
  631. public function countObjects($subtype = "") {
  632. elgg_deprecated_notice("\ElggUser::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
  633. return count_user_objects($this->getGUID(), $subtype);
  634. }
  635. /**
  636. * Get the collections associated with a user.
  637. *
  638. * @param string $subtype Optionally, the subtype of result we want to limit to
  639. * @param int $limit The number of results to return
  640. * @param int $offset Any indexing offset
  641. *
  642. * @return array|false
  643. */
  644. public function getCollections($subtype = "", $limit = 10, $offset = 0) {
  645. elgg_deprecated_notice("\ElggUser::getCollections() has been deprecated", 1.8);
  646. return false;
  647. }
  648. /**
  649. * Get a user's owner GUID
  650. *
  651. * Returns it's own GUID if the user is not owned.
  652. *
  653. * @return int
  654. */
  655. public function getOwnerGUID() {
  656. if ($this->owner_guid == 0) {
  657. return $this->guid;
  658. }
  659. return $this->owner_guid;
  660. }
  661. /**
  662. * If a user's owner is blank, return its own GUID as the owner
  663. *
  664. * @return int User GUID
  665. * @deprecated 1.8 Use getOwnerGUID()
  666. */
  667. public function getOwner() {
  668. elgg_deprecated_notice("\ElggUser::getOwner deprecated for \ElggUser::getOwnerGUID", 1.8);
  669. $this->getOwnerGUID();
  670. }
  671. /**
  672. * {@inheritdoc}
  673. */
  674. protected function prepareObject($object) {
  675. $object = parent::prepareObject($object);
  676. $object->name = $this->getDisplayName();
  677. $object->username = $this->username;
  678. $object->language = $this->language;
  679. unset($object->read_access);
  680. return $object;
  681. }
  682. // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
  683. /**
  684. * Return an array of fields which can be exported.
  685. *
  686. * @return array
  687. * @deprecated 1.9 Use toObject()
  688. */
  689. public function getExportableValues() {
  690. return array_merge(parent::getExportableValues(), array(
  691. 'name',
  692. 'username',
  693. 'language',
  694. ));
  695. }
  696. /**
  697. * Can a user comment on this user?
  698. *
  699. * @see \ElggEntity::canComment()
  700. *
  701. * @param int $user_guid User guid (default is logged in user)
  702. * @return bool
  703. * @since 1.8.0
  704. */
  705. public function canComment($user_guid = 0) {
  706. $result = parent::canComment($user_guid);
  707. if ($result !== null) {
  708. return $result;
  709. }
  710. return false;
  711. }
  712. /**
  713. * Set the necessary attributes to store a hash of the user's password. Also removes
  714. * the legacy hash/salt values.
  715. *
  716. * @tip You must save() to persist the attributes
  717. *
  718. * @param string $password The password to be hashed
  719. * @return void
  720. * @since 1.10.0
  721. */
  722. public function setPassword($password) {
  723. $this->attributes['salt'] = "";
  724. $this->attributes['password'] = "";
  725. $this->attributes['password_hash'] = _elgg_services()->passwords->generateHash($password);
  726. }
  727. }