users.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. /**
  3. * Elgg users
  4. * Functions to manage multiple or single users in an Elgg install
  5. *
  6. * @package Elgg.Core
  7. * @subpackage DataModel.User
  8. */
  9. /**
  10. * Return the user specific details of a user by a row.
  11. *
  12. * @param int $guid The \ElggUser guid
  13. *
  14. * @return mixed
  15. * @access private
  16. */
  17. function get_user_entity_as_row($guid) {
  18. return _elgg_services()->usersTable->getRow($guid);
  19. }
  20. /**
  21. * Disables all of a user's entities
  22. *
  23. * @param int $owner_guid The owner GUID
  24. *
  25. * @return bool Depending on success
  26. */
  27. function disable_user_entities($owner_guid) {
  28. return _elgg_services()->usersTable->disableEntities($owner_guid);
  29. }
  30. /**
  31. * Ban a user
  32. *
  33. * @param int $user_guid The user guid
  34. * @param string $reason A reason
  35. *
  36. * @return bool
  37. */
  38. function ban_user($user_guid, $reason = "") {
  39. return _elgg_services()->usersTable->ban($user_guid, $reason);
  40. }
  41. /**
  42. * Unban a user.
  43. *
  44. * @param int $user_guid Unban a user.
  45. *
  46. * @return bool
  47. */
  48. function unban_user($user_guid) {
  49. return _elgg_services()->usersTable->unban($user_guid);
  50. }
  51. /**
  52. * Makes user $guid an admin.
  53. *
  54. * @param int $user_guid User guid
  55. *
  56. * @return bool
  57. */
  58. function make_user_admin($user_guid) {
  59. return _elgg_services()->usersTable->makeAdmin($user_guid);
  60. }
  61. /**
  62. * Removes user $guid's admin flag.
  63. *
  64. * @param int $user_guid User GUID
  65. *
  66. * @return bool
  67. */
  68. function remove_user_admin($user_guid) {
  69. return _elgg_services()->usersTable->removeAdmin($user_guid);
  70. }
  71. /**
  72. * Get a user object from a GUID.
  73. *
  74. * This function returns an \ElggUser from a given GUID.
  75. *
  76. * @param int $guid The GUID
  77. *
  78. * @return \ElggUser|false
  79. */
  80. function get_user($guid) {
  81. return _elgg_services()->entityTable->get($guid, 'user');
  82. }
  83. /**
  84. * Get user by username
  85. *
  86. * @param string $username The user's username
  87. *
  88. * @return \ElggUser|false Depending on success
  89. */
  90. function get_user_by_username($username) {
  91. return _elgg_services()->usersTable->getByUsername($username);
  92. }
  93. /**
  94. * Get user by persistent login password
  95. *
  96. * @param string $hash Hash of the persistent login password
  97. *
  98. * @return \ElggUser
  99. */
  100. function get_user_by_code($hash) {
  101. return _elgg_services()->persistentLogin->getUserFromHash($hash);
  102. }
  103. /**
  104. * Get an array of users from an email address
  105. *
  106. * @param string $email Email address.
  107. *
  108. * @return array
  109. */
  110. function get_user_by_email($email) {
  111. return _elgg_services()->usersTable->getByEmail($email);
  112. }
  113. /**
  114. * Return users (or the number of them) who have been active within a recent period.
  115. *
  116. * @param array $options Array of options with keys:
  117. *
  118. * seconds (int) => Length of period (default 600 = 10min)
  119. * limit (int) => Limit (default from settings)
  120. * offset (int) => Offset (default 0)
  121. * count (bool) => Return a count instead of users? (default false)
  122. *
  123. * Formerly this was the seconds parameter.
  124. *
  125. * @param int $limit Limit (deprecated usage, use $options)
  126. * @param int $offset Offset (deprecated usage, use $options)
  127. * @param bool $count Count (deprecated usage, use $options)
  128. *
  129. * @return \ElggUser[]|int
  130. */
  131. function find_active_users($options = array(), $limit = 10, $offset = 0, $count = false) {
  132. return _elgg_services()->usersTable->findActive($options, $limit, $offset, $count);
  133. }
  134. /**
  135. * Generate and send a password request email to a given user's registered email address.
  136. *
  137. * @param int $user_guid User GUID
  138. *
  139. * @return bool
  140. */
  141. function send_new_password_request($user_guid) {
  142. return _elgg_services()->passwords->sendNewPasswordRequest($user_guid);
  143. }
  144. /**
  145. * Low level function to reset a given user's password.
  146. *
  147. * This can only be called from execute_new_password_request().
  148. *
  149. * @param int $user_guid The user.
  150. * @param string $password Text (which will then be converted into a hash and stored)
  151. *
  152. * @return bool
  153. */
  154. function force_user_password_reset($user_guid, $password) {
  155. return _elgg_services()->passwords->forcePasswordReset($user_guid, $password);
  156. }
  157. /**
  158. * Validate and change password for a user.
  159. *
  160. * @param int $user_guid The user id
  161. * @param string $conf_code Confirmation code as sent in the request email.
  162. * @param string $password Optional new password, if not randomly generated.
  163. *
  164. * @return bool True on success
  165. */
  166. function execute_new_password_request($user_guid, $conf_code, $password = null) {
  167. return _elgg_services()->passwords->executeNewPasswordReset($user_guid, $conf_code, $password);
  168. }
  169. /**
  170. * Generate a random 12 character clear text password.
  171. *
  172. * @return string
  173. */
  174. function generate_random_cleartext_password() {
  175. return _elgg_services()->crypto->getRandomString(12, \ElggCrypto::CHARS_PASSWORD);
  176. }
  177. /**
  178. * Simple function which ensures that a username contains only valid characters.
  179. *
  180. * This should only permit chars that are valid on the file system as well.
  181. *
  182. * @param string $username Username
  183. *
  184. * @return bool
  185. * @throws RegistrationException on invalid
  186. */
  187. function validate_username($username) {
  188. global $CONFIG;
  189. // Basic, check length
  190. if (!isset($CONFIG->minusername)) {
  191. $CONFIG->minusername = 4;
  192. }
  193. if (strlen($username) < $CONFIG->minusername) {
  194. $msg = elgg_echo('registration:usernametooshort', array($CONFIG->minusername));
  195. throw new \RegistrationException($msg);
  196. }
  197. // username in the database has a limit of 128 characters
  198. if (strlen($username) > 128) {
  199. $msg = elgg_echo('registration:usernametoolong', array(128));
  200. throw new \RegistrationException($msg);
  201. }
  202. // Blacklist for bad characters (partially nicked from mediawiki)
  203. $blacklist = '/[' .
  204. '\x{0080}-\x{009f}' . // iso-8859-1 control chars
  205. '\x{00a0}' . // non-breaking space
  206. '\x{2000}-\x{200f}' . // various whitespace
  207. '\x{2028}-\x{202f}' . // breaks and control chars
  208. '\x{3000}' . // ideographic space
  209. '\x{e000}-\x{f8ff}' . // private use
  210. ']/u';
  211. if (preg_match($blacklist, $username)) {
  212. // @todo error message needs work
  213. throw new \RegistrationException(elgg_echo('registration:invalidchars'));
  214. }
  215. // Belts and braces
  216. // @todo Tidy into main unicode
  217. $blacklist2 = '\'/\\"*& ?#%^(){}[]~?<>;|¬`@+=';
  218. $blacklist2 = elgg_trigger_plugin_hook('username:character_blacklist', 'user',
  219. array('blacklist' => $blacklist2), $blacklist2);
  220. for ($n = 0; $n < strlen($blacklist2); $n++) {
  221. if (strpos($username, $blacklist2[$n]) !== false) {
  222. $msg = elgg_echo('registration:invalidchars', array($blacklist2[$n], $blacklist2));
  223. $msg = htmlspecialchars($msg, ENT_QUOTES, 'UTF-8');
  224. throw new \RegistrationException($msg);
  225. }
  226. }
  227. $result = true;
  228. return elgg_trigger_plugin_hook('registeruser:validate:username', 'all',
  229. array('username' => $username), $result);
  230. }
  231. /**
  232. * Simple validation of a password.
  233. *
  234. * @param string $password Clear text password
  235. *
  236. * @return bool
  237. * @throws RegistrationException on invalid
  238. */
  239. function validate_password($password) {
  240. global $CONFIG;
  241. if (!isset($CONFIG->min_password_length)) {
  242. $CONFIG->min_password_length = 6;
  243. }
  244. if (strlen($password) < $CONFIG->min_password_length) {
  245. $msg = elgg_echo('registration:passwordtooshort', array($CONFIG->min_password_length));
  246. throw new \RegistrationException($msg);
  247. }
  248. $result = true;
  249. return elgg_trigger_plugin_hook('registeruser:validate:password', 'all',
  250. array('password' => $password), $result);
  251. }
  252. /**
  253. * Simple validation of a email.
  254. *
  255. * @param string $address Email address
  256. *
  257. * @throws RegistrationException on invalid
  258. * @return bool
  259. */
  260. function validate_email_address($address) {
  261. if (!is_email_address($address)) {
  262. throw new \RegistrationException(elgg_echo('registration:notemail'));
  263. }
  264. // Got here, so lets try a hook (defaulting to ok)
  265. $result = true;
  266. return elgg_trigger_plugin_hook('registeruser:validate:email', 'all',
  267. array('email' => $address), $result);
  268. }
  269. /**
  270. * Registers a user, returning false if the username already exists
  271. *
  272. * @param string $username The username of the new user
  273. * @param string $password The password
  274. * @param string $name The user's display name
  275. * @param string $email The user's email address
  276. * @param bool $allow_multiple_emails Allow the same email address to be
  277. * registered multiple times?
  278. *
  279. * @return int|false The new user's GUID; false on failure
  280. * @throws RegistrationException
  281. */
  282. function register_user($username, $password, $name, $email, $allow_multiple_emails = false) {
  283. return _elgg_services()->usersTable->register($username, $password, $name, $email, $allow_multiple_emails);
  284. }
  285. /**
  286. * Generates a unique invite code for a user
  287. *
  288. * @param string $username The username of the user sending the invitation
  289. *
  290. * @return string Invite code
  291. * @see elgg_validate_invite_code
  292. */
  293. function generate_invite_code($username) {
  294. return _elgg_services()->usersTable->generateInviteCode($username);
  295. }
  296. /**
  297. * Validate a user's invite code
  298. *
  299. * @param string $username The username
  300. * @param string $code The invite code
  301. *
  302. * @return bool
  303. * @see generate_invite_code
  304. * @since 1.10
  305. */
  306. function elgg_validate_invite_code($username, $code) {
  307. return _elgg_services()->usersTable->validateInviteCode($username, $code);
  308. }
  309. /**
  310. * Set the validation status for a user.
  311. *
  312. * @param int $user_guid The user's GUID
  313. * @param bool $status Validated (true) or unvalidated (false)
  314. * @param string $method Optional method to say how a user was validated
  315. * @return bool
  316. * @since 1.8.0
  317. */
  318. function elgg_set_user_validation_status($user_guid, $status, $method = '') {
  319. return _elgg_services()->usersTable->setValidationStatus($user_guid, $status, $method);
  320. }
  321. /**
  322. * Gets the validation status of a user.
  323. *
  324. * @param int $user_guid The user's GUID
  325. * @return bool|null Null means status was not set for this user.
  326. * @since 1.8.0
  327. */
  328. function elgg_get_user_validation_status($user_guid) {
  329. return _elgg_services()->usersTable->getValidationStatus($user_guid);
  330. }
  331. /**
  332. * Page handler for account related pages
  333. *
  334. * @param array $page_elements Page elements
  335. * @param string $handler The handler string
  336. *
  337. * @return bool
  338. * @access private
  339. */
  340. function elgg_user_account_page_handler($page_elements, $handler) {
  341. $base_dir = elgg_get_root_path() . 'pages/account';
  342. switch ($handler) {
  343. case 'login':
  344. require_once("$base_dir/login.php");
  345. break;
  346. case 'forgotpassword':
  347. require_once("$base_dir/forgotten_password.php");
  348. break;
  349. case 'changepassword':
  350. require_once("$base_dir/change_password.php");
  351. break;
  352. case 'register':
  353. require_once("$base_dir/register.php");
  354. break;
  355. default:
  356. return false;
  357. }
  358. return true;
  359. }
  360. /**
  361. * Sets the last action time of the given user to right now.
  362. *
  363. * @param int $user_guid The user GUID
  364. *
  365. * @return void
  366. */
  367. function set_last_action($user_guid) {
  368. _elgg_services()->usersTable->setLastAction($user_guid);
  369. }
  370. /**
  371. * Sets the last logon time of the given user to right now.
  372. *
  373. * @param int $user_guid The user GUID
  374. *
  375. * @return void
  376. */
  377. function set_last_login($user_guid) {
  378. _elgg_services()->usersTable->setLastLogin($user_guid);
  379. }
  380. /**
  381. * Creates a relationship between this site and the user.
  382. *
  383. * @param string $event create
  384. * @param string $object_type user
  385. * @param \ElggUser $object User object
  386. *
  387. * @return void
  388. * @access private
  389. */
  390. function user_create_hook_add_site_relationship($event, $object_type, $object) {
  391. add_entity_relationship($object->getGUID(), 'member_of_site', elgg_get_site_entity()->guid);
  392. }
  393. /**
  394. * Serves the user's avatar
  395. *
  396. * @param string $hook
  397. * @param string $entity_type
  398. * @param string $returnvalue
  399. * @param array $params
  400. * @return string
  401. * @access private
  402. */
  403. function user_avatar_hook($hook, $entity_type, $returnvalue, $params) {
  404. $user = $params['entity'];
  405. $size = $params['size'];
  406. if (isset($user->icontime)) {
  407. return "avatar/view/$user->username/$size/$user->icontime";
  408. } else {
  409. return "_graphics/icons/user/default{$size}.gif";
  410. }
  411. }
  412. /**
  413. * Setup the default user hover menu
  414. * @access private
  415. */
  416. function elgg_user_hover_menu($hook, $type, $return, $params) {
  417. $user = elgg_extract('entity', $params);
  418. /* @var \ElggUser $user */
  419. if (!$user instanceof \ElggUser) {
  420. return;
  421. }
  422. if (!elgg_is_logged_in()) {
  423. return;
  424. }
  425. if (elgg_get_logged_in_user_guid() == $user->guid) {
  426. $url = "profile/$user->username/edit";
  427. $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
  428. $item->setSection('action');
  429. $return[] = $item;
  430. $url = "avatar/edit/$user->username";
  431. $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
  432. $item->setSection('action');
  433. $return[] = $item;
  434. }
  435. // prevent admins from banning or deleting themselves
  436. if (elgg_get_logged_in_user_guid() == $user->guid) {
  437. return $return;
  438. }
  439. if (elgg_is_admin_logged_in()) {
  440. $actions = array();
  441. if (!$user->isBanned()) {
  442. $actions[] = 'ban';
  443. } else {
  444. $actions[] = 'unban';
  445. }
  446. $actions[] = 'delete';
  447. $actions[] = 'resetpassword';
  448. if (!$user->isAdmin()) {
  449. $actions[] = 'makeadmin';
  450. } else {
  451. $actions[] = 'removeadmin';
  452. }
  453. foreach ($actions as $action) {
  454. $url = "action/admin/user/$action?guid={$user->guid}";
  455. $url = elgg_add_action_tokens_to_url($url);
  456. $item = new \ElggMenuItem($action, elgg_echo($action), $url);
  457. $item->setSection('admin');
  458. $item->setConfirmText(true);
  459. $return[] = $item;
  460. }
  461. $url = "profile/$user->username/edit";
  462. $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
  463. $item->setSection('admin');
  464. $return[] = $item;
  465. $url = "avatar/edit/$user->username";
  466. $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
  467. $item->setSection('admin');
  468. $return[] = $item;
  469. $url = "settings/user/$user->username";
  470. $item = new \ElggMenuItem('settings:edit', elgg_echo('settings:edit'), $url);
  471. $item->setSection('admin');
  472. $return[] = $item;
  473. $url = "activity/owner/$user->username";
  474. $item = new \ElggMenuItem('activity:owner', elgg_echo('activity:owner'), $url);
  475. $item->setSection('action');
  476. $return[] = $item;
  477. }
  478. return $return;
  479. }
  480. /**
  481. * Setup the menu shown with an entity
  482. *
  483. * @param string $hook
  484. * @param string $type
  485. * @param array $return
  486. * @param array $params
  487. * @return array
  488. *
  489. * @access private
  490. */
  491. function elgg_users_setup_entity_menu($hook, $type, $return, $params) {
  492. if (elgg_in_context('widgets')) {
  493. return $return;
  494. }
  495. $entity = $params['entity'];
  496. if (!elgg_instanceof($entity, 'user')) {
  497. return $return;
  498. }
  499. /* @var \ElggUser $entity */
  500. if ($entity->isBanned()) {
  501. $banned = elgg_echo('banned');
  502. $options = array(
  503. 'name' => 'banned',
  504. 'text' => "<span>$banned</span>",
  505. 'href' => false,
  506. 'priority' => 0,
  507. );
  508. $return = array(\ElggMenuItem::factory($options));
  509. } else {
  510. $return = array();
  511. $location = $entity->location;
  512. if (is_string($location) && $location !== '') {
  513. $location = htmlspecialchars($location, ENT_QUOTES, 'UTF-8', false);
  514. $options = array(
  515. 'name' => 'location',
  516. 'text' => "<span>$location</span>",
  517. 'href' => false,
  518. 'priority' => 150,
  519. );
  520. $return[] = \ElggMenuItem::factory($options);
  521. }
  522. }
  523. return $return;
  524. }
  525. /**
  526. * This function loads a set of default fields into the profile, then triggers a hook letting other plugins to edit
  527. * add and delete fields.
  528. *
  529. * Note: This is a secondary system:init call and is run at a super low priority to guarantee that it is called after all
  530. * other plugins have initialised.
  531. * @access private
  532. */
  533. function elgg_profile_fields_setup() {
  534. global $CONFIG;
  535. $profile_defaults = array (
  536. 'description' => 'longtext',
  537. 'briefdescription' => 'text',
  538. 'location' => 'location',
  539. 'interests' => 'tags',
  540. 'skills' => 'tags',
  541. 'contactemail' => 'email',
  542. 'phone' => 'text',
  543. 'mobile' => 'text',
  544. 'website' => 'url',
  545. 'twitter' => 'text',
  546. );
  547. $loaded_defaults = array();
  548. $fieldlist = elgg_get_config('profile_custom_fields');
  549. if ($fieldlist || $fieldlist === '0') {
  550. $fieldlistarray = explode(',', $fieldlist);
  551. foreach ($fieldlistarray as $listitem) {
  552. if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
  553. $type = elgg_get_config("admin_defined_profile_type_{$listitem}");
  554. $loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
  555. add_translation(get_current_language(), array("profile:admin_defined_profile_{$listitem}" => $translation));
  556. }
  557. }
  558. }
  559. if (count($loaded_defaults)) {
  560. $CONFIG->profile_using_custom = true;
  561. $profile_defaults = $loaded_defaults;
  562. }
  563. $CONFIG->profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
  564. // register any tag metadata names
  565. foreach ($CONFIG->profile_fields as $name => $type) {
  566. if ($type == 'tags' || $type == 'location' || $type == 'tag') {
  567. elgg_register_tag_metadata_name($name);
  568. // register a tag name translation
  569. add_translation(get_current_language(), array("tag_names:$name" => elgg_echo("profile:$name")));
  570. }
  571. }
  572. }
  573. /**
  574. * Avatar page handler
  575. *
  576. * /avatar/edit/<username>
  577. * /avatar/view/<username>/<size>/<icontime>
  578. *
  579. * @param array $page
  580. * @return bool
  581. * @access private
  582. */
  583. function elgg_avatar_page_handler($page) {
  584. global $CONFIG;
  585. $user = get_user_by_username($page[1]);
  586. if ($user) {
  587. elgg_set_page_owner_guid($user->getGUID());
  588. }
  589. if ($page[0] == 'edit') {
  590. require_once("{$CONFIG->path}pages/avatar/edit.php");
  591. return true;
  592. } else {
  593. set_input('size', $page[2]);
  594. require_once("{$CONFIG->path}pages/avatar/view.php");
  595. return true;
  596. }
  597. return false;
  598. }
  599. /**
  600. * Profile page handler
  601. *
  602. * @param array $page
  603. * @return bool
  604. * @access private
  605. */
  606. function elgg_profile_page_handler($page) {
  607. global $CONFIG;
  608. $user = get_user_by_username($page[0]);
  609. elgg_set_page_owner_guid($user->guid);
  610. if ($page[1] == 'edit') {
  611. require_once("{$CONFIG->path}pages/profile/edit.php");
  612. return true;
  613. }
  614. return false;
  615. }
  616. /**
  617. * Sets up user-related menu items
  618. *
  619. * @return void
  620. * @access private
  621. */
  622. function users_pagesetup() {
  623. $owner = elgg_get_page_owner_entity();
  624. $viewer = elgg_get_logged_in_user_entity();
  625. if ($owner) {
  626. elgg_register_menu_item('page', array(
  627. 'name' => 'edit_avatar',
  628. 'href' => "avatar/edit/{$owner->username}",
  629. 'text' => elgg_echo('avatar:edit'),
  630. 'section' => '1_profile',
  631. 'contexts' => array('settings'),
  632. ));
  633. elgg_register_menu_item('page', array(
  634. 'name' => 'edit_profile',
  635. 'href' => "profile/{$owner->username}/edit",
  636. 'text' => elgg_echo('profile:edit'),
  637. 'section' => '1_profile',
  638. 'contexts' => array('settings'),
  639. ));
  640. }
  641. // topbar
  642. if ($viewer) {
  643. elgg_register_menu_item('topbar', array(
  644. 'name' => 'usersettings',
  645. 'href' => "settings/user/{$viewer->username}",
  646. 'text' => elgg_view_icon('settings') . elgg_echo('settings'),
  647. 'priority' => 500,
  648. 'section' => 'alt',
  649. ));
  650. elgg_register_menu_item('topbar', array(
  651. 'name' => 'logout',
  652. 'href' => "action/logout",
  653. 'text' => elgg_echo('logout'),
  654. 'is_action' => true,
  655. 'priority' => 1000,
  656. 'section' => 'alt',
  657. ));
  658. }
  659. }
  660. /**
  661. * Users initialisation function, which establishes the page handler
  662. *
  663. * @return void
  664. * @access private
  665. */
  666. function users_init() {
  667. elgg_register_page_handler('register', 'elgg_user_account_page_handler');
  668. elgg_register_page_handler('forgotpassword', 'elgg_user_account_page_handler');
  669. elgg_register_page_handler('changepassword', 'elgg_user_account_page_handler');
  670. elgg_register_page_handler('login', 'elgg_user_account_page_handler');
  671. elgg_register_page_handler('avatar', 'elgg_avatar_page_handler');
  672. elgg_register_page_handler('profile', 'elgg_profile_page_handler');
  673. elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu');
  674. elgg_register_action('register', '', 'public');
  675. elgg_register_action('useradd', '', 'admin');
  676. elgg_register_action('avatar/upload');
  677. elgg_register_action('avatar/crop');
  678. elgg_register_action('avatar/remove');
  679. elgg_register_action('profile/edit');
  680. elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
  681. elgg_register_action('user/changepassword', '', 'public');
  682. elgg_register_action('user/requestnewpassword', '', 'public');
  683. // Register the user type
  684. elgg_register_entity_type('user', '');
  685. elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_users_setup_entity_menu', 501);
  686. elgg_register_event_handler('create', 'user', 'user_create_hook_add_site_relationship');
  687. }
  688. /**
  689. * Runs unit tests for \ElggUser
  690. *
  691. * @param string $hook unit_test
  692. * @param string $type system
  693. * @param mixed $value Array of tests
  694. * @param mixed $params Params
  695. *
  696. * @return array
  697. * @access private
  698. */
  699. function users_test($hook, $type, $value, $params) {
  700. global $CONFIG;
  701. $value[] = "{$CONFIG->path}engine/tests/ElggUserTest.php";
  702. return $value;
  703. }
  704. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  705. $events->registerHandler('init', 'system', 'users_init', 0);
  706. $events->registerHandler('init', 'system', 'elgg_profile_fields_setup', 10000); // Ensure this runs after other plugins
  707. $events->registerHandler('pagesetup', 'system', 'users_pagesetup', 0);
  708. $hooks->registerHandler('unit_test', 'system', 'users_test');
  709. };