User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\User;
  32. use OC\Accounts\AccountManager;
  33. use OC\Files\Cache\Storage;
  34. use OC\Hooks\Emitter;
  35. use OC_Helper;
  36. use OCP\IAvatarManager;
  37. use OCP\IImage;
  38. use OCP\IURLGenerator;
  39. use OCP\IUser;
  40. use OCP\IConfig;
  41. use OCP\UserInterface;
  42. use \OCP\IUserBackend;
  43. class User implements IUser {
  44. /** @var string $uid */
  45. private $uid;
  46. /** @var string $displayName */
  47. private $displayName;
  48. /** @var UserInterface $backend */
  49. private $backend;
  50. /** @var bool $enabled */
  51. private $enabled;
  52. /** @var Emitter|Manager $emitter */
  53. private $emitter;
  54. /** @var string $home */
  55. private $home;
  56. /** @var int $lastLogin */
  57. private $lastLogin;
  58. /** @var \OCP\IConfig $config */
  59. private $config;
  60. /** @var IAvatarManager */
  61. private $avatarManager;
  62. /** @var IURLGenerator */
  63. private $urlGenerator;
  64. /**
  65. * @param string $uid
  66. * @param UserInterface $backend
  67. * @param \OC\Hooks\Emitter $emitter
  68. * @param IConfig|null $config
  69. * @param IURLGenerator $urlGenerator
  70. */
  71. public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) {
  72. $this->uid = $uid;
  73. $this->backend = $backend;
  74. $this->emitter = $emitter;
  75. if(is_null($config)) {
  76. $config = \OC::$server->getConfig();
  77. }
  78. $this->config = $config;
  79. $this->urlGenerator = $urlGenerator;
  80. $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
  81. $this->enabled = ($enabled === 'true');
  82. $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0);
  83. if (is_null($this->urlGenerator)) {
  84. $this->urlGenerator = \OC::$server->getURLGenerator();
  85. }
  86. }
  87. /**
  88. * get the user id
  89. *
  90. * @return string
  91. */
  92. public function getUID() {
  93. return $this->uid;
  94. }
  95. /**
  96. * get the display name for the user, if no specific display name is set it will fallback to the user id
  97. *
  98. * @return string
  99. */
  100. public function getDisplayName() {
  101. if (!isset($this->displayName)) {
  102. $displayName = '';
  103. if ($this->backend and $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
  104. // get display name and strip whitespace from the beginning and end of it
  105. $backendDisplayName = $this->backend->getDisplayName($this->uid);
  106. if (is_string($backendDisplayName)) {
  107. $displayName = trim($backendDisplayName);
  108. }
  109. }
  110. if (!empty($displayName)) {
  111. $this->displayName = $displayName;
  112. } else {
  113. $this->displayName = $this->uid;
  114. }
  115. }
  116. return $this->displayName;
  117. }
  118. /**
  119. * set the displayname for the user
  120. *
  121. * @param string $displayName
  122. * @return bool
  123. */
  124. public function setDisplayName($displayName) {
  125. $displayName = trim($displayName);
  126. if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName)) {
  127. $result = $this->backend->setDisplayName($this->uid, $displayName);
  128. if ($result) {
  129. $this->displayName = $displayName;
  130. $this->triggerChange('displayName', $displayName);
  131. }
  132. return $result !== false;
  133. } else {
  134. return false;
  135. }
  136. }
  137. /**
  138. * set the email address of the user
  139. *
  140. * @param string|null $mailAddress
  141. * @return void
  142. * @since 9.0.0
  143. */
  144. public function setEMailAddress($mailAddress) {
  145. $oldMailAddress = $this->getEMailAddress();
  146. if($mailAddress === '') {
  147. $this->config->deleteUserValue($this->uid, 'settings', 'email');
  148. } else {
  149. $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress);
  150. }
  151. if($oldMailAddress !== $mailAddress) {
  152. $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress);
  153. }
  154. }
  155. /**
  156. * returns the timestamp of the user's last login or 0 if the user did never
  157. * login
  158. *
  159. * @return int
  160. */
  161. public function getLastLogin() {
  162. return $this->lastLogin;
  163. }
  164. /**
  165. * updates the timestamp of the most recent login of this user
  166. */
  167. public function updateLastLoginTimestamp() {
  168. $firstTimeLogin = ($this->lastLogin === 0);
  169. $this->lastLogin = time();
  170. $this->config->setUserValue(
  171. $this->uid, 'login', 'lastLogin', $this->lastLogin);
  172. return $firstTimeLogin;
  173. }
  174. /**
  175. * Delete the user
  176. *
  177. * @return bool
  178. */
  179. public function delete() {
  180. if ($this->emitter) {
  181. $this->emitter->emit('\OC\User', 'preDelete', array($this));
  182. }
  183. // get the home now because it won't return it after user deletion
  184. $homePath = $this->getHome();
  185. $result = $this->backend->deleteUser($this->uid);
  186. if ($result) {
  187. // FIXME: Feels like an hack - suggestions?
  188. $groupManager = \OC::$server->getGroupManager();
  189. // We have to delete the user from all groups
  190. foreach ($groupManager->getUserGroupIds($this) as $groupId) {
  191. $group = $groupManager->get($groupId);
  192. if ($group) {
  193. \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]);
  194. $group->removeUser($this);
  195. \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]);
  196. }
  197. }
  198. // Delete the user's keys in preferences
  199. \OC::$server->getConfig()->deleteAllUserValues($this->uid);
  200. // Delete user files in /data/
  201. if ($homePath !== false) {
  202. // FIXME: this operates directly on FS, should use View instead...
  203. // also this is not testable/mockable...
  204. \OC_Helper::rmdirr($homePath);
  205. }
  206. // Delete the users entry in the storage table
  207. Storage::remove('home::' . $this->uid);
  208. \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid);
  209. \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
  210. $notification = \OC::$server->getNotificationManager()->createNotification();
  211. $notification->setUser($this->uid);
  212. \OC::$server->getNotificationManager()->markProcessed($notification);
  213. /** @var AccountManager $accountManager */
  214. $accountManager = \OC::$server->query(AccountManager::class);
  215. $accountManager->deleteUser($this);
  216. if ($this->emitter) {
  217. $this->emitter->emit('\OC\User', 'postDelete', array($this));
  218. }
  219. }
  220. return !($result === false);
  221. }
  222. /**
  223. * Set the password of the user
  224. *
  225. * @param string $password
  226. * @param string $recoveryPassword for the encryption app to reset encryption keys
  227. * @return bool
  228. */
  229. public function setPassword($password, $recoveryPassword = null) {
  230. if ($this->emitter) {
  231. $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
  232. }
  233. if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
  234. $result = $this->backend->setPassword($this->uid, $password);
  235. if ($this->emitter) {
  236. $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
  237. }
  238. return !($result === false);
  239. } else {
  240. return false;
  241. }
  242. }
  243. /**
  244. * get the users home folder to mount
  245. *
  246. * @return string
  247. */
  248. public function getHome() {
  249. if (!$this->home) {
  250. if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  251. $this->home = $home;
  252. } elseif ($this->config) {
  253. $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid;
  254. } else {
  255. $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
  256. }
  257. }
  258. return $this->home;
  259. }
  260. /**
  261. * Get the name of the backend class the user is connected with
  262. *
  263. * @return string
  264. */
  265. public function getBackendClassName() {
  266. if($this->backend instanceof IUserBackend) {
  267. return $this->backend->getBackendName();
  268. }
  269. return get_class($this->backend);
  270. }
  271. public function getBackend() {
  272. return $this->backend;
  273. }
  274. /**
  275. * check if the backend allows the user to change his avatar on Personal page
  276. *
  277. * @return bool
  278. */
  279. public function canChangeAvatar() {
  280. if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) {
  281. return $this->backend->canChangeAvatar($this->uid);
  282. }
  283. return true;
  284. }
  285. /**
  286. * check if the backend supports changing passwords
  287. *
  288. * @return bool
  289. */
  290. public function canChangePassword() {
  291. return $this->backend->implementsActions(Backend::SET_PASSWORD);
  292. }
  293. /**
  294. * check if the backend supports changing display names
  295. *
  296. * @return bool
  297. */
  298. public function canChangeDisplayName() {
  299. if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) {
  300. return false;
  301. }
  302. return $this->backend->implementsActions(Backend::SET_DISPLAYNAME);
  303. }
  304. /**
  305. * check if the user is enabled
  306. *
  307. * @return bool
  308. */
  309. public function isEnabled() {
  310. return $this->enabled;
  311. }
  312. /**
  313. * set the enabled status for the user
  314. *
  315. * @param bool $enabled
  316. */
  317. public function setEnabled(bool $enabled = true) {
  318. $oldStatus = $this->isEnabled();
  319. $this->enabled = $enabled;
  320. if ($oldStatus !== $this->enabled) {
  321. $this->triggerChange('enabled', $enabled);
  322. $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false');
  323. }
  324. }
  325. /**
  326. * get the users email address
  327. *
  328. * @return string|null
  329. * @since 9.0.0
  330. */
  331. public function getEMailAddress() {
  332. return $this->config->getUserValue($this->uid, 'settings', 'email', null);
  333. }
  334. /**
  335. * get the users' quota
  336. *
  337. * @return string
  338. * @since 9.0.0
  339. */
  340. public function getQuota() {
  341. $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default');
  342. if($quota === 'default') {
  343. $quota = $this->config->getAppValue('files', 'default_quota', 'none');
  344. }
  345. return $quota;
  346. }
  347. /**
  348. * set the users' quota
  349. *
  350. * @param string $quota
  351. * @return void
  352. * @since 9.0.0
  353. */
  354. public function setQuota($quota) {
  355. $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
  356. if($quota !== 'none' and $quota !== 'default') {
  357. $quota = OC_Helper::computerFileSize($quota);
  358. $quota = OC_Helper::humanFileSize($quota);
  359. }
  360. $this->config->setUserValue($this->uid, 'files', 'quota', $quota);
  361. if($quota !== $oldQuota) {
  362. $this->triggerChange('quota', $quota);
  363. }
  364. }
  365. /**
  366. * get the avatar image if it exists
  367. *
  368. * @param int $size
  369. * @return IImage|null
  370. * @since 9.0.0
  371. */
  372. public function getAvatarImage($size) {
  373. // delay the initialization
  374. if (is_null($this->avatarManager)) {
  375. $this->avatarManager = \OC::$server->getAvatarManager();
  376. }
  377. $avatar = $this->avatarManager->getAvatar($this->uid);
  378. $image = $avatar->get(-1);
  379. if ($image) {
  380. return $image;
  381. }
  382. return null;
  383. }
  384. /**
  385. * get the federation cloud id
  386. *
  387. * @return string
  388. * @since 9.0.0
  389. */
  390. public function getCloudId() {
  391. $uid = $this->getUID();
  392. $server = $this->urlGenerator->getAbsoluteURL('/');
  393. $server = rtrim( $this->removeProtocolFromUrl($server), '/');
  394. return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId();
  395. }
  396. /**
  397. * @param string $url
  398. * @return string
  399. */
  400. private function removeProtocolFromUrl($url) {
  401. if (strpos($url, 'https://') === 0) {
  402. return substr($url, strlen('https://'));
  403. } else if (strpos($url, 'http://') === 0) {
  404. return substr($url, strlen('http://'));
  405. }
  406. return $url;
  407. }
  408. public function triggerChange($feature, $value = null, $oldValue = null) {
  409. if ($this->emitter) {
  410. $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue));
  411. }
  412. }
  413. }