user.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This class provides wrapper methods for user management. Multiple backends are
  24. * supported. User management operations are delegated to the configured backend for
  25. * execution.
  26. *
  27. * Hooks provided:
  28. * pre_createUser(&run, uid, password)
  29. * post_createUser(uid, password)
  30. * pre_deleteUser(&run, uid)
  31. * post_deleteUser(uid)
  32. * pre_setPassword(&run, uid, password, recoveryPassword)
  33. * post_setPassword(uid, password, recoveryPassword)
  34. * pre_login(&run, uid, password)
  35. * post_login(uid)
  36. * logout()
  37. */
  38. class OC_User {
  39. public static $userSession = null;
  40. private static function getUserSession() {
  41. if (!self::$userSession) {
  42. $manager = new \OC\User\Manager();
  43. self::$userSession = new \OC\User\Session($manager, \OC::$session);
  44. self::$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
  45. \OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
  46. });
  47. self::$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
  48. /** @var $user \OC\User\User */
  49. \OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
  50. });
  51. self::$userSession->listen('\OC\User', 'preDelete', function ($user) {
  52. /** @var $user \OC\User\User */
  53. \OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
  54. });
  55. self::$userSession->listen('\OC\User', 'postDelete', function ($user) {
  56. /** @var $user \OC\User\User */
  57. \OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
  58. });
  59. self::$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
  60. /** @var $user \OC\User\User */
  61. OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  62. });
  63. self::$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
  64. /** @var $user \OC\User\User */
  65. OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  66. });
  67. self::$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
  68. \OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
  69. });
  70. self::$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
  71. /** @var $user \OC\User\User */
  72. \OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
  73. });
  74. self::$userSession->listen('\OC\User', 'logout', function () {
  75. \OC_Hook::emit('OC_User', 'logout', array());
  76. });
  77. }
  78. return self::$userSession;
  79. }
  80. /**
  81. * @return \OC\User\Manager
  82. */
  83. private static function getManager() {
  84. return self::getUserSession()->getManager();
  85. }
  86. private static $_backends = array();
  87. private static $_usedBackends = array();
  88. private static $_setupedBackends = array();
  89. /**
  90. * @brief registers backend
  91. * @param string $backend name of the backend
  92. * @deprecated Add classes by calling useBackend with a class instance instead
  93. * @return bool
  94. *
  95. * Makes a list of backends that can be used by other modules
  96. */
  97. public static function registerBackend($backend) {
  98. self::$_backends[] = $backend;
  99. return true;
  100. }
  101. /**
  102. * @brief gets available backends
  103. * @deprecated
  104. * @returns array of backends
  105. *
  106. * Returns the names of all backends.
  107. */
  108. public static function getBackends() {
  109. return self::$_backends;
  110. }
  111. /**
  112. * @brief gets used backends
  113. * @deprecated
  114. * @returns array of backends
  115. *
  116. * Returns the names of all used backends.
  117. */
  118. public static function getUsedBackends() {
  119. return array_keys(self::$_usedBackends);
  120. }
  121. /**
  122. * @brief Adds the backend to the list of used backends
  123. * @param string | OC_User_Backend $backend default: database The backend to use for user management
  124. * @return bool
  125. *
  126. * Set the User Authentication Module
  127. */
  128. public static function useBackend($backend = 'database') {
  129. if ($backend instanceof OC_User_Interface) {
  130. OC_Log::write('core', 'Adding user backend instance of ' . get_class($backend) . '.', OC_Log::DEBUG);
  131. self::$_usedBackends[get_class($backend)] = $backend;
  132. self::getManager()->registerBackend($backend);
  133. } else {
  134. // You'll never know what happens
  135. if (null === $backend OR !is_string($backend)) {
  136. $backend = 'database';
  137. }
  138. // Load backend
  139. switch ($backend) {
  140. case 'database':
  141. case 'mysql':
  142. case 'sqlite':
  143. OC_Log::write('core', 'Adding user backend ' . $backend . '.', OC_Log::DEBUG);
  144. self::$_usedBackends[$backend] = new OC_User_Database();
  145. self::getManager()->registerBackend(self::$_usedBackends[$backend]);
  146. break;
  147. default:
  148. OC_Log::write('core', 'Adding default user backend ' . $backend . '.', OC_Log::DEBUG);
  149. $className = 'OC_USER_' . strToUpper($backend);
  150. self::$_usedBackends[$backend] = new $className();
  151. self::getManager()->registerBackend(self::$_usedBackends[$backend]);
  152. break;
  153. }
  154. }
  155. return true;
  156. }
  157. /**
  158. * remove all used backends
  159. */
  160. public static function clearBackends() {
  161. self::$_usedBackends = array();
  162. self::getManager()->clearBackends();
  163. }
  164. /**
  165. * setup the configured backends in config.php
  166. */
  167. public static function setupBackends() {
  168. $backends = OC_Config::getValue('user_backends', array());
  169. foreach ($backends as $i => $config) {
  170. $class = $config['class'];
  171. $arguments = $config['arguments'];
  172. if (class_exists($class)) {
  173. if (array_search($i, self::$_setupedBackends) === false) {
  174. // make a reflection object
  175. $reflectionObj = new ReflectionClass($class);
  176. // use Reflection to create a new instance, using the $args
  177. $backend = $reflectionObj->newInstanceArgs($arguments);
  178. self::useBackend($backend);
  179. $_setupedBackends[] = $i;
  180. } else {
  181. OC_Log::write('core', 'User backend ' . $class . ' already initialized.', OC_Log::DEBUG);
  182. }
  183. } else {
  184. OC_Log::write('core', 'User backend ' . $class . ' not found.', OC_Log::ERROR);
  185. }
  186. }
  187. }
  188. /**
  189. * @brief Create a new user
  190. * @param string $uid The username of the user to create
  191. * @param string $password The password of the new user
  192. * @throws Exception
  193. * @return bool true/false
  194. *
  195. * Creates a new user. Basic checking of username is done in OC_User
  196. * itself, not in its subclasses.
  197. *
  198. * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
  199. */
  200. public static function createUser($uid, $password) {
  201. self::getManager()->createUser($uid, $password);
  202. }
  203. /**
  204. * @brief delete a user
  205. * @param string $uid The username of the user to delete
  206. * @return bool
  207. *
  208. * Deletes a user
  209. */
  210. public static function deleteUser($uid) {
  211. $user = self::getManager()->get($uid);
  212. if ($user) {
  213. $user->delete();
  214. // We have to delete the user from all groups
  215. foreach (OC_Group::getUserGroups($uid) as $i) {
  216. OC_Group::removeFromGroup($uid, $i);
  217. }
  218. // Delete the user's keys in preferences
  219. OC_Preferences::deleteUser($uid);
  220. // Delete user files in /data/
  221. OC_Helper::rmdirr(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid . '/');
  222. }
  223. }
  224. /**
  225. * @brief Try to login a user
  226. * @param $uid The username of the user to log in
  227. * @param $password The password of the user
  228. * @return bool
  229. *
  230. * Log in a user and regenerate a new session - if the password is ok
  231. */
  232. public static function login($uid, $password) {
  233. return self::getUserSession()->login($uid, $password);
  234. }
  235. /**
  236. * @brief Sets user id for session and triggers emit
  237. */
  238. public static function setUserId($uid) {
  239. OC::$session->set('user_id', $uid);
  240. }
  241. /**
  242. * @brief Sets user display name for session
  243. */
  244. public static function setDisplayName($uid, $displayName = null) {
  245. if (is_null($displayName)) {
  246. $displayName = $uid;
  247. }
  248. $user = self::getManager()->get($uid);
  249. if ($user) {
  250. return $user->setDisplayName($displayName);
  251. } else {
  252. return false;
  253. }
  254. }
  255. /**
  256. * @brief Logs the current user out and kills all the session data
  257. *
  258. * Logout, destroys session
  259. */
  260. public static function logout() {
  261. self::getUserSession()->logout();
  262. }
  263. /**
  264. * @brief Check if the user is logged in
  265. * @returns bool
  266. *
  267. * Checks if the user is logged in
  268. */
  269. public static function isLoggedIn() {
  270. if (\OC::$session->get('user_id')) {
  271. OC_App::loadApps(array('authentication'));
  272. self::setupBackends();
  273. return self::userExists(\OC::$session->get('user_id'));
  274. }
  275. return false;
  276. }
  277. /**
  278. * @brief Check if the user is an admin user
  279. * @param string $uid uid of the admin
  280. * @return bool
  281. */
  282. public static function isAdminUser($uid) {
  283. if (OC_Group::inGroup($uid, 'admin')) {
  284. return true;
  285. }
  286. return false;
  287. }
  288. /**
  289. * @brief get the user id of the user currently logged in.
  290. * @return string uid or false
  291. */
  292. public static function getUser() {
  293. $uid = OC::$session->get('user_id');
  294. if (!is_null($uid)) {
  295. return $uid;
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * @brief get the display name of the user currently logged in.
  302. * @param string $uid
  303. * @return string uid or false
  304. */
  305. public static function getDisplayName($uid = null) {
  306. if ($uid) {
  307. $user = self::getManager()->get($uid);
  308. if ($user) {
  309. return $user->getDisplayName();
  310. } else {
  311. return $uid;
  312. }
  313. } else {
  314. $user = self::getUserSession()->getUser();
  315. if ($user) {
  316. return $user->getDisplayName();
  317. } else {
  318. return false;
  319. }
  320. }
  321. }
  322. /**
  323. * @brief Autogenerate a password
  324. * @return string
  325. *
  326. * generates a password
  327. */
  328. public static function generatePassword() {
  329. return OC_Util::generate_random_bytes(30);
  330. }
  331. /**
  332. * @brief Set password
  333. * @param string $uid The username
  334. * @param string $password The new password
  335. * @param string $recoveryPassword for the encryption app to reset encryption keys
  336. * @return bool
  337. *
  338. * Change the password of a user
  339. */
  340. public static function setPassword($uid, $password, $recoveryPassword = null) {
  341. $user = self::getManager()->get($uid);
  342. if ($user) {
  343. return $user->setPassword($password, $recoveryPassword);
  344. } else {
  345. return false;
  346. }
  347. }
  348. /**
  349. * @brief Check whether user can change his password
  350. * @param string $uid The username
  351. * @return bool
  352. *
  353. * Check whether a specified user can change his password
  354. */
  355. public static function canUserChangePassword($uid) {
  356. $user = self::getManager()->get($uid);
  357. if ($user) {
  358. return $user->canChangePassword();
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * @brief Check whether user can change his display name
  365. * @param string $uid The username
  366. * @return bool
  367. *
  368. * Check whether a specified user can change his display name
  369. */
  370. public static function canUserChangeDisplayName($uid) {
  371. $user = self::getManager()->get($uid);
  372. if ($user) {
  373. return $user->canChangeDisplayName();
  374. } else {
  375. return false;
  376. }
  377. }
  378. /**
  379. * @brief Check if the password is correct
  380. * @param string $uid The username
  381. * @param string $password The password
  382. * @return bool
  383. *
  384. * Check if the password is correct without logging in the user
  385. * returns the user id or false
  386. */
  387. public static function checkPassword($uid, $password) {
  388. $user = self::getManager()->get($uid);
  389. if ($user) {
  390. if ($user->checkPassword($password)) {
  391. return $user->getUID();
  392. } else {
  393. return false;
  394. }
  395. } else {
  396. return false;
  397. }
  398. }
  399. /**
  400. * @param string $uid The username
  401. * @return string
  402. *
  403. * returns the path to the users home directory
  404. */
  405. public static function getHome($uid) {
  406. $user = self::getManager()->get($uid);
  407. if ($user) {
  408. return $user->getHome();
  409. } else {
  410. return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
  411. }
  412. }
  413. /**
  414. * @brief Get a list of all users
  415. * @returns array with all uids
  416. *
  417. * Get a list of all users.
  418. */
  419. public static function getUsers($search = '', $limit = null, $offset = null) {
  420. $users = self::getManager()->search($search, $limit, $offset);
  421. $uids = array();
  422. foreach ($users as $user) {
  423. $uids[] = $user->getUID();
  424. }
  425. return $uids;
  426. }
  427. /**
  428. * @brief Get a list of all users display name
  429. * @param string $search
  430. * @param int $limit
  431. * @param int $offset
  432. * @return array associative array with all display names (value) and corresponding uids (key)
  433. *
  434. * Get a list of all display names and user ids.
  435. */
  436. public static function getDisplayNames($search = '', $limit = null, $offset = null) {
  437. $displayNames = array();
  438. $users = self::getManager()->searchDisplayName($search, $limit, $offset);
  439. foreach ($users as $user) {
  440. $displayNames[$user->getUID()] = $user->getDisplayName();
  441. }
  442. return $displayNames;
  443. }
  444. /**
  445. * @brief check if a user exists
  446. * @param string $uid the username
  447. * @return boolean
  448. */
  449. public static function userExists($uid) {
  450. return self::getManager()->userExists($uid);
  451. }
  452. /**
  453. * disables a user
  454. *
  455. * @param string $uid the user to disable
  456. */
  457. public static function disableUser($uid) {
  458. $user = self::getManager()->get($uid);
  459. if ($user) {
  460. $user->setEnabled(false);
  461. }
  462. }
  463. /**
  464. * enable a user
  465. *
  466. * @param string $uid
  467. */
  468. public static function enableUser($uid) {
  469. $user = self::getManager()->get($uid);
  470. if ($user) {
  471. $user->setEnabled(true);
  472. }
  473. }
  474. /**
  475. * checks if a user is enabled
  476. *
  477. * @param string $uid
  478. * @return bool
  479. */
  480. public static function isEnabled($uid) {
  481. $user = self::getManager()->get($uid);
  482. if ($user) {
  483. return $user->isEnabled();
  484. } else {
  485. return false;
  486. }
  487. }
  488. /**
  489. * @brief Set cookie value to use in next page load
  490. * @param string $username username to be set
  491. * @param string $token
  492. */
  493. public static function setMagicInCookie($username, $token) {
  494. self::getUserSession()->setMagicInCookie($username, $token);
  495. }
  496. /**
  497. * @brief Remove cookie for "remember username"
  498. */
  499. public static function unsetMagicInCookie() {
  500. self::getUserSession()->unsetMagicInCookie();
  501. }
  502. }