UserTrait.php 975 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Service;
  8. use OCP\IUser;
  9. use OCP\IUserSession;
  10. /**
  11. * Trait for getting user information in a service
  12. */
  13. trait UserTrait {
  14. /** @var IUserSession */
  15. protected $userSession;
  16. /**
  17. * User override
  18. *
  19. * @var IUser|null
  20. */
  21. private $user = null;
  22. /**
  23. * @return IUser|null
  24. */
  25. protected function getUser() {
  26. if ($this->user) {
  27. return $this->user;
  28. }
  29. return $this->userSession->getUser();
  30. }
  31. /**
  32. * Override the user from the session
  33. * Unset with ->resetUser() when finished!
  34. *
  35. * @param IUser $user
  36. * @return self
  37. */
  38. public function setUser(IUser $user) {
  39. $this->user = $user;
  40. return $this;
  41. }
  42. /**
  43. * Reset the user override
  44. *
  45. * @return self
  46. */
  47. public function resetUser() {
  48. $this->user = null;
  49. return $this;
  50. }
  51. }