You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\User;
  9. use OC\Hooks\Emitter;
  10. use OCP\IUser;
  11. use OCP\IConfig;
  12. class User implements IUser {
  13. /**
  14. * @var string $uid
  15. */
  16. private $uid;
  17. /**
  18. * @var string $displayName
  19. */
  20. private $displayName;
  21. /**
  22. * @var \OC_User_Interface $backend
  23. */
  24. private $backend;
  25. /**
  26. * @var bool $enabled
  27. */
  28. private $enabled;
  29. /**
  30. * @var Emitter|Manager $emitter
  31. */
  32. private $emitter;
  33. /**
  34. * @var string $home
  35. */
  36. private $home;
  37. /**
  38. * @var int $lastLogin
  39. */
  40. private $lastLogin;
  41. /**
  42. * @var \OCP\IConfig $config
  43. */
  44. private $config;
  45. /**
  46. * @param string $uid
  47. * @param \OC_User_Interface $backend
  48. * @param \OC\Hooks\Emitter $emitter
  49. * @param \OCP\IConfig $config
  50. */
  51. public function __construct($uid, $backend, $emitter = null, IConfig $config = null) {
  52. $this->uid = $uid;
  53. $this->backend = $backend;
  54. $this->emitter = $emitter;
  55. $this->config = $config;
  56. if ($this->config) {
  57. $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
  58. $this->enabled = ($enabled === 'true');
  59. $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0);
  60. } else {
  61. $this->enabled = true;
  62. $this->lastLogin = \OC::$server->getConfig()->getUserValue($uid, 'login', 'lastLogin', 0);
  63. }
  64. }
  65. /**
  66. * get the user id
  67. *
  68. * @return string
  69. */
  70. public function getUID() {
  71. return $this->uid;
  72. }
  73. /**
  74. * get the displayname for the user, if no specific displayname is set it will fallback to the user id
  75. *
  76. * @return string
  77. */
  78. public function getDisplayName() {
  79. if (!isset($this->displayName)) {
  80. $displayName = '';
  81. if ($this->backend and $this->backend->implementsActions(\OC_User_Backend::GET_DISPLAYNAME)) {
  82. // get display name and strip whitespace from the beginning and end of it
  83. $backendDisplayName = $this->backend->getDisplayName($this->uid);
  84. if (is_string($backendDisplayName)) {
  85. $displayName = trim($backendDisplayName);
  86. }
  87. }
  88. if (!empty($displayName)) {
  89. $this->displayName = $displayName;
  90. } else {
  91. $this->displayName = $this->uid;
  92. }
  93. }
  94. return $this->displayName;
  95. }
  96. /**
  97. * set the displayname for the user
  98. *
  99. * @param string $displayName
  100. * @return bool
  101. */
  102. public function setDisplayName($displayName) {
  103. $displayName = trim($displayName);
  104. if ($this->backend->implementsActions(\OC_User_Backend::SET_DISPLAYNAME) && !empty($displayName)) {
  105. $this->displayName = $displayName;
  106. $result = $this->backend->setDisplayName($this->uid, $displayName);
  107. return $result !== false;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * returns the timestamp of the user's last login or 0 if the user did never
  114. * login
  115. *
  116. * @return int
  117. */
  118. public function getLastLogin() {
  119. return $this->lastLogin;
  120. }
  121. /**
  122. * updates the timestamp of the most recent login of this user
  123. */
  124. public function updateLastLoginTimestamp() {
  125. $this->lastLogin = time();
  126. \OC::$server->getConfig()->setUserValue(
  127. $this->uid, 'login', 'lastLogin', $this->lastLogin);
  128. }
  129. /**
  130. * Delete the user
  131. *
  132. * @return bool
  133. */
  134. public function delete() {
  135. if ($this->emitter) {
  136. $this->emitter->emit('\OC\User', 'preDelete', array($this));
  137. }
  138. $result = $this->backend->deleteUser($this->uid);
  139. if ($result) {
  140. // FIXME: Feels like an hack - suggestions?
  141. // We have to delete the user from all groups
  142. foreach (\OC_Group::getUserGroups($this->uid) as $i) {
  143. \OC_Group::removeFromGroup($this->uid, $i);
  144. }
  145. // Delete the user's keys in preferences
  146. \OC::$server->getConfig()->deleteAllUserValues($this->uid);
  147. // Delete user files in /data/
  148. \OC_Helper::rmdirr(\OC_User::getHome($this->uid));
  149. // Delete the users entry in the storage table
  150. \OC\Files\Cache\Storage::remove('home::' . $this->uid);
  151. }
  152. if ($this->emitter) {
  153. $this->emitter->emit('\OC\User', 'postDelete', array($this));
  154. }
  155. return !($result === false);
  156. }
  157. /**
  158. * Set the password of the user
  159. *
  160. * @param string $password
  161. * @param string $recoveryPassword for the encryption app to reset encryption keys
  162. * @return bool
  163. */
  164. public function setPassword($password, $recoveryPassword = null) {
  165. if ($this->emitter) {
  166. $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
  167. }
  168. if ($this->backend->implementsActions(\OC_User_Backend::SET_PASSWORD)) {
  169. $result = $this->backend->setPassword($this->uid, $password);
  170. if ($this->emitter) {
  171. $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
  172. }
  173. return !($result === false);
  174. } else {
  175. return false;
  176. }
  177. }
  178. /**
  179. * get the users home folder to mount
  180. *
  181. * @return string
  182. */
  183. public function getHome() {
  184. if (!$this->home) {
  185. if ($this->backend->implementsActions(\OC_User_Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  186. $this->home = $home;
  187. } elseif ($this->config) {
  188. $this->home = $this->config->getSystemValue('datadirectory') . '/' . $this->uid;
  189. } else {
  190. $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
  191. }
  192. }
  193. return $this->home;
  194. }
  195. /**
  196. * check if the backend allows the user to change his avatar on Personal page
  197. *
  198. * @return bool
  199. */
  200. public function canChangeAvatar() {
  201. if ($this->backend->implementsActions(\OC_User_Backend::PROVIDE_AVATAR)) {
  202. return $this->backend->canChangeAvatar($this->uid);
  203. }
  204. return true;
  205. }
  206. /**
  207. * check if the backend supports changing passwords
  208. *
  209. * @return bool
  210. */
  211. public function canChangePassword() {
  212. return $this->backend->implementsActions(\OC_User_Backend::SET_PASSWORD);
  213. }
  214. /**
  215. * check if the backend supports changing display names
  216. *
  217. * @return bool
  218. */
  219. public function canChangeDisplayName() {
  220. if ($this->config and $this->config->getSystemValue('allow_user_to_change_display_name') === false) {
  221. return false;
  222. } else {
  223. return $this->backend->implementsActions(\OC_User_Backend::SET_DISPLAYNAME);
  224. }
  225. }
  226. /**
  227. * check if the user is enabled
  228. *
  229. * @return bool
  230. */
  231. public function isEnabled() {
  232. return $this->enabled;
  233. }
  234. /**
  235. * set the enabled status for the user
  236. *
  237. * @param bool $enabled
  238. */
  239. public function setEnabled($enabled) {
  240. $this->enabled = $enabled;
  241. if ($this->config) {
  242. $enabled = ($enabled) ? 'true' : 'false';
  243. $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled);
  244. }
  245. }
  246. }