SubadminMiddleware.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@statuscode.ch>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Roeland Jago Douma <rullzer@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Settings\Middleware;
  24. use OC\AppFramework\Http;
  25. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  26. use OC\AppFramework\Utility\ControllerMethodReflector;
  27. use OCP\AppFramework\Http\TemplateResponse;
  28. use OCP\AppFramework\Middleware;
  29. /**
  30. * Verifies whether an user has at least subadmin rights.
  31. * To bypass use the `@NoSubadminRequired` annotation
  32. *
  33. * @package OC\Settings\Middleware
  34. */
  35. class SubadminMiddleware extends Middleware {
  36. /** @var bool */
  37. protected $isSubAdmin;
  38. /** @var ControllerMethodReflector */
  39. protected $reflector;
  40. /**
  41. * @param ControllerMethodReflector $reflector
  42. * @param bool $isSubAdmin
  43. */
  44. public function __construct(ControllerMethodReflector $reflector,
  45. $isSubAdmin) {
  46. $this->reflector = $reflector;
  47. $this->isSubAdmin = $isSubAdmin;
  48. }
  49. /**
  50. * Check if sharing is enabled before the controllers is executed
  51. * @param \OCP\AppFramework\Controller $controller
  52. * @param string $methodName
  53. * @throws \Exception
  54. */
  55. public function beforeController($controller, $methodName) {
  56. if(!$this->reflector->hasAnnotation('NoSubadminRequired')) {
  57. if(!$this->isSubAdmin) {
  58. throw new NotAdminException('Logged in user must be a subadmin');
  59. }
  60. }
  61. }
  62. /**
  63. * Return 403 page in case of an exception
  64. * @param \OCP\AppFramework\Controller $controller
  65. * @param string $methodName
  66. * @param \Exception $exception
  67. * @return TemplateResponse
  68. * @throws \Exception
  69. */
  70. public function afterException($controller, $methodName, \Exception $exception) {
  71. if($exception instanceof NotAdminException) {
  72. $response = new TemplateResponse('core', '403', array(), 'guest');
  73. $response->setStatus(Http::STATUS_FORBIDDEN);
  74. return $response;
  75. }
  76. throw $exception;
  77. }
  78. }