AvatarManager.php 3.2 KB

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