SharingCheckMiddleware.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing\Middleware;
  27. use OCA\Files_Sharing\Controller\ExternalSharesController;
  28. use OCA\Files_Sharing\Controller\ShareController;
  29. use OCP\App\IAppManager;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http\NotFoundResponse;
  32. use OCP\AppFramework\Middleware;
  33. use OCP\Files\NotFoundException;
  34. use OCP\IConfig;
  35. use OCP\AppFramework\Utility\IControllerMethodReflector;
  36. use OCA\Files_Sharing\Exceptions\S2SException;
  37. use OCP\AppFramework\Http\JSONResponse;
  38. use OCP\IRequest;
  39. use OCP\Share\IManager;
  40. use OCP\Share\Exceptions\ShareNotFound;
  41. /**
  42. * Checks whether the "sharing check" is enabled
  43. *
  44. * @package OCA\Files_Sharing\Middleware
  45. */
  46. class SharingCheckMiddleware extends Middleware {
  47. /** @var string */
  48. protected $appName;
  49. /** @var IConfig */
  50. protected $config;
  51. /** @var IAppManager */
  52. protected $appManager;
  53. /** @var IControllerMethodReflector */
  54. protected $reflector;
  55. /** @var IManager */
  56. protected $shareManager;
  57. /** @var IRequest */
  58. protected $request;
  59. /***
  60. * @param string $appName
  61. * @param IConfig $config
  62. * @param IAppManager $appManager
  63. * @param IControllerMethodReflector $reflector
  64. * @param IManager $shareManager
  65. * @param IRequest $request
  66. */
  67. public function __construct($appName,
  68. IConfig $config,
  69. IAppManager $appManager,
  70. IControllerMethodReflector $reflector,
  71. IManager $shareManager,
  72. IRequest $request
  73. ) {
  74. $this->appName = $appName;
  75. $this->config = $config;
  76. $this->appManager = $appManager;
  77. $this->reflector = $reflector;
  78. $this->shareManager = $shareManager;
  79. $this->request = $request;
  80. }
  81. /**
  82. * Check if sharing is enabled before the controllers is executed
  83. *
  84. * @param Controller $controller
  85. * @param string $methodName
  86. * @throws NotFoundException
  87. * @throws S2SException
  88. * @throws ShareNotFound
  89. */
  90. public function beforeController($controller, $methodName) {
  91. if(!$this->isSharingEnabled()) {
  92. throw new NotFoundException('Sharing is disabled.');
  93. }
  94. if ($controller instanceof ExternalSharesController &&
  95. !$this->externalSharesChecks()) {
  96. throw new S2SException('Federated sharing not allowed');
  97. }
  98. }
  99. /**
  100. * Return 404 page in case of a not found exception
  101. *
  102. * @param Controller $controller
  103. * @param string $methodName
  104. * @param \Exception $exception
  105. * @return NotFoundResponse
  106. * @throws \Exception
  107. */
  108. public function afterException($controller, $methodName, \Exception $exception) {
  109. if(is_a($exception, NotFoundException::class)) {
  110. return new NotFoundResponse();
  111. }
  112. if (is_a($exception, S2SException::class)) {
  113. return new JSONResponse($exception->getMessage(), 405);
  114. }
  115. throw $exception;
  116. }
  117. /**
  118. * Checks for externalshares controller
  119. * @return bool
  120. */
  121. private function externalSharesChecks() {
  122. if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') &&
  123. $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') {
  124. return false;
  125. }
  126. if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') &&
  127. $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
  128. return false;
  129. }
  130. return true;
  131. }
  132. /**
  133. * Check whether sharing is enabled
  134. * @return bool
  135. */
  136. private function isSharingEnabled() {
  137. // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
  138. // Check whether the sharing application is enabled
  139. if(!$this->appManager->isEnabledForUser($this->appName)) {
  140. return false;
  141. }
  142. return true;
  143. }
  144. }