You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.3 KiB

3 years ago
  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 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Michael Weimann <mail@michael-weimann.eu>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Avatar;
  33. use OC\User\Manager;
  34. use OC\User\NoUserException;
  35. use OCP\Files\IAppData;
  36. use OCP\Files\NotFoundException;
  37. use OCP\Files\NotPermittedException;
  38. use OCP\IAvatar;
  39. use OCP\IAvatarManager;
  40. use OCP\IConfig;
  41. use OCP\IL10N;
  42. use OCP\ILogger;
  43. /**
  44. * This class implements methods to access Avatar functionality
  45. */
  46. class AvatarManager implements IAvatarManager {
  47. /** @var Manager */
  48. private $userManager;
  49. /** @var IAppData */
  50. private $appData;
  51. /** @var IL10N */
  52. private $l;
  53. /** @var ILogger */
  54. private $logger;
  55. /** @var IConfig */
  56. private $config;
  57. /**
  58. * AvatarManager constructor.
  59. *
  60. * @param Manager $userManager
  61. * @param IAppData $appData
  62. * @param IL10N $l
  63. * @param ILogger $logger
  64. * @param IConfig $config
  65. */
  66. public function __construct(
  67. Manager $userManager,
  68. IAppData $appData,
  69. IL10N $l,
  70. ILogger $logger,
  71. IConfig $config) {
  72. $this->userManager = $userManager;
  73. $this->appData = $appData;
  74. $this->l = $l;
  75. $this->logger = $logger;
  76. $this->config = $config;
  77. }
  78. /**
  79. * return a user specific instance of \OCP\IAvatar
  80. * @see \OCP\IAvatar
  81. * @param string $userId the ownCloud user id
  82. * @return \OCP\IAvatar
  83. * @throws \Exception In case the username is potentially dangerous
  84. * @throws NotFoundException In case there is no user folder yet
  85. */
  86. public function getAvatar(string $userId) : IAvatar {
  87. $user = $this->userManager->get($userId);
  88. if ($user === null) {
  89. throw new \Exception('user does not exist');
  90. }
  91. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  92. $userId = $user->getUID();
  93. try {
  94. $folder = $this->appData->getFolder($userId);
  95. } catch (NotFoundException $e) {
  96. $folder = $this->appData->newFolder($userId);
  97. }
  98. return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
  99. }
  100. /**
  101. * Clear generated avatars
  102. */
  103. public function clearCachedAvatars() {
  104. $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
  105. foreach ($users as $userId) {
  106. try {
  107. $folder = $this->appData->getFolder($userId);
  108. $folder->delete();
  109. } catch (NotFoundException $e) {
  110. $this->logger->debug("No cache for the user $userId. Ignoring...");
  111. }
  112. $this->config->setUserValue($userId, 'avatar', 'generated', 'false');
  113. }
  114. }
  115. public function deleteUserAvatar(string $userId): void {
  116. try {
  117. $folder = $this->appData->getFolder($userId);
  118. $folder->delete();
  119. } catch (NotFoundException $e) {
  120. $this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
  121. } catch (NotPermittedException $e) {
  122. $this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
  123. } catch (NoUserException $e) {
  124. $this->logger->debug("User $userId not found. gnoring avatar deletion");
  125. }
  126. $this->config->deleteUserValue($userId, 'avatar', 'generated');
  127. }
  128. /**
  129. * Returns a GuestAvatar.
  130. *
  131. * @param string $name The guest name, e.g. "Albert".
  132. * @return IAvatar
  133. */
  134. public function getGuestAvatar(string $name): IAvatar {
  135. return new GuestAvatar($name, $this->logger);
  136. }
  137. }