1
0

LockdownManager.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Lockdown;
  7. use OCP\Authentication\Token\IToken;
  8. use OCP\ISession;
  9. use OCP\Lockdown\ILockdownManager;
  10. class LockdownManager implements ILockdownManager {
  11. /** @var ISession */
  12. private $sessionCallback;
  13. private $enabled = false;
  14. /** @var array|null */
  15. private $scope;
  16. /**
  17. * LockdownManager constructor.
  18. *
  19. * @param callable $sessionCallback we need to inject the session lazily to avoid dependency loops
  20. */
  21. public function __construct(callable $sessionCallback) {
  22. $this->sessionCallback = $sessionCallback;
  23. }
  24. public function enable() {
  25. $this->enabled = true;
  26. }
  27. /**
  28. * @return ISession
  29. */
  30. private function getSession() {
  31. $callback = $this->sessionCallback;
  32. return $callback();
  33. }
  34. private function getScopeAsArray() {
  35. if (!$this->scope) {
  36. $session = $this->getSession();
  37. $sessionScope = $session->get('token_scope');
  38. if ($sessionScope) {
  39. $this->scope = $sessionScope;
  40. }
  41. }
  42. return $this->scope;
  43. }
  44. public function setToken(IToken $token) {
  45. $this->scope = $token->getScopeAsArray();
  46. $session = $this->getSession();
  47. $session->set('token_scope', $this->scope);
  48. $this->enable();
  49. }
  50. public function canAccessFilesystem() {
  51. $scope = $this->getScopeAsArray();
  52. return !$scope || $scope[IToken::SCOPE_FILESYSTEM];
  53. }
  54. }