1
0

ProvisioningApiMiddleware.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Provisioning_API\Middleware;
  27. use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\AppFramework\Middleware;
  31. use OCP\AppFramework\OCS\OCSException;
  32. use OCP\AppFramework\Utility\IControllerMethodReflector;
  33. class ProvisioningApiMiddleware extends Middleware {
  34. /** @var IControllerMethodReflector */
  35. private $reflector;
  36. /** @var bool */
  37. private $isAdmin;
  38. /** @var bool */
  39. private $isSubAdmin;
  40. /**
  41. * ProvisioningApiMiddleware constructor.
  42. *
  43. * @param IControllerMethodReflector $reflector
  44. * @param bool $isAdmin
  45. * @param bool $isSubAdmin
  46. */
  47. public function __construct(
  48. IControllerMethodReflector $reflector,
  49. bool $isAdmin,
  50. bool $isSubAdmin) {
  51. $this->reflector = $reflector;
  52. $this->isAdmin = $isAdmin;
  53. $this->isSubAdmin = $isSubAdmin;
  54. }
  55. /**
  56. * @param Controller $controller
  57. * @param string $methodName
  58. *
  59. * @throws NotSubAdminException
  60. */
  61. public function beforeController($controller, $methodName) {
  62. if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) {
  63. throw new NotSubAdminException();
  64. }
  65. }
  66. /**
  67. * @param Controller $controller
  68. * @param string $methodName
  69. * @param \Exception $exception
  70. * @throws \Exception
  71. * @return Response
  72. */
  73. public function afterException($controller, $methodName, \Exception $exception) {
  74. if ($exception instanceof NotSubAdminException) {
  75. throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED);
  76. }
  77. throw $exception;
  78. }
  79. }