1
0

ShareInfoMiddleware.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. public function __construct(
  17. private IManager $shareManager,
  18. ) {
  19. }
  20. /**
  21. * @param Controller $controller
  22. * @param string $methodName
  23. * @throws S2SException
  24. */
  25. public function beforeController($controller, $methodName) {
  26. if (!($controller instanceof ShareInfoController)) {
  27. return;
  28. }
  29. if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
  30. throw new S2SException();
  31. }
  32. }
  33. /**
  34. * @param Controller $controller
  35. * @param string $methodName
  36. * @param \Exception $exception
  37. * @throws \Exception
  38. * @return Response
  39. */
  40. public function afterException($controller, $methodName, \Exception $exception) {
  41. if (!($controller instanceof ShareInfoController)) {
  42. throw $exception;
  43. }
  44. if ($exception instanceof S2SException) {
  45. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  46. }
  47. throw $exception;
  48. }
  49. /**
  50. * @param Controller $controller
  51. * @param string $methodName
  52. * @param Response $response
  53. * @return Response
  54. */
  55. public function afterController($controller, $methodName, Response $response) {
  56. if (!($controller instanceof ShareInfoController)) {
  57. return $response;
  58. }
  59. if (!($response instanceof JSONResponse)) {
  60. return $response;
  61. }
  62. $data = $response->getData();
  63. $status = 'error';
  64. if ($response->getStatus() === Http::STATUS_OK) {
  65. $status = 'success';
  66. }
  67. $response->setData([
  68. 'data' => $data,
  69. 'status' => $status,
  70. ]);
  71. return $response;
  72. }
  73. }