SharingCheckMiddleware.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public function __construct(
  29. protected string $appName,
  30. protected IConfig $config,
  31. protected IAppManager $appManager,
  32. protected IControllerMethodReflector $reflector,
  33. protected IManager $shareManager,
  34. protected IRequest $request,
  35. ) {
  36. }
  37. /**
  38. * Check if sharing is enabled before the controllers is executed
  39. *
  40. * @param Controller $controller
  41. * @param string $methodName
  42. * @throws NotFoundException
  43. * @throws S2SException
  44. */
  45. public function beforeController($controller, $methodName): void {
  46. if (!$this->isSharingEnabled()) {
  47. throw new NotFoundException('Sharing is disabled.');
  48. }
  49. if ($controller instanceof ExternalSharesController &&
  50. !$this->externalSharesChecks()) {
  51. throw new S2SException('Federated sharing not allowed');
  52. }
  53. }
  54. /**
  55. * Return 404 page in case of a not found exception
  56. *
  57. * @param Controller $controller
  58. * @param string $methodName
  59. * @param \Exception $exception
  60. * @return Response
  61. * @throws \Exception
  62. */
  63. public function afterException($controller, $methodName, \Exception $exception): Response {
  64. if (is_a($exception, NotFoundException::class)) {
  65. return new NotFoundResponse();
  66. }
  67. if (is_a($exception, S2SException::class)) {
  68. return new JSONResponse($exception->getMessage(), 405);
  69. }
  70. throw $exception;
  71. }
  72. /**
  73. * Checks for externalshares controller
  74. * @return bool
  75. */
  76. private function externalSharesChecks(): bool {
  77. if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') &&
  78. $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') {
  79. return false;
  80. }
  81. if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') &&
  82. $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * Check whether sharing is enabled
  89. * @return bool
  90. */
  91. private function isSharingEnabled(): bool {
  92. // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
  93. // Check whether the sharing application is enabled
  94. if (!$this->appManager->isEnabledForUser($this->appName)) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. }