AvatarManager.php 5.6 KB

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