AvatarManager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  70. /**
  71. * return a user specific instance of \OCP\IAvatar
  72. * @see \OCP\IAvatar
  73. * @param string $userId the ownCloud user id
  74. * @return \OCP\IAvatar
  75. * @throws \Exception In case the username is potentially dangerous
  76. * @throws NotFoundException In case there is no user folder yet
  77. */
  78. public function getAvatar($userId) {
  79. $user = $this->userManager->get($userId);
  80. if (is_null($user)) {
  81. throw new \Exception('user does not exist');
  82. }
  83. // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
  84. $userId = $user->getUID();
  85. try {
  86. $folder = $this->appData->getFolder($userId);
  87. } catch (NotFoundException $e) {
  88. $folder = $this->appData->newFolder($userId);
  89. }
  90. return new Avatar($folder, $this->l, $user, $this->logger, $this->config);
  91. }
  92. }