AvatarManager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. *
  92. * If the user is disabled a guest avatar will be returned
  93. *
  94. * @see \OCP\IAvatar
  95. * @param string $userId the ownCloud user id
  96. * @return \OCP\IAvatar
  97. * @throws \Exception In case the username is potentially dangerous
  98. * @throws NotFoundException In case there is no user folder yet
  99. */
  100. public function getAvatar(string $userId) : IAvatar {
  101. $user = $this->userManager->get($userId);
  102. if ($user === null) {
  103. throw new \Exception('user does not exist');
  104. }
  105. if (!$user->isEnabled()) {
  106. return $this->getGuestAvatar($userId);
  107. }
  108. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  109. $userId = $user->getUID();
  110. $requestingUser = null;
  111. if ($this->userSession !== null) {
  112. $requestingUser = $this->userSession->getUser();
  113. }
  114. try {
  115. $folder = $this->appData->getFolder($userId);
  116. } catch (NotFoundException $e) {
  117. $folder = $this->appData->newFolder($userId);
  118. }
  119. try {
  120. $account = $this->accountManager->getAccount($user);
  121. $avatarProperties = $account->getProperty(IAccountManager::PROPERTY_AVATAR);
  122. $avatarScope = $avatarProperties->getScope();
  123. } catch (PropertyDoesNotExistException $e) {
  124. $avatarScope = '';
  125. }
  126. switch ($avatarScope) {
  127. // v2-private scope hides the avatar from public access and from unknown users
  128. case IAccountManager::SCOPE_PRIVATE:
  129. if ($requestingUser !== null && $this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)) {
  130. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  131. }
  132. break;
  133. case IAccountManager::SCOPE_LOCAL:
  134. case IAccountManager::SCOPE_FEDERATED:
  135. case IAccountManager::SCOPE_PUBLISHED:
  136. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  137. default:
  138. // use a placeholder avatar which caches the generated images
  139. return new PlaceholderAvatar($folder, $user, $this->logger);
  140. }
  141. return new PlaceholderAvatar($folder, $user, $this->logger);
  142. }
  143. /**
  144. * Clear generated avatars
  145. */
  146. public function clearCachedAvatars() {
  147. $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
  148. foreach ($users as $userId) {
  149. // This also bumps the avatar version leading to cache invalidation in browsers
  150. $this->getAvatar($userId)->remove();
  151. }
  152. }
  153. public function deleteUserAvatar(string $userId): void {
  154. try {
  155. $folder = $this->appData->getFolder($userId);
  156. $folder->delete();
  157. } catch (NotFoundException $e) {
  158. $this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
  159. } catch (NotPermittedException | StorageNotAvailableException $e) {
  160. $this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
  161. } catch (NoUserException $e) {
  162. $this->logger->debug("User $userId not found. gnoring avatar deletion");
  163. }
  164. $this->config->deleteUserValue($userId, 'avatar', 'generated');
  165. }
  166. /**
  167. * Returns a GuestAvatar.
  168. *
  169. * @param string $name The guest name, e.g. "Albert".
  170. * @return IAvatar
  171. */
  172. public function getGuestAvatar(string $name): IAvatar {
  173. return new GuestAvatar($name, $this->logger);
  174. }
  175. }