SharingCheckMiddleware.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_Sharing\Middleware;
  9. use OCA\Files_Sharing\Controller\ExternalSharesController;
  10. use OCA\Files_Sharing\Exceptions\S2SException;
  11. use OCP\App\IAppManager;
  12. use OCP\AppFramework\Controller;
  13. use OCP\AppFramework\Http\JSONResponse;
  14. use OCP\AppFramework\Http\NotFoundResponse;
  15. use OCP\AppFramework\Http\Response;
  16. use OCP\AppFramework\Middleware;
  17. use OCP\AppFramework\Utility\IControllerMethodReflector;
  18. use OCP\Files\NotFoundException;
  19. use OCP\IConfig;
  20. use OCP\IRequest;
  21. use OCP\Share\IManager;
  22. /**
  23. * Checks whether the "sharing check" is enabled
  24. *
  25. * @package OCA\Files_Sharing\Middleware
  26. */
  27. class SharingCheckMiddleware extends Middleware {
  28. /** @var string */
  29. protected $appName;
  30. /** @var IConfig */
  31. protected $config;
  32. /** @var IAppManager */
  33. protected $appManager;
  34. /** @var IControllerMethodReflector */
  35. protected $reflector;
  36. /** @var IManager */
  37. protected $shareManager;
  38. /** @var IRequest */
  39. protected $request;
  40. public function __construct(string $appName,
  41. IConfig $config,
  42. IAppManager $appManager,
  43. IControllerMethodReflector $reflector,
  44. IManager $shareManager,
  45. IRequest $request
  46. ) {
  47. $this->appName = $appName;
  48. $this->config = $config;
  49. $this->appManager = $appManager;
  50. $this->reflector = $reflector;
  51. $this->shareManager = $shareManager;
  52. $this->request = $request;
  53. }
  54. /**
  55. * Check if sharing is enabled before the controllers is executed
  56. *
  57. * @param Controller $controller
  58. * @param string $methodName
  59. * @throws NotFoundException
  60. * @throws S2SException
  61. */
  62. public function beforeController($controller, $methodName): void {
  63. if (!$this->isSharingEnabled()) {
  64. throw new NotFoundException('Sharing is disabled.');
  65. }
  66. if ($controller instanceof ExternalSharesController &&
  67. !$this->externalSharesChecks()) {
  68. throw new S2SException('Federated sharing not allowed');
  69. }
  70. }
  71. /**
  72. * Return 404 page in case of a not found exception
  73. *
  74. * @param Controller $controller
  75. * @param string $methodName
  76. * @param \Exception $exception
  77. * @return Response
  78. * @throws \Exception
  79. */
  80. public function afterException($controller, $methodName, \Exception $exception): Response {
  81. if (is_a($exception, NotFoundException::class)) {
  82. return new NotFoundResponse();
  83. }
  84. if (is_a($exception, S2SException::class)) {
  85. return new JSONResponse($exception->getMessage(), 405);
  86. }
  87. throw $exception;
  88. }
  89. /**
  90. * Checks for externalshares controller
  91. * @return bool
  92. */
  93. private function externalSharesChecks(): bool {
  94. if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') &&
  95. $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') {
  96. return false;
  97. }
  98. if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') &&
  99. $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Check whether sharing is enabled
  106. * @return bool
  107. */
  108. private function isSharingEnabled(): bool {
  109. // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
  110. // Check whether the sharing application is enabled
  111. if (!$this->appManager->isEnabledForUser($this->appName)) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. }