AvatarManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IAvatarManager;
  31. use OCP\IConfig;
  32. use OCP\ILogger;
  33. use OCP\IUserManager;
  34. use OCP\IL10N;
  35. /**
  36. * This class implements methods to access Avatar functionality
  37. */
  38. class AvatarManager implements IAvatarManager {
  39. /** @var IUserManager */
  40. private $userManager;
  41. /** @var IAppData */
  42. private $appData;
  43. /** @var IL10N */
  44. private $l;
  45. /** @var ILogger */
  46. private $logger;
  47. /** @var IConfig */
  48. private $config;
  49. /**
  50. * AvatarManager constructor.
  51. *
  52. * @param IUserManager $userManager
  53. * @param IAppData $appData
  54. * @param IL10N $l
  55. * @param ILogger $logger
  56. * @param IConfig $config
  57. */
  58. public function __construct(
  59. IUserManager $userManager,
  60. IAppData $appData,
  61. IL10N $l,
  62. ILogger $logger,
  63. IConfig $config) {
  64. $this->userManager = $userManager;
  65. $this->appData = $appData;
  66. $this->l = $l;
  67. $this->logger = $logger;
  68. $this->config = $config;
  69. $this->userManager->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
  70. if ($feature === 'displayName') {
  71. $this->updateAvatarVersionOnDisplayNameChange($user);
  72. }
  73. });
  74. }
  75. /**
  76. * return a user specific instance of \OCP\IAvatar
  77. * @see \OCP\IAvatar
  78. * @param string $userId the ownCloud user id
  79. * @return \OCP\IAvatar
  80. * @throws \Exception In case the username is potentially dangerous
  81. * @throws NotFoundException In case there is no user folder yet
  82. */
  83. public function getAvatar($userId) {
  84. $user = $this->userManager->get($userId);
  85. if (is_null($user)) {
  86. throw new \Exception('user does not exist');
  87. }
  88. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  89. $userId = $user->getUID();
  90. try {
  91. $folder = $this->appData->getFolder($userId);
  92. } catch (NotFoundException $e) {
  93. $folder = $this->appData->newFolder($userId);
  94. }
  95. return new Avatar($folder, $this->l, $user, $this->logger, $this->config);
  96. }
  97. /**
  98. * Increases the avatar version if needed.
  99. *
  100. * When a user has no avatar set the avatar is generated in the client based
  101. * on the display name, so in that case a display name change acts like an
  102. * avatar change and its version has to be increased.
  103. *
  104. * @param \OC\User\User $user the user whose display name has changed
  105. */
  106. private function updateAvatarVersionOnDisplayNameChange(\OC\User\User $user) {
  107. $avatar = $this->getAvatar($user->getUID());
  108. if (!$avatar->exists()) {
  109. $this->config->setUserValue($user->getUID(), 'avatar', 'version',
  110. (int)$this->config->getUserValue($user->getUID(), 'avatar', 'version', 0) + 1);
  111. }
  112. }
  113. }