database.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * @author adrien <adrien.waksberg@believedigital.com>
  4. * @author Aldo "xoen" Giambelluca <xoen@xoen.org>
  5. * @author Arthur Schiwon <blizzz@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <schiessle@owncloud.com>
  8. * @author fabian <fabian@web2.0-apps.de>
  9. * @author Georg Ehrke <georg@owncloud.com>
  10. * @author Jakob Sack <mail@jakobsack.de>
  11. * @author Joas Schilling <nickvergessen@owncloud.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author Lukas Reschke <lukas@owncloud.com>
  14. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author nishiki <nishiki@yaegashi.fr>
  17. * @author Robin Appelman <icewind@owncloud.com>
  18. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  21. *
  22. * @copyright Copyright (c) 2015, ownCloud, Inc.
  23. * @license AGPL-3.0
  24. *
  25. * This code is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU Affero General Public License, version 3,
  27. * as published by the Free Software Foundation.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Affero General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Affero General Public License, version 3,
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>
  36. *
  37. */
  38. /*
  39. *
  40. * The following SQL statement is just a help for developers and will not be
  41. * executed!
  42. *
  43. * CREATE TABLE `users` (
  44. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  45. * `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  46. * PRIMARY KEY (`uid`)
  47. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  48. *
  49. */
  50. /**
  51. * Class for user management in a SQL Database (e.g. MySQL, SQLite)
  52. */
  53. class OC_User_Database extends OC_User_Backend implements \OCP\IUserBackend {
  54. private $cache = array();
  55. /**
  56. * Create a new user
  57. * @param string $uid The username of the user to create
  58. * @param string $password The password of the new user
  59. * @return bool
  60. *
  61. * Creates a new user. Basic checking of username is done in OC_User
  62. * itself, not in its subclasses.
  63. */
  64. public function createUser($uid, $password) {
  65. if (!$this->userExists($uid)) {
  66. $query = OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )');
  67. $result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password)));
  68. return $result ? true : false;
  69. }
  70. return false;
  71. }
  72. /**
  73. * delete a user
  74. * @param string $uid The username of the user to delete
  75. * @return bool
  76. *
  77. * Deletes a user
  78. */
  79. public function deleteUser($uid) {
  80. // Delete user-group-relation
  81. $query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?');
  82. $result = $query->execute(array($uid));
  83. if (isset($this->cache[$uid])) {
  84. unset($this->cache[$uid]);
  85. }
  86. return $result ? true : false;
  87. }
  88. /**
  89. * Set password
  90. * @param string $uid The username
  91. * @param string $password The new password
  92. * @return bool
  93. *
  94. * Change the password of a user
  95. */
  96. public function setPassword($uid, $password) {
  97. if ($this->userExists($uid)) {
  98. $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?');
  99. $result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid));
  100. return $result ? true : false;
  101. }
  102. return false;
  103. }
  104. /**
  105. * Set display name
  106. * @param string $uid The username
  107. * @param string $displayName The new display name
  108. * @return bool
  109. *
  110. * Change the display name of a user
  111. */
  112. public function setDisplayName($uid, $displayName) {
  113. if ($this->userExists($uid)) {
  114. $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)');
  115. $query->execute(array($displayName, $uid));
  116. $this->cache[$uid]['displayname'] = $displayName;
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * get display name of the user
  123. * @param string $uid user ID of the user
  124. * @return string display name
  125. */
  126. public function getDisplayName($uid) {
  127. $this->loadUser($uid);
  128. return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname'];
  129. }
  130. /**
  131. * Get a list of all display names and user ids.
  132. *
  133. * @param string $search
  134. * @param string|null $limit
  135. * @param string|null $offset
  136. * @return array an array of all displayNames (value) and the corresponding uids (key)
  137. */
  138. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  139. $parameters = [];
  140. $searchLike = '';
  141. if ($search !== '') {
  142. $parameters[] = '%' . $search . '%';
  143. $parameters[] = '%' . $search . '%';
  144. $searchLike = ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR '
  145. . 'LOWER(`uid`) LIKE LOWER(?)';
  146. }
  147. $displayNames = array();
  148. $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`'
  149. . $searchLike .' ORDER BY `uid` ASC', $limit, $offset);
  150. $result = $query->execute($parameters);
  151. while ($row = $result->fetchRow()) {
  152. $displayNames[$row['uid']] = $row['displayname'];
  153. }
  154. return $displayNames;
  155. }
  156. /**
  157. * Check if the password is correct
  158. * @param string $uid The username
  159. * @param string $password The password
  160. * @return string
  161. *
  162. * Check if the password is correct without logging in the user
  163. * returns the user id or false
  164. */
  165. public function checkPassword($uid, $password) {
  166. $query = OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
  167. $result = $query->execute(array($uid));
  168. $row = $result->fetchRow();
  169. if ($row) {
  170. $storedHash = $row['password'];
  171. $newHash = '';
  172. if(\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) {
  173. if(!empty($newHash)) {
  174. $this->setPassword($uid, $password);
  175. }
  176. return $row['uid'];
  177. }
  178. }
  179. return false;
  180. }
  181. /**
  182. * Load an user in the cache
  183. * @param string $uid the username
  184. * @return boolean
  185. */
  186. private function loadUser($uid) {
  187. if (empty($this->cache[$uid])) {
  188. $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
  189. $result = $query->execute(array($uid));
  190. if (OC_DB::isError($result)) {
  191. \OCP\Util::writeLog('core', OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  192. return false;
  193. }
  194. while ($row = $result->fetchRow()) {
  195. $this->cache[$uid]['uid'] = $row['uid'];
  196. $this->cache[$uid]['displayname'] = $row['displayname'];
  197. }
  198. }
  199. return true;
  200. }
  201. /**
  202. * Get a list of all users
  203. *
  204. * @param string $search
  205. * @param null|int $limit
  206. * @param null|int $offset
  207. * @return string[] an array of all uids
  208. */
  209. public function getUsers($search = '', $limit = null, $offset = null) {
  210. $parameters = [];
  211. $searchLike = '';
  212. if ($search !== '') {
  213. $parameters[] = '%' . $search . '%';
  214. $searchLike = ' WHERE LOWER(`uid`) LIKE LOWER(?)';
  215. }
  216. $query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users`' . $searchLike . ' ORDER BY `uid` ASC', $limit, $offset);
  217. $result = $query->execute($parameters);
  218. $users = array();
  219. while ($row = $result->fetchRow()) {
  220. $users[] = $row['uid'];
  221. }
  222. return $users;
  223. }
  224. /**
  225. * check if a user exists
  226. * @param string $uid the username
  227. * @return boolean
  228. */
  229. public function userExists($uid) {
  230. $this->loadUser($uid);
  231. return !empty($this->cache[$uid]);
  232. }
  233. /**
  234. * get the user's home directory
  235. * @param string $uid the username
  236. * @return string|false
  237. */
  238. public function getHome($uid) {
  239. if ($this->userExists($uid)) {
  240. return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid;
  241. }
  242. return false;
  243. }
  244. /**
  245. * @return bool
  246. */
  247. public function hasUserListings() {
  248. return true;
  249. }
  250. /**
  251. * counts the users in the database
  252. *
  253. * @return int|bool
  254. */
  255. public function countUsers() {
  256. $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`');
  257. $result = $query->execute();
  258. if (OC_DB::isError($result)) {
  259. \OCP\Util::writeLog('core', OC_DB::getErrorMessage(), \OCP\Util::ERROR);
  260. return false;
  261. }
  262. return $result->fetchOne();
  263. }
  264. /**
  265. * Backend name to be shown in user management
  266. * @return string the name of the backend to be shown
  267. */
  268. public function getBackendName(){
  269. return 'Database';
  270. }
  271. }