UserContext.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_External\Config;
  7. use OCP\IRequest;
  8. use OCP\IUser;
  9. use OCP\IUserManager;
  10. use OCP\IUserSession;
  11. use OCP\Share\Exceptions\ShareNotFound;
  12. use OCP\Share\IManager as ShareManager;
  13. class UserContext {
  14. /** @var string */
  15. private $userId;
  16. public function __construct(
  17. private IUserSession $session,
  18. private ShareManager $shareManager,
  19. private IRequest $request,
  20. private IUserManager $userManager,
  21. ) {
  22. }
  23. public function getSession(): IUserSession {
  24. return $this->session;
  25. }
  26. public function setUserId(string $userId): void {
  27. $this->userId = $userId;
  28. }
  29. protected function getUserId(): ?string {
  30. if ($this->userId !== null) {
  31. return $this->userId;
  32. }
  33. if ($this->session->getUser() !== null) {
  34. return $this->session->getUser()->getUID();
  35. }
  36. try {
  37. $shareToken = $this->request->getParam('token');
  38. $share = $this->shareManager->getShareByToken($shareToken);
  39. return $share->getShareOwner();
  40. } catch (ShareNotFound $e) {
  41. }
  42. return null;
  43. }
  44. protected function getUser(): ?IUser {
  45. $userId = $this->getUserId();
  46. if ($userId !== null) {
  47. return $this->userManager->get($userId);
  48. }
  49. return null;
  50. }
  51. }