user.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. public 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. public 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. self::$_usedBackends[get_class($backend)] = $backend;
  131. self::getManager()->registerBackend($backend);
  132. } else {
  133. // You'll never know what happens
  134. if (null === $backend OR !is_string($backend)) {
  135. $backend = 'database';
  136. }
  137. // Load backend
  138. switch ($backend) {
  139. case 'database':
  140. case 'mysql':
  141. case 'sqlite':
  142. OC_Log::write('core', 'Adding user backend ' . $backend . '.', OC_Log::DEBUG);
  143. self::$_usedBackends[$backend] = new OC_User_Database();
  144. self::getManager()->registerBackend(self::$_usedBackends[$backend]);
  145. break;
  146. default:
  147. OC_Log::write('core', 'Adding default user backend ' . $backend . '.', OC_Log::DEBUG);
  148. $className = 'OC_USER_' . strToUpper($backend);
  149. self::$_usedBackends[$backend] = new $className();
  150. self::getManager()->registerBackend(self::$_usedBackends[$backend]);
  151. break;
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * remove all used backends
  158. */
  159. public static function clearBackends() {
  160. self::$_usedBackends = array();
  161. self::getManager()->clearBackends();
  162. }
  163. /**
  164. * setup the configured backends in config.php
  165. */
  166. public static function setupBackends() {
  167. $backends = OC_Config::getValue('user_backends', array());
  168. foreach ($backends as $i => $config) {
  169. $class = $config['class'];
  170. $arguments = $config['arguments'];
  171. if (class_exists($class)) {
  172. if (array_search($i, self::$_setupedBackends) === false) {
  173. // make a reflection object
  174. $reflectionObj = new ReflectionClass($class);
  175. // use Reflection to create a new instance, using the $args
  176. $backend = $reflectionObj->newInstanceArgs($arguments);
  177. self::useBackend($backend);
  178. self::$_setupedBackends[] = $i;
  179. } else {
  180. OC_Log::write('core', 'User backend ' . $class . ' already initialized.', OC_Log::DEBUG);
  181. }
  182. } else {
  183. OC_Log::write('core', 'User backend ' . $class . ' not found.', OC_Log::ERROR);
  184. }
  185. }
  186. }
  187. /**
  188. * @brief Create a new user
  189. * @param string $uid The username of the user to create
  190. * @param string $password The password of the new user
  191. * @throws Exception
  192. * @return bool true/false
  193. *
  194. * Creates a new user. Basic checking of username is done in OC_User
  195. * itself, not in its subclasses.
  196. *
  197. * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
  198. */
  199. public static function createUser($uid, $password) {
  200. return self::getManager()->createUser($uid, $password);
  201. }
  202. /**
  203. * @brief delete a user
  204. * @param string $uid The username of the user to delete
  205. * @return bool
  206. *
  207. * Deletes a user
  208. */
  209. public static function deleteUser($uid) {
  210. $user = self::getManager()->get($uid);
  211. if ($user) {
  212. $user->delete();
  213. // We have to delete the user from all groups
  214. foreach (OC_Group::getUserGroups($uid) as $i) {
  215. OC_Group::removeFromGroup($uid, $i);
  216. }
  217. // Delete the user's keys in preferences
  218. OC_Preferences::deleteUser($uid);
  219. // Delete user files in /data/
  220. OC_Helper::rmdirr(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid . '/');
  221. }
  222. }
  223. /**
  224. * @brief Try to login a user
  225. * @param $uid The username of the user to log in
  226. * @param $password The password of the user
  227. * @return bool
  228. *
  229. * Log in a user and regenerate a new session - if the password is ok
  230. */
  231. public static function login($uid, $password) {
  232. return self::getUserSession()->login($uid, $password);
  233. }
  234. /**
  235. * @brief Sets user id for session and triggers emit
  236. */
  237. public static function setUserId($uid) {
  238. OC::$session->set('user_id', $uid);
  239. }
  240. /**
  241. * @brief Sets user display name for session
  242. */
  243. public static function setDisplayName($uid, $displayName = null) {
  244. if (is_null($displayName)) {
  245. $displayName = $uid;
  246. }
  247. $user = self::getManager()->get($uid);
  248. if ($user) {
  249. return $user->setDisplayName($displayName);
  250. } else {
  251. return false;
  252. }
  253. }
  254. /**
  255. * @brief Logs the current user out and kills all the session data
  256. *
  257. * Logout, destroys session
  258. */
  259. public static function logout() {
  260. self::getUserSession()->logout();
  261. }
  262. /**
  263. * @brief Check if the user is logged in
  264. * @returns bool
  265. *
  266. * Checks if the user is logged in
  267. */
  268. public static function isLoggedIn() {
  269. if (\OC::$session->get('user_id')) {
  270. OC_App::loadApps(array('authentication'));
  271. self::setupBackends();
  272. return self::userExists(\OC::$session->get('user_id'));
  273. }
  274. return false;
  275. }
  276. /**
  277. * @brief Check if the user is an admin user
  278. * @param string $uid uid of the admin
  279. * @return bool
  280. */
  281. public static function isAdminUser($uid) {
  282. if (OC_Group::inGroup($uid, 'admin')) {
  283. return true;
  284. }
  285. return false;
  286. }
  287. /**
  288. * @brief get the user id of the user currently logged in.
  289. * @return string uid or false
  290. */
  291. public static function getUser() {
  292. $uid = OC::$session ? OC::$session->get('user_id') : null;
  293. if (!is_null($uid)) {
  294. return $uid;
  295. } else {
  296. return false;
  297. }
  298. }
  299. /**
  300. * @brief get the display name of the user currently logged in.
  301. * @param string $uid
  302. * @return string uid or false
  303. */
  304. public static function getDisplayName($uid = null) {
  305. if ($uid) {
  306. $user = self::getManager()->get($uid);
  307. if ($user) {
  308. return $user->getDisplayName();
  309. } else {
  310. return $uid;
  311. }
  312. } else {
  313. $user = self::getUserSession()->getUser();
  314. if ($user) {
  315. return $user->getDisplayName();
  316. } else {
  317. return false;
  318. }
  319. }
  320. }
  321. /**
  322. * @brief Autogenerate a password
  323. * @return string
  324. *
  325. * generates a password
  326. */
  327. public static function generatePassword() {
  328. return OC_Util::generateRandomBytes(30);
  329. }
  330. /**
  331. * @brief Set password
  332. * @param string $uid The username
  333. * @param string $password The new password
  334. * @param string $recoveryPassword for the encryption app to reset encryption keys
  335. * @return bool
  336. *
  337. * Change the password of a user
  338. */
  339. public static function setPassword($uid, $password, $recoveryPassword = null) {
  340. $user = self::getManager()->get($uid);
  341. if ($user) {
  342. return $user->setPassword($password, $recoveryPassword);
  343. } else {
  344. return false;
  345. }
  346. }
  347. /**
  348. * @brief Check whether user can change his password
  349. * @param string $uid The username
  350. * @return bool
  351. *
  352. * Check whether a specified user can change his password
  353. */
  354. public static function canUserChangePassword($uid) {
  355. $user = self::getManager()->get($uid);
  356. if ($user) {
  357. return $user->canChangePassword();
  358. } else {
  359. return false;
  360. }
  361. }
  362. /**
  363. * @brief Check whether user can change his display name
  364. * @param string $uid The username
  365. * @return bool
  366. *
  367. * Check whether a specified user can change his display name
  368. */
  369. public static function canUserChangeDisplayName($uid) {
  370. $user = self::getManager()->get($uid);
  371. if ($user) {
  372. return $user->canChangeDisplayName();
  373. } else {
  374. return false;
  375. }
  376. }
  377. /**
  378. * @brief Check if the password is correct
  379. * @param string $uid The username
  380. * @param string $password The password
  381. * @return bool
  382. *
  383. * Check if the password is correct without logging in the user
  384. * returns the user id or false
  385. */
  386. public static function checkPassword($uid, $password) {
  387. $user = self::getManager()->get($uid);
  388. if ($user) {
  389. if ($user->checkPassword($password)) {
  390. return $user->getUID();
  391. } else {
  392. return false;
  393. }
  394. } else {
  395. return false;
  396. }
  397. }
  398. /**
  399. * @param string $uid The username
  400. * @return string
  401. *
  402. * returns the path to the users home directory
  403. */
  404. public static function getHome($uid) {
  405. $user = self::getManager()->get($uid);
  406. if ($user) {
  407. return $user->getHome();
  408. } else {
  409. return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
  410. }
  411. }
  412. /**
  413. * @brief Get a list of all users
  414. * @returns array with all uids
  415. *
  416. * Get a list of all users.
  417. */
  418. public static function getUsers($search = '', $limit = null, $offset = null) {
  419. $users = self::getManager()->search($search, $limit, $offset);
  420. $uids = array();
  421. foreach ($users as $user) {
  422. $uids[] = $user->getUID();
  423. }
  424. return $uids;
  425. }
  426. /**
  427. * @brief Get a list of all users display name
  428. * @param string $search
  429. * @param int $limit
  430. * @param int $offset
  431. * @return array associative array with all display names (value) and corresponding uids (key)
  432. *
  433. * Get a list of all display names and user ids.
  434. */
  435. public static function getDisplayNames($search = '', $limit = null, $offset = null) {
  436. $displayNames = array();
  437. $users = self::getManager()->searchDisplayName($search, $limit, $offset);
  438. foreach ($users as $user) {
  439. $displayNames[$user->getUID()] = $user->getDisplayName();
  440. }
  441. return $displayNames;
  442. }
  443. /**
  444. * @brief check if a user exists
  445. * @param string $uid the username
  446. * @return boolean
  447. */
  448. public static function userExists($uid) {
  449. return self::getManager()->userExists($uid);
  450. }
  451. /**
  452. * disables a user
  453. *
  454. * @param string $uid the user to disable
  455. */
  456. public static function disableUser($uid) {
  457. $user = self::getManager()->get($uid);
  458. if ($user) {
  459. $user->setEnabled(false);
  460. }
  461. }
  462. /**
  463. * enable a user
  464. *
  465. * @param string $uid
  466. */
  467. public static function enableUser($uid) {
  468. $user = self::getManager()->get($uid);
  469. if ($user) {
  470. $user->setEnabled(true);
  471. }
  472. }
  473. /**
  474. * checks if a user is enabled
  475. *
  476. * @param string $uid
  477. * @return bool
  478. */
  479. public static function isEnabled($uid) {
  480. $user = self::getManager()->get($uid);
  481. if ($user) {
  482. return $user->isEnabled();
  483. } else {
  484. return false;
  485. }
  486. }
  487. /**
  488. * @brief Set cookie value to use in next page load
  489. * @param string $username username to be set
  490. * @param string $token
  491. */
  492. public static function setMagicInCookie($username, $token) {
  493. self::getUserSession()->setMagicInCookie($username, $token);
  494. }
  495. /**
  496. * @brief Remove cookie for "remember username"
  497. */
  498. public static function unsetMagicInCookie() {
  499. self::getUserSession()->unsetMagicInCookie();
  500. }
  501. }