LockdownManager.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Robin Appelman <robin@icewind.nl>
  4. *
  5. * This code is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License, version 3,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License, version 3,
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>
  16. *
  17. */
  18. namespace OC\Lockdown;
  19. use OC\Authentication\Token\IToken;
  20. use OCP\ISession;
  21. use OCP\Lockdown\ILockdownManager;
  22. class LockdownManager implements ILockdownManager {
  23. /** @var ISession */
  24. private $sessionCallback;
  25. private $enabled = false;
  26. /** @var array|null */
  27. private $scope;
  28. /**
  29. * LockdownManager constructor.
  30. *
  31. * @param callable $sessionCallback we need to inject the session lazily to avoid dependency loops
  32. */
  33. public function __construct(callable $sessionCallback) {
  34. $this->sessionCallback = $sessionCallback;
  35. }
  36. public function enable() {
  37. $this->enabled = true;
  38. }
  39. /**
  40. * @return ISession
  41. */
  42. private function getSession() {
  43. $callback = $this->sessionCallback;
  44. return $callback();
  45. }
  46. private function getScopeAsArray() {
  47. if (!$this->scope) {
  48. $session = $this->getSession();
  49. $sessionScope = $session->get('token_scope');
  50. if ($sessionScope) {
  51. $this->scope = $sessionScope;
  52. }
  53. }
  54. return $this->scope;
  55. }
  56. public function setToken(IToken $token) {
  57. $this->scope = $token->getScopeAsArray();
  58. $session = $this->getSession();
  59. $session->set('token_scope', $this->scope);
  60. $this->enable();
  61. }
  62. public function canAccessFilesystem() {
  63. $scope = $this->getScopeAsArray();
  64. return !$scope || $scope['filesystem'];
  65. }
  66. }