UserTrait.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Service;
  25. use OCP\IUser;
  26. use OCP\IUserSession;
  27. /**
  28. * Trait for getting user information in a service
  29. */
  30. trait UserTrait {
  31. /** @var IUserSession */
  32. protected $userSession;
  33. /**
  34. * User override
  35. *
  36. * @var IUser|null
  37. */
  38. private $user = null;
  39. /**
  40. * @return IUser|null
  41. */
  42. protected function getUser() {
  43. if ($this->user) {
  44. return $this->user;
  45. }
  46. return $this->userSession->getUser();
  47. }
  48. /**
  49. * Override the user from the session
  50. * Unset with ->resetUser() when finished!
  51. *
  52. * @param IUser $user
  53. * @return self
  54. */
  55. public function setUser(IUser $user) {
  56. $this->user = $user;
  57. return $this;
  58. }
  59. /**
  60. * Reset the user override
  61. *
  62. * @return self
  63. */
  64. public function resetUser() {
  65. $this->user = null;
  66. return $this;
  67. }
  68. }