1
0

AvatarManager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * @see \OCP\IAvatar
  68. * @param string $userId the ownCloud user id
  69. * @throws \Exception In case the username is potentially dangerous
  70. * @throws NotFoundException In case there is no user folder yet
  71. */
  72. public function getAvatar(string $userId): IAvatar {
  73. $user = $this->userManager->get($userId);
  74. if ($user === null) {
  75. throw new \Exception('user does not exist');
  76. }
  77. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  78. $userId = $user->getUID();
  79. $requestingUser = $this->userSession->getUser();
  80. try {
  81. $folder = $this->appData->getFolder($userId);
  82. } catch (NotFoundException $e) {
  83. $folder = $this->appData->newFolder($userId);
  84. }
  85. try {
  86. $account = $this->accountManager->getAccount($user);
  87. $avatarProperties = $account->getProperty(IAccountManager::PROPERTY_AVATAR);
  88. $avatarScope = $avatarProperties->getScope();
  89. } catch (PropertyDoesNotExistException $e) {
  90. $avatarScope = '';
  91. }
  92. switch ($avatarScope) {
  93. // v2-private scope hides the avatar from public access and from unknown users
  94. case IAccountManager::SCOPE_PRIVATE:
  95. if ($requestingUser !== null && $this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)) {
  96. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  97. }
  98. break;
  99. case IAccountManager::SCOPE_LOCAL:
  100. case IAccountManager::SCOPE_FEDERATED:
  101. case IAccountManager::SCOPE_PUBLISHED:
  102. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  103. default:
  104. // use a placeholder avatar which caches the generated images
  105. return new PlaceholderAvatar($folder, $user, $this->logger);
  106. }
  107. return new PlaceholderAvatar($folder, $user, $this->logger);
  108. }
  109. /**
  110. * Clear generated avatars
  111. */
  112. public function clearCachedAvatars(): void {
  113. $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
  114. foreach ($users as $userId) {
  115. // This also bumps the avatar version leading to cache invalidation in browsers
  116. $this->getAvatar($userId)->remove();
  117. }
  118. }
  119. public function deleteUserAvatar(string $userId): void {
  120. try {
  121. $folder = $this->appData->getFolder($userId);
  122. $folder->delete();
  123. } catch (NotFoundException $e) {
  124. $this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
  125. } catch (NotPermittedException | StorageNotAvailableException $e) {
  126. $this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
  127. } catch (NoUserException $e) {
  128. $this->logger->debug("User $userId not found. gnoring avatar deletion");
  129. }
  130. $this->config->deleteUserValue($userId, 'avatar', 'generated');
  131. }
  132. /**
  133. * Returns a GuestAvatar.
  134. *
  135. * @param string $name The guest name, e.g. "Albert".
  136. */
  137. public function getGuestAvatar(string $name): IAvatar {
  138. return new GuestAvatar($name, $this->logger);
  139. }
  140. }