You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

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