1
0

ShareInfoMiddleware.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Middleware;
  25. use OCA\Files_Sharing\Controller\ShareInfoController;
  26. use OCA\Files_Sharing\Exceptions\S2SException;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\AppFramework\Http\Response;
  31. use OCP\AppFramework\Middleware;
  32. use OCP\Share\IManager;
  33. class ShareInfoMiddleware extends Middleware {
  34. /** @var IManager */
  35. private $shareManager;
  36. public function __construct(IManager $shareManager) {
  37. $this->shareManager = $shareManager;
  38. }
  39. /**
  40. * @param Controller $controller
  41. * @param string $methodName
  42. * @throws S2SException
  43. */
  44. public function beforeController($controller, $methodName) {
  45. if (!($controller instanceof ShareInfoController)) {
  46. return;
  47. }
  48. if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
  49. throw new S2SException();
  50. }
  51. }
  52. /**
  53. * @param Controller $controller
  54. * @param string $methodName
  55. * @param \Exception $exception
  56. * @throws \Exception
  57. * @return Response
  58. */
  59. public function afterException($controller, $methodName, \Exception $exception) {
  60. if (!($controller instanceof ShareInfoController)) {
  61. throw $exception;
  62. }
  63. if ($exception instanceof S2SException) {
  64. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  65. }
  66. throw $exception;
  67. }
  68. /**
  69. * @param Controller $controller
  70. * @param string $methodName
  71. * @param Response $response
  72. * @return Response
  73. */
  74. public function afterController($controller, $methodName, Response $response) {
  75. if (!($controller instanceof ShareInfoController)) {
  76. return $response;
  77. }
  78. if (!($response instanceof JSONResponse)) {
  79. return $response;
  80. }
  81. $data = $response->getData();
  82. $status = 'error';
  83. if ($response->getStatus() === Http::STATUS_OK) {
  84. $status = 'success';
  85. }
  86. $response->setData([
  87. 'data' => $data,
  88. 'status' => $status,
  89. ]);
  90. return $response;
  91. }
  92. }