user.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\User;
  30. use OC\Hooks\Emitter;
  31. use OCP\IUser;
  32. use OCP\IConfig;
  33. class User implements IUser {
  34. /**
  35. * @var string $uid
  36. */
  37. private $uid;
  38. /**
  39. * @var string $displayName
  40. */
  41. private $displayName;
  42. /**
  43. * @var \OC_User_Interface $backend
  44. */
  45. private $backend;
  46. /**
  47. * @var bool $enabled
  48. */
  49. private $enabled;
  50. /**
  51. * @var Emitter|Manager $emitter
  52. */
  53. private $emitter;
  54. /**
  55. * @var string $home
  56. */
  57. private $home;
  58. /**
  59. * @var int $lastLogin
  60. */
  61. private $lastLogin;
  62. /**
  63. * @var \OCP\IConfig $config
  64. */
  65. private $config;
  66. /**
  67. * @param string $uid
  68. * @param \OC_User_Interface $backend
  69. * @param \OC\Hooks\Emitter $emitter
  70. * @param \OCP\IConfig $config
  71. */
  72. public function __construct($uid, $backend, $emitter = null, IConfig $config = null) {
  73. $this->uid = $uid;
  74. $this->backend = $backend;
  75. $this->emitter = $emitter;
  76. $this->config = $config;
  77. if ($this->config) {
  78. $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
  79. $this->enabled = ($enabled === 'true');
  80. $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0);
  81. } else {
  82. $this->enabled = true;
  83. $this->lastLogin = \OC::$server->getConfig()->getUserValue($uid, 'login', 'lastLogin', 0);
  84. }
  85. }
  86. /**
  87. * get the user id
  88. *
  89. * @return string
  90. */
  91. public function getUID() {
  92. return $this->uid;
  93. }
  94. /**
  95. * get the displayname for the user, if no specific displayname is set it will fallback to the user id
  96. *
  97. * @return string
  98. */
  99. public function getDisplayName() {
  100. if (!isset($this->displayName)) {
  101. $displayName = '';
  102. if ($this->backend and $this->backend->implementsActions(\OC_User_Backend::GET_DISPLAYNAME)) {
  103. // get display name and strip whitespace from the beginning and end of it
  104. $backendDisplayName = $this->backend->getDisplayName($this->uid);
  105. if (is_string($backendDisplayName)) {
  106. $displayName = trim($backendDisplayName);
  107. }
  108. }
  109. if (!empty($displayName)) {
  110. $this->displayName = $displayName;
  111. } else {
  112. $this->displayName = $this->uid;
  113. }
  114. }
  115. return $this->displayName;
  116. }
  117. /**
  118. * set the displayname for the user
  119. *
  120. * @param string $displayName
  121. * @return bool
  122. */
  123. public function setDisplayName($displayName) {
  124. $displayName = trim($displayName);
  125. if ($this->backend->implementsActions(\OC_User_Backend::SET_DISPLAYNAME) && !empty($displayName)) {
  126. $this->displayName = $displayName;
  127. $result = $this->backend->setDisplayName($this->uid, $displayName);
  128. return $result !== false;
  129. } else {
  130. return false;
  131. }
  132. }
  133. /**
  134. * returns the timestamp of the user's last login or 0 if the user did never
  135. * login
  136. *
  137. * @return int
  138. */
  139. public function getLastLogin() {
  140. return $this->lastLogin;
  141. }
  142. /**
  143. * updates the timestamp of the most recent login of this user
  144. */
  145. public function updateLastLoginTimestamp() {
  146. $this->lastLogin = time();
  147. \OC::$server->getConfig()->setUserValue(
  148. $this->uid, 'login', 'lastLogin', $this->lastLogin);
  149. }
  150. /**
  151. * Delete the user
  152. *
  153. * @return bool
  154. */
  155. public function delete() {
  156. if ($this->emitter) {
  157. $this->emitter->emit('\OC\User', 'preDelete', array($this));
  158. }
  159. $result = $this->backend->deleteUser($this->uid);
  160. if ($result) {
  161. // FIXME: Feels like an hack - suggestions?
  162. // We have to delete the user from all groups
  163. foreach (\OC_Group::getUserGroups($this->uid) as $i) {
  164. \OC_Group::removeFromGroup($this->uid, $i);
  165. }
  166. // Delete the user's keys in preferences
  167. \OC::$server->getConfig()->deleteAllUserValues($this->uid);
  168. // Delete user files in /data/
  169. \OC_Helper::rmdirr(\OC_User::getHome($this->uid));
  170. // Delete the users entry in the storage table
  171. \OC\Files\Cache\Storage::remove('home::' . $this->uid);
  172. }
  173. if ($this->emitter) {
  174. $this->emitter->emit('\OC\User', 'postDelete', array($this));
  175. }
  176. return !($result === false);
  177. }
  178. /**
  179. * Set the password of the user
  180. *
  181. * @param string $password
  182. * @param string $recoveryPassword for the encryption app to reset encryption keys
  183. * @return bool
  184. */
  185. public function setPassword($password, $recoveryPassword = null) {
  186. if ($this->emitter) {
  187. $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
  188. }
  189. if ($this->backend->implementsActions(\OC_User_Backend::SET_PASSWORD)) {
  190. $result = $this->backend->setPassword($this->uid, $password);
  191. if ($this->emitter) {
  192. $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
  193. }
  194. return !($result === false);
  195. } else {
  196. return false;
  197. }
  198. }
  199. /**
  200. * get the users home folder to mount
  201. *
  202. * @return string
  203. */
  204. public function getHome() {
  205. if (!$this->home) {
  206. if ($this->backend->implementsActions(\OC_User_Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  207. $this->home = $home;
  208. } elseif ($this->config) {
  209. $this->home = $this->config->getSystemValue('datadirectory') . '/' . $this->uid;
  210. } else {
  211. $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
  212. }
  213. }
  214. return $this->home;
  215. }
  216. /**
  217. * Get the name of the backend class the user is connected with
  218. *
  219. * @return string
  220. */
  221. public function getBackendClassName() {
  222. if($this->backend instanceof \OCP\IUserBackend) {
  223. return $this->backend->getBackendName();
  224. }
  225. return get_class($this->backend);
  226. }
  227. /**
  228. * check if the backend allows the user to change his avatar on Personal page
  229. *
  230. * @return bool
  231. */
  232. public function canChangeAvatar() {
  233. if ($this->backend->implementsActions(\OC_User_Backend::PROVIDE_AVATAR)) {
  234. return $this->backend->canChangeAvatar($this->uid);
  235. }
  236. return true;
  237. }
  238. /**
  239. * check if the backend supports changing passwords
  240. *
  241. * @return bool
  242. */
  243. public function canChangePassword() {
  244. return $this->backend->implementsActions(\OC_User_Backend::SET_PASSWORD);
  245. }
  246. /**
  247. * check if the backend supports changing display names
  248. *
  249. * @return bool
  250. */
  251. public function canChangeDisplayName() {
  252. if ($this->config and $this->config->getSystemValue('allow_user_to_change_display_name') === false) {
  253. return false;
  254. } else {
  255. return $this->backend->implementsActions(\OC_User_Backend::SET_DISPLAYNAME);
  256. }
  257. }
  258. /**
  259. * check if the user is enabled
  260. *
  261. * @return bool
  262. */
  263. public function isEnabled() {
  264. return $this->enabled;
  265. }
  266. /**
  267. * set the enabled status for the user
  268. *
  269. * @param bool $enabled
  270. */
  271. public function setEnabled($enabled) {
  272. $this->enabled = $enabled;
  273. if ($this->config) {
  274. $enabled = ($enabled) ? 'true' : 'false';
  275. $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled);
  276. }
  277. }
  278. }