ShareInfoMiddleware.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Middleware;
  7. use OCA\Files_Sharing\Controller\ShareInfoController;
  8. use OCA\Files_Sharing\Exceptions\S2SException;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\JSONResponse;
  12. use OCP\AppFramework\Http\Response;
  13. use OCP\AppFramework\Middleware;
  14. use OCP\Share\IManager;
  15. class ShareInfoMiddleware extends Middleware {
  16. /** @var IManager */
  17. private $shareManager;
  18. public function __construct(IManager $shareManager) {
  19. $this->shareManager = $shareManager;
  20. }
  21. /**
  22. * @param Controller $controller
  23. * @param string $methodName
  24. * @throws S2SException
  25. */
  26. public function beforeController($controller, $methodName) {
  27. if (!($controller instanceof ShareInfoController)) {
  28. return;
  29. }
  30. if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
  31. throw new S2SException();
  32. }
  33. }
  34. /**
  35. * @param Controller $controller
  36. * @param string $methodName
  37. * @param \Exception $exception
  38. * @throws \Exception
  39. * @return Response
  40. */
  41. public function afterException($controller, $methodName, \Exception $exception) {
  42. if (!($controller instanceof ShareInfoController)) {
  43. throw $exception;
  44. }
  45. if ($exception instanceof S2SException) {
  46. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  47. }
  48. throw $exception;
  49. }
  50. /**
  51. * @param Controller $controller
  52. * @param string $methodName
  53. * @param Response $response
  54. * @return Response
  55. */
  56. public function afterController($controller, $methodName, Response $response) {
  57. if (!($controller instanceof ShareInfoController)) {
  58. return $response;
  59. }
  60. if (!($response instanceof JSONResponse)) {
  61. return $response;
  62. }
  63. $data = $response->getData();
  64. $status = 'error';
  65. if ($response->getStatus() === Http::STATUS_OK) {
  66. $status = 'success';
  67. }
  68. $response->setData([
  69. 'data' => $data,
  70. 'status' => $status,
  71. ]);
  72. return $response;
  73. }
  74. }