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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  44. use Symfony\Component\EventDispatcher\GenericEvent;
  45. class User implements IUser {
  46. /** @var string */
  47. private $uid;
  48. /** @var string */
  49. private $displayName;
  50. /** @var UserInterface|null */
  51. private $backend;
  52. /** @var EventDispatcherInterface */
  53. private $dispatcher;
  54. /** @var bool */
  55. private $enabled;
  56. /** @var Emitter|Manager */
  57. private $emitter;
  58. /** @var string */
  59. private $home;
  60. /** @var int */
  61. private $lastLogin;
  62. /** @var \OCP\IConfig */
  63. private $config;
  64. /** @var IAvatarManager */
  65. private $avatarManager;
  66. /** @var IURLGenerator */
  67. private $urlGenerator;
  68. public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) {
  69. $this->uid = $uid;
  70. $this->backend = $backend;
  71. $this->dispatcher = $dispatcher;
  72. $this->emitter = $emitter;
  73. if(is_null($config)) {
  74. $config = \OC::$server->getConfig();
  75. }
  76. $this->config = $config;
  77. $this->urlGenerator = $urlGenerator;
  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. if (is_null($this->urlGenerator)) {
  82. $this->urlGenerator = \OC::$server->getURLGenerator();
  83. }
  84. }
  85. /**
  86. * get the user id
  87. *
  88. * @return string
  89. */
  90. public function getUID() {
  91. return $this->uid;
  92. }
  93. /**
  94. * get the display name for the user, if no specific display name is set it will fallback to the user id
  95. *
  96. * @return string
  97. */
  98. public function getDisplayName() {
  99. if (!isset($this->displayName)) {
  100. $displayName = '';
  101. if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
  102. // get display name and strip whitespace from the beginning and end of it
  103. $backendDisplayName = $this->backend->getDisplayName($this->uid);
  104. if (is_string($backendDisplayName)) {
  105. $displayName = trim($backendDisplayName);
  106. }
  107. }
  108. if (!empty($displayName)) {
  109. $this->displayName = $displayName;
  110. } else {
  111. $this->displayName = $this->uid;
  112. }
  113. }
  114. return $this->displayName;
  115. }
  116. /**
  117. * set the displayname for the user
  118. *
  119. * @param string $displayName
  120. * @return bool
  121. */
  122. public function setDisplayName($displayName) {
  123. $displayName = trim($displayName);
  124. $oldDisplayName = $this->getDisplayName();
  125. if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) {
  126. $result = $this->backend->setDisplayName($this->uid, $displayName);
  127. if ($result) {
  128. $this->displayName = $displayName;
  129. $this->triggerChange('displayName', $displayName, $oldDisplayName);
  130. }
  131. return $result !== false;
  132. }
  133. return false;
  134. }
  135. /**
  136. * set the email address of the user
  137. *
  138. * @param string|null $mailAddress
  139. * @return void
  140. * @since 9.0.0
  141. */
  142. public function setEMailAddress($mailAddress) {
  143. $oldMailAddress = $this->getEMailAddress();
  144. if($oldMailAddress !== $mailAddress) {
  145. if($mailAddress === '') {
  146. $this->config->deleteUserValue($this->uid, 'settings', 'email');
  147. } else {
  148. $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress);
  149. }
  150. $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress);
  151. }
  152. }
  153. /**
  154. * returns the timestamp of the user's last login or 0 if the user did never
  155. * login
  156. *
  157. * @return int
  158. */
  159. public function getLastLogin() {
  160. return $this->lastLogin;
  161. }
  162. /**
  163. * updates the timestamp of the most recent login of this user
  164. */
  165. public function updateLastLoginTimestamp() {
  166. $firstTimeLogin = ($this->lastLogin === 0);
  167. $this->lastLogin = time();
  168. $this->config->setUserValue(
  169. $this->uid, 'login', 'lastLogin', $this->lastLogin);
  170. return $firstTimeLogin;
  171. }
  172. /**
  173. * Delete the user
  174. *
  175. * @return bool
  176. */
  177. public function delete() {
  178. $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this));
  179. if ($this->emitter) {
  180. $this->emitter->emit('\OC\User', 'preDelete', array($this));
  181. }
  182. // get the home now because it won't return it after user deletion
  183. $homePath = $this->getHome();
  184. $result = $this->backend->deleteUser($this->uid);
  185. if ($result) {
  186. // FIXME: Feels like an hack - suggestions?
  187. $groupManager = \OC::$server->getGroupManager();
  188. // We have to delete the user from all groups
  189. foreach ($groupManager->getUserGroupIds($this) as $groupId) {
  190. $group = $groupManager->get($groupId);
  191. if ($group) {
  192. \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]);
  193. $group->removeUser($this);
  194. \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]);
  195. }
  196. }
  197. // Delete the user's keys in preferences
  198. \OC::$server->getConfig()->deleteAllUserValues($this->uid);
  199. // Delete user files in /data/
  200. if ($homePath !== false) {
  201. // FIXME: this operates directly on FS, should use View instead...
  202. // also this is not testable/mockable...
  203. \OC_Helper::rmdirr($homePath);
  204. }
  205. // Delete the users entry in the storage table
  206. Storage::remove('home::' . $this->uid);
  207. \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid);
  208. \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
  209. $notification = \OC::$server->getNotificationManager()->createNotification();
  210. $notification->setUser($this->uid);
  211. \OC::$server->getNotificationManager()->markProcessed($notification);
  212. /** @var AccountManager $accountManager */
  213. $accountManager = \OC::$server->query(AccountManager::class);
  214. $accountManager->deleteUser($this);
  215. $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($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. $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [
  231. 'password' => $password,
  232. 'recoveryPassword' => $recoveryPassword,
  233. ]));
  234. if ($this->emitter) {
  235. $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
  236. }
  237. if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
  238. $result = $this->backend->setPassword($this->uid, $password);
  239. $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [
  240. 'password' => $password,
  241. 'recoveryPassword' => $recoveryPassword,
  242. ]));
  243. if ($this->emitter) {
  244. $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
  245. }
  246. return !($result === false);
  247. } else {
  248. return false;
  249. }
  250. }
  251. /**
  252. * get the users home folder to mount
  253. *
  254. * @return string
  255. */
  256. public function getHome() {
  257. if (!$this->home) {
  258. if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  259. $this->home = $home;
  260. } elseif ($this->config) {
  261. $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid;
  262. } else {
  263. $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
  264. }
  265. }
  266. return $this->home;
  267. }
  268. /**
  269. * Get the name of the backend class the user is connected with
  270. *
  271. * @return string
  272. */
  273. public function getBackendClassName() {
  274. if($this->backend instanceof IUserBackend) {
  275. return $this->backend->getBackendName();
  276. }
  277. return get_class($this->backend);
  278. }
  279. public function getBackend() {
  280. return $this->backend;
  281. }
  282. /**
  283. * check if the backend allows the user to change his avatar on Personal page
  284. *
  285. * @return bool
  286. */
  287. public function canChangeAvatar() {
  288. if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) {
  289. return $this->backend->canChangeAvatar($this->uid);
  290. }
  291. return true;
  292. }
  293. /**
  294. * check if the backend supports changing passwords
  295. *
  296. * @return bool
  297. */
  298. public function canChangePassword() {
  299. return $this->backend->implementsActions(Backend::SET_PASSWORD);
  300. }
  301. /**
  302. * check if the backend supports changing display names
  303. *
  304. * @return bool
  305. */
  306. public function canChangeDisplayName() {
  307. if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) {
  308. return false;
  309. }
  310. return $this->backend->implementsActions(Backend::SET_DISPLAYNAME);
  311. }
  312. /**
  313. * check if the user is enabled
  314. *
  315. * @return bool
  316. */
  317. public function isEnabled() {
  318. return $this->enabled;
  319. }
  320. /**
  321. * set the enabled status for the user
  322. *
  323. * @param bool $enabled
  324. */
  325. public function setEnabled(bool $enabled = true) {
  326. $oldStatus = $this->isEnabled();
  327. $this->enabled = $enabled;
  328. if ($oldStatus !== $this->enabled) {
  329. // TODO: First change the value, then trigger the event as done for all other properties.
  330. $this->triggerChange('enabled', $enabled, $oldStatus);
  331. $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false');
  332. }
  333. }
  334. /**
  335. * get the users email address
  336. *
  337. * @return string|null
  338. * @since 9.0.0
  339. */
  340. public function getEMailAddress() {
  341. return $this->config->getUserValue($this->uid, 'settings', 'email', null);
  342. }
  343. /**
  344. * get the users' quota
  345. *
  346. * @return string
  347. * @since 9.0.0
  348. */
  349. public function getQuota() {
  350. $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default');
  351. if($quota === 'default') {
  352. $quota = $this->config->getAppValue('files', 'default_quota', 'none');
  353. }
  354. return $quota;
  355. }
  356. /**
  357. * set the users' quota
  358. *
  359. * @param string $quota
  360. * @return void
  361. * @since 9.0.0
  362. */
  363. public function setQuota($quota) {
  364. $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
  365. if($quota !== 'none' and $quota !== 'default') {
  366. $quota = OC_Helper::computerFileSize($quota);
  367. $quota = OC_Helper::humanFileSize($quota);
  368. }
  369. if($quota !== $oldQuota) {
  370. $this->config->setUserValue($this->uid, 'files', 'quota', $quota);
  371. $this->triggerChange('quota', $quota, $oldQuota);
  372. }
  373. }
  374. /**
  375. * get the avatar image if it exists
  376. *
  377. * @param int $size
  378. * @return IImage|null
  379. * @since 9.0.0
  380. */
  381. public function getAvatarImage($size) {
  382. // delay the initialization
  383. if (is_null($this->avatarManager)) {
  384. $this->avatarManager = \OC::$server->getAvatarManager();
  385. }
  386. $avatar = $this->avatarManager->getAvatar($this->uid);
  387. $image = $avatar->get(-1);
  388. if ($image) {
  389. return $image;
  390. }
  391. return null;
  392. }
  393. /**
  394. * get the federation cloud id
  395. *
  396. * @return string
  397. * @since 9.0.0
  398. */
  399. public function getCloudId() {
  400. $uid = $this->getUID();
  401. $server = $this->urlGenerator->getAbsoluteURL('/');
  402. $server = rtrim( $this->removeProtocolFromUrl($server), '/');
  403. return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId();
  404. }
  405. /**
  406. * @param string $url
  407. * @return string
  408. */
  409. private function removeProtocolFromUrl($url) {
  410. if (strpos($url, 'https://') === 0) {
  411. return substr($url, strlen('https://'));
  412. } else if (strpos($url, 'http://') === 0) {
  413. return substr($url, strlen('http://'));
  414. }
  415. return $url;
  416. }
  417. public function triggerChange($feature, $value = null, $oldValue = null) {
  418. $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [
  419. 'feature' => $feature,
  420. 'value' => $value,
  421. 'oldValue' => $oldValue,
  422. ]));
  423. if ($this->emitter) {
  424. $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue));
  425. }
  426. }
  427. }