User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Leon Klingele <leon@struktur.de>
  14. * @author Lukas Reschke <lukas@statuscode.ch>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC\User;
  36. use InvalidArgumentException;
  37. use OC\Accounts\AccountManager;
  38. use OC\Avatar\AvatarManager;
  39. use OC\Hooks\Emitter;
  40. use OC_Helper;
  41. use OCP\Accounts\IAccountManager;
  42. use OCP\EventDispatcher\IEventDispatcher;
  43. use OCP\Group\Events\BeforeUserRemovedEvent;
  44. use OCP\Group\Events\UserRemovedEvent;
  45. use OCP\IAvatarManager;
  46. use OCP\IConfig;
  47. use OCP\IImage;
  48. use OCP\IURLGenerator;
  49. use OCP\IUser;
  50. use OCP\IUserBackend;
  51. use OCP\User\Events\BeforeUserDeletedEvent;
  52. use OCP\User\Events\UserDeletedEvent;
  53. use OCP\User\GetQuotaEvent;
  54. use OCP\UserInterface;
  55. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  56. use Symfony\Component\EventDispatcher\GenericEvent;
  57. class User implements IUser {
  58. /** @var IAccountManager */
  59. protected $accountManager;
  60. /** @var string */
  61. private $uid;
  62. /** @var string|null */
  63. private $displayName;
  64. /** @var UserInterface|null */
  65. private $backend;
  66. /** @var EventDispatcherInterface */
  67. private $legacyDispatcher;
  68. /** @var IEventDispatcher */
  69. private $dispatcher;
  70. /** @var bool */
  71. private $enabled;
  72. /** @var Emitter|Manager */
  73. private $emitter;
  74. /** @var string */
  75. private $home;
  76. /** @var int */
  77. private $lastLogin;
  78. /** @var \OCP\IConfig */
  79. private $config;
  80. /** @var IAvatarManager */
  81. private $avatarManager;
  82. /** @var IURLGenerator */
  83. private $urlGenerator;
  84. public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) {
  85. $this->uid = $uid;
  86. $this->backend = $backend;
  87. $this->legacyDispatcher = $dispatcher;
  88. $this->emitter = $emitter;
  89. if (is_null($config)) {
  90. $config = \OC::$server->getConfig();
  91. }
  92. $this->config = $config;
  93. $this->urlGenerator = $urlGenerator;
  94. $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
  95. $this->enabled = ($enabled === 'true');
  96. $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0);
  97. if (is_null($this->urlGenerator)) {
  98. $this->urlGenerator = \OC::$server->getURLGenerator();
  99. }
  100. // TODO: inject
  101. $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
  102. }
  103. /**
  104. * get the user id
  105. *
  106. * @return string
  107. */
  108. public function getUID() {
  109. return $this->uid;
  110. }
  111. /**
  112. * get the display name for the user, if no specific display name is set it will fallback to the user id
  113. *
  114. * @return string
  115. */
  116. public function getDisplayName() {
  117. if ($this->displayName === null) {
  118. $displayName = '';
  119. if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
  120. // get display name and strip whitespace from the beginning and end of it
  121. $backendDisplayName = $this->backend->getDisplayName($this->uid);
  122. if (is_string($backendDisplayName)) {
  123. $displayName = trim($backendDisplayName);
  124. }
  125. }
  126. if (!empty($displayName)) {
  127. $this->displayName = $displayName;
  128. } else {
  129. $this->displayName = $this->uid;
  130. }
  131. }
  132. return $this->displayName;
  133. }
  134. /**
  135. * set the displayname for the user
  136. *
  137. * @param string $displayName
  138. * @return bool
  139. */
  140. public function setDisplayName($displayName) {
  141. $displayName = trim($displayName);
  142. $oldDisplayName = $this->getDisplayName();
  143. if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName) && $displayName !== $oldDisplayName) {
  144. $result = $this->backend->setDisplayName($this->uid, $displayName);
  145. if ($result) {
  146. $this->displayName = $displayName;
  147. $this->triggerChange('displayName', $displayName, $oldDisplayName);
  148. }
  149. return $result !== false;
  150. }
  151. return false;
  152. }
  153. /**
  154. * @inheritDoc
  155. */
  156. public function setEMailAddress($mailAddress) {
  157. $this->setSystemEMailAddress($mailAddress);
  158. }
  159. /**
  160. * @inheritDoc
  161. */
  162. public function setSystemEMailAddress(string $mailAddress): void {
  163. $oldMailAddress = $this->getSystemEMailAddress();
  164. if ($mailAddress === '') {
  165. $this->config->deleteUserValue($this->uid, 'settings', 'email');
  166. } else {
  167. $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress);
  168. }
  169. $primaryAddress = $this->getPrimaryEMailAddress();
  170. if ($primaryAddress === $mailAddress) {
  171. // on match no dedicated primary settings is necessary
  172. $this->setPrimaryEMailAddress('');
  173. }
  174. if ($oldMailAddress !== $mailAddress) {
  175. $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress);
  176. }
  177. }
  178. /**
  179. * @inheritDoc
  180. */
  181. public function setPrimaryEMailAddress(string $mailAddress): void {
  182. if ($mailAddress === '') {
  183. $this->config->deleteUserValue($this->uid, 'settings', 'primary_email');
  184. return;
  185. }
  186. $this->ensureAccountManager();
  187. $account = $this->accountManager->getAccount($this);
  188. $property = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)
  189. ->getPropertyByValue($mailAddress);
  190. if ($property === null || $property->getLocallyVerified() !== IAccountManager::VERIFIED) {
  191. throw new InvalidArgumentException('Only verified emails can be set as primary');
  192. }
  193. $this->config->setUserValue($this->uid, 'settings', 'primary_email', $mailAddress);
  194. }
  195. private function ensureAccountManager() {
  196. if (!$this->accountManager instanceof IAccountManager) {
  197. $this->accountManager = \OC::$server->get(IAccountManager::class);
  198. }
  199. }
  200. /**
  201. * returns the timestamp of the user's last login or 0 if the user did never
  202. * login
  203. *
  204. * @return int
  205. */
  206. public function getLastLogin() {
  207. return $this->lastLogin;
  208. }
  209. /**
  210. * updates the timestamp of the most recent login of this user
  211. */
  212. public function updateLastLoginTimestamp() {
  213. $firstTimeLogin = ($this->lastLogin === 0);
  214. $this->lastLogin = time();
  215. $this->config->setUserValue(
  216. $this->uid, 'login', 'lastLogin', $this->lastLogin);
  217. return $firstTimeLogin;
  218. }
  219. /**
  220. * Delete the user
  221. *
  222. * @return bool
  223. */
  224. public function delete() {
  225. /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
  226. $this->legacyDispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this));
  227. if ($this->emitter) {
  228. /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
  229. $this->emitter->emit('\OC\User', 'preDelete', [$this]);
  230. }
  231. $this->dispatcher->dispatchTyped(new BeforeUserDeletedEvent($this));
  232. $result = $this->backend->deleteUser($this->uid);
  233. if ($result) {
  234. // FIXME: Feels like an hack - suggestions?
  235. $groupManager = \OC::$server->getGroupManager();
  236. // We have to delete the user from all groups
  237. foreach ($groupManager->getUserGroupIds($this) as $groupId) {
  238. $group = $groupManager->get($groupId);
  239. if ($group) {
  240. $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this));
  241. $group->removeUser($this);
  242. $this->dispatcher->dispatchTyped(new UserRemovedEvent($group, $this));
  243. }
  244. }
  245. // Delete the user's keys in preferences
  246. \OC::$server->getConfig()->deleteAllUserValues($this->uid);
  247. \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid);
  248. \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
  249. /** @var IAvatarManager $avatarManager */
  250. $avatarManager = \OC::$server->query(AvatarManager::class);
  251. $avatarManager->deleteUserAvatar($this->uid);
  252. $notification = \OC::$server->getNotificationManager()->createNotification();
  253. $notification->setUser($this->uid);
  254. \OC::$server->getNotificationManager()->markProcessed($notification);
  255. /** @var AccountManager $accountManager */
  256. $accountManager = \OC::$server->query(AccountManager::class);
  257. $accountManager->deleteUser($this);
  258. /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
  259. $this->legacyDispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this));
  260. if ($this->emitter) {
  261. /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
  262. $this->emitter->emit('\OC\User', 'postDelete', [$this]);
  263. }
  264. $this->dispatcher->dispatchTyped(new UserDeletedEvent($this));
  265. }
  266. return !($result === false);
  267. }
  268. /**
  269. * Set the password of the user
  270. *
  271. * @param string $password
  272. * @param string $recoveryPassword for the encryption app to reset encryption keys
  273. * @return bool
  274. */
  275. public function setPassword($password, $recoveryPassword = null) {
  276. $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [
  277. 'password' => $password,
  278. 'recoveryPassword' => $recoveryPassword,
  279. ]));
  280. if ($this->emitter) {
  281. $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]);
  282. }
  283. if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
  284. $result = $this->backend->setPassword($this->uid, $password);
  285. $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [
  286. 'password' => $password,
  287. 'recoveryPassword' => $recoveryPassword,
  288. ]));
  289. if ($this->emitter) {
  290. $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]);
  291. }
  292. return !($result === false);
  293. } else {
  294. return false;
  295. }
  296. }
  297. /**
  298. * get the users home folder to mount
  299. *
  300. * @return string
  301. */
  302. public function getHome() {
  303. if (!$this->home) {
  304. if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  305. $this->home = $home;
  306. } elseif ($this->config) {
  307. $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid;
  308. } else {
  309. $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
  310. }
  311. }
  312. return $this->home;
  313. }
  314. /**
  315. * Get the name of the backend class the user is connected with
  316. *
  317. * @return string
  318. */
  319. public function getBackendClassName() {
  320. if ($this->backend instanceof IUserBackend) {
  321. return $this->backend->getBackendName();
  322. }
  323. return get_class($this->backend);
  324. }
  325. public function getBackend() {
  326. return $this->backend;
  327. }
  328. /**
  329. * check if the backend allows the user to change his avatar on Personal page
  330. *
  331. * @return bool
  332. */
  333. public function canChangeAvatar() {
  334. if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) {
  335. return $this->backend->canChangeAvatar($this->uid);
  336. }
  337. return true;
  338. }
  339. /**
  340. * check if the backend supports changing passwords
  341. *
  342. * @return bool
  343. */
  344. public function canChangePassword() {
  345. return $this->backend->implementsActions(Backend::SET_PASSWORD);
  346. }
  347. /**
  348. * check if the backend supports changing display names
  349. *
  350. * @return bool
  351. */
  352. public function canChangeDisplayName() {
  353. if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) {
  354. return false;
  355. }
  356. return $this->backend->implementsActions(Backend::SET_DISPLAYNAME);
  357. }
  358. /**
  359. * check if the user is enabled
  360. *
  361. * @return bool
  362. */
  363. public function isEnabled() {
  364. return $this->enabled;
  365. }
  366. /**
  367. * set the enabled status for the user
  368. *
  369. * @param bool $enabled
  370. */
  371. public function setEnabled(bool $enabled = true) {
  372. $oldStatus = $this->isEnabled();
  373. $this->enabled = $enabled;
  374. if ($oldStatus !== $this->enabled) {
  375. // TODO: First change the value, then trigger the event as done for all other properties.
  376. $this->triggerChange('enabled', $enabled, $oldStatus);
  377. $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false');
  378. }
  379. }
  380. /**
  381. * get the users email address
  382. *
  383. * @return string|null
  384. * @since 9.0.0
  385. */
  386. public function getEMailAddress() {
  387. return $this->getPrimaryEMailAddress() ?? $this->getSystemEMailAddress();
  388. }
  389. /**
  390. * @inheritDoc
  391. */
  392. public function getSystemEMailAddress(): ?string {
  393. return $this->config->getUserValue($this->uid, 'settings', 'email', null);
  394. }
  395. /**
  396. * @inheritDoc
  397. */
  398. public function getPrimaryEMailAddress(): ?string {
  399. return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
  400. }
  401. /**
  402. * get the users' quota
  403. *
  404. * @return string
  405. * @since 9.0.0
  406. */
  407. public function getQuota() {
  408. // allow apps to modify the user quota by hooking into the event
  409. $event = new GetQuotaEvent($this);
  410. $this->dispatcher->dispatchTyped($event);
  411. $overwriteQuota = $event->getQuota();
  412. if ($overwriteQuota) {
  413. $quota = $overwriteQuota;
  414. } else {
  415. $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default');
  416. }
  417. if ($quota === 'default') {
  418. $quota = $this->config->getAppValue('files', 'default_quota', 'none');
  419. // if unlimited quota is not allowed => avoid getting 'unlimited' as default_quota fallback value
  420. // use the first preset instead
  421. $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1';
  422. if (!$allowUnlimitedQuota) {
  423. $presets = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
  424. $presets = array_filter(array_map('trim', explode(',', $presets)));
  425. $quotaPreset = array_values(array_diff($presets, ['default', 'none']));
  426. if (count($quotaPreset) > 0) {
  427. $quota = $this->config->getAppValue('files', 'default_quota', $quotaPreset[0]);
  428. }
  429. }
  430. }
  431. return $quota;
  432. }
  433. /**
  434. * set the users' quota
  435. *
  436. * @param string $quota
  437. * @return void
  438. * @since 9.0.0
  439. */
  440. public function setQuota($quota) {
  441. $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
  442. if ($quota !== 'none' and $quota !== 'default') {
  443. $quota = OC_Helper::computerFileSize($quota);
  444. $quota = OC_Helper::humanFileSize($quota);
  445. }
  446. if ($quota !== $oldQuota) {
  447. $this->config->setUserValue($this->uid, 'files', 'quota', $quota);
  448. $this->triggerChange('quota', $quota, $oldQuota);
  449. }
  450. }
  451. /**
  452. * get the avatar image if it exists
  453. *
  454. * @param int $size
  455. * @return IImage|null
  456. * @since 9.0.0
  457. */
  458. public function getAvatarImage($size) {
  459. // delay the initialization
  460. if (is_null($this->avatarManager)) {
  461. $this->avatarManager = \OC::$server->getAvatarManager();
  462. }
  463. $avatar = $this->avatarManager->getAvatar($this->uid);
  464. $image = $avatar->get(-1);
  465. if ($image) {
  466. return $image;
  467. }
  468. return null;
  469. }
  470. /**
  471. * get the federation cloud id
  472. *
  473. * @return string
  474. * @since 9.0.0
  475. */
  476. public function getCloudId() {
  477. $uid = $this->getUID();
  478. $server = $this->urlGenerator->getAbsoluteURL('/');
  479. $server = rtrim($this->removeProtocolFromUrl($server), '/');
  480. return $uid . '@' . $server;
  481. }
  482. /**
  483. * @param string $url
  484. * @return string
  485. */
  486. private function removeProtocolFromUrl($url) {
  487. if (strpos($url, 'https://') === 0) {
  488. return substr($url, strlen('https://'));
  489. } elseif (strpos($url, 'http://') === 0) {
  490. return substr($url, strlen('http://'));
  491. }
  492. return $url;
  493. }
  494. public function triggerChange($feature, $value = null, $oldValue = null) {
  495. $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [
  496. 'feature' => $feature,
  497. 'value' => $value,
  498. 'oldValue' => $oldValue,
  499. ]));
  500. if ($this->emitter) {
  501. $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]);
  502. }
  503. }
  504. }