AvatarManager.php 5.7 KB

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