1
0

UserContext.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_External\Config;
  26. use OCP\IRequest;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\IUserSession;
  30. use OCP\Share\Exceptions\ShareNotFound;
  31. use OCP\Share\IManager as ShareManager;
  32. class UserContext {
  33. /** @var IUserSession */
  34. private $session;
  35. /** @var ShareManager */
  36. private $shareManager;
  37. /** @var IRequest */
  38. private $request;
  39. /** @var string */
  40. private $userId;
  41. /** @var IUserManager */
  42. private $userManager;
  43. public function __construct(IUserSession $session, ShareManager $manager, IRequest $request, IUserManager $userManager) {
  44. $this->session = $session;
  45. $this->shareManager = $manager;
  46. $this->request = $request;
  47. $this->userManager = $userManager;
  48. }
  49. public function getSession(): IUserSession {
  50. return $this->session;
  51. }
  52. public function setUserId(string $userId): void {
  53. $this->userId = $userId;
  54. }
  55. protected function getUserId(): ?string {
  56. if ($this->userId !== null) {
  57. return $this->userId;
  58. }
  59. if ($this->session && $this->session->getUser() !== null) {
  60. return $this->session->getUser()->getUID();
  61. }
  62. try {
  63. $shareToken = $this->request->getParam('token');
  64. $share = $this->shareManager->getShareByToken($shareToken);
  65. return $share->getShareOwner();
  66. } catch (ShareNotFound $e) {
  67. }
  68. return null;
  69. }
  70. protected function getUser(): ?IUser {
  71. $userId = $this->getUserId();
  72. if ($userId !== null) {
  73. return $this->userManager->get($userId);
  74. }
  75. return null;
  76. }
  77. }