ProvisioningApiMiddleware.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Provisioning_API\Middleware;
  28. use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\Response;
  32. use OCP\AppFramework\Middleware;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\Utility\IControllerMethodReflector;
  35. class ProvisioningApiMiddleware extends Middleware {
  36. /** @var IControllerMethodReflector */
  37. private $reflector;
  38. /** @var bool */
  39. private $isAdmin;
  40. /** @var bool */
  41. private $isSubAdmin;
  42. /**
  43. * ProvisioningApiMiddleware constructor.
  44. *
  45. * @param IControllerMethodReflector $reflector
  46. * @param bool $isAdmin
  47. * @param bool $isSubAdmin
  48. */
  49. public function __construct(
  50. IControllerMethodReflector $reflector,
  51. bool $isAdmin,
  52. bool $isSubAdmin) {
  53. $this->reflector = $reflector;
  54. $this->isAdmin = $isAdmin;
  55. $this->isSubAdmin = $isSubAdmin;
  56. }
  57. /**
  58. * @param Controller $controller
  59. * @param string $methodName
  60. *
  61. * @throws NotSubAdminException
  62. */
  63. public function beforeController($controller, $methodName) {
  64. // If AuthorizedAdminSetting, the check will be done in the SecurityMiddleware
  65. if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin && !$this->reflector->hasAnnotation('AuthorizedAdminSetting')) {
  66. throw new NotSubAdminException();
  67. }
  68. }
  69. /**
  70. * @param Controller $controller
  71. * @param string $methodName
  72. * @param \Exception $exception
  73. * @throws \Exception
  74. * @return Response
  75. */
  76. public function afterException($controller, $methodName, \Exception $exception) {
  77. if ($exception instanceof NotSubAdminException) {
  78. throw new OCSException($exception->getMessage(), Http::STATUS_FORBIDDEN);
  79. }
  80. throw $exception;
  81. }
  82. }