1
0

AvatarManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Weimann <mail@michael-weimann.eu>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\Avatar;
  35. use OC\KnownUser\KnownUserService;
  36. use OC\User\Manager;
  37. use OC\User\NoUserException;
  38. use OCP\Accounts\IAccountManager;
  39. use OCP\Accounts\PropertyDoesNotExistException;
  40. use OCP\Files\IAppData;
  41. use OCP\Files\NotFoundException;
  42. use OCP\Files\NotPermittedException;
  43. use OCP\Files\StorageNotAvailableException;
  44. use OCP\IAvatar;
  45. use OCP\IAvatarManager;
  46. use OCP\IConfig;
  47. use OCP\IL10N;
  48. use OCP\IUserSession;
  49. use Psr\Log\LoggerInterface;
  50. /**
  51. * This class implements methods to access Avatar functionality
  52. */
  53. class AvatarManager implements IAvatarManager {
  54. public function __construct(
  55. private IUserSession $userSession,
  56. private Manager $userManager,
  57. private IAppData $appData,
  58. private IL10N $l,
  59. private LoggerInterface $logger,
  60. private IConfig $config,
  61. private IAccountManager $accountManager,
  62. private KnownUserService $knownUserService,
  63. ) {
  64. }
  65. /**
  66. * return a user specific instance of \OCP\IAvatar
  67. *
  68. * If the user is disabled a guest avatar will be returned
  69. *
  70. * @see \OCP\IAvatar
  71. * @param string $userId the ownCloud user id
  72. * @throws \Exception In case the username is potentially dangerous
  73. * @throws NotFoundException In case there is no user folder yet
  74. */
  75. public function getAvatar(string $userId): IAvatar {
  76. $user = $this->userManager->get($userId);
  77. if ($user === null) {
  78. throw new \Exception('user does not exist');
  79. }
  80. if (!$user->isEnabled()) {
  81. return $this->getGuestAvatar($userId);
  82. }
  83. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  84. $userId = $user->getUID();
  85. $requestingUser = $this->userSession->getUser();
  86. try {
  87. $folder = $this->appData->getFolder($userId);
  88. } catch (NotFoundException $e) {
  89. $folder = $this->appData->newFolder($userId);
  90. }
  91. try {
  92. $account = $this->accountManager->getAccount($user);
  93. $avatarProperties = $account->getProperty(IAccountManager::PROPERTY_AVATAR);
  94. $avatarScope = $avatarProperties->getScope();
  95. } catch (PropertyDoesNotExistException $e) {
  96. $avatarScope = '';
  97. }
  98. switch ($avatarScope) {
  99. // v2-private scope hides the avatar from public access and from unknown users
  100. case IAccountManager::SCOPE_PRIVATE:
  101. if ($requestingUser !== null && $this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)) {
  102. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  103. }
  104. break;
  105. case IAccountManager::SCOPE_LOCAL:
  106. case IAccountManager::SCOPE_FEDERATED:
  107. case IAccountManager::SCOPE_PUBLISHED:
  108. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  109. default:
  110. // use a placeholder avatar which caches the generated images
  111. return new PlaceholderAvatar($folder, $user, $this->logger);
  112. }
  113. return new PlaceholderAvatar($folder, $user, $this->logger);
  114. }
  115. /**
  116. * Clear generated avatars
  117. */
  118. public function clearCachedAvatars(): void {
  119. $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
  120. foreach ($users as $userId) {
  121. // This also bumps the avatar version leading to cache invalidation in browsers
  122. $this->getAvatar($userId)->remove();
  123. }
  124. }
  125. public function deleteUserAvatar(string $userId): void {
  126. try {
  127. $folder = $this->appData->getFolder($userId);
  128. $folder->delete();
  129. } catch (NotFoundException $e) {
  130. $this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
  131. } catch (NotPermittedException | StorageNotAvailableException $e) {
  132. $this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
  133. } catch (NoUserException $e) {
  134. $this->logger->debug("User $userId not found. gnoring avatar deletion");
  135. }
  136. $this->config->deleteUserValue($userId, 'avatar', 'generated');
  137. }
  138. /**
  139. * Returns a GuestAvatar.
  140. *
  141. * @param string $name The guest name, e.g. "Albert".
  142. */
  143. public function getGuestAvatar(string $name): IAvatar {
  144. return new GuestAvatar($name, $this->logger);
  145. }
  146. }