2014-12-04 08:15:55 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors/**
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-12-04 08:15:55 -05:00
|
|
|
*/
|
2019-09-17 10:33:27 -04:00
|
|
|
namespace OCA\Settings\Middleware;
|
2014-12-04 08:15:55 -05:00
|
|
|
|
|
|
|
|
use OC\AppFramework\Http;
|
2016-04-22 09:28:48 -04:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
|
2014-12-04 08:15:55 -05:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2017-07-26 03:03:04 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
2014-12-04 08:15:55 -05:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
|
use OCP\AppFramework\Middleware;
|
2018-02-26 09:32:17 -05:00
|
|
|
use OCP\IL10N;
|
2014-12-04 08:15:55 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verifies whether an user has at least subadmin rights.
|
2020-06-23 14:18:38 -04:00
|
|
|
* To bypass use the `@NoSubAdminRequired` annotation
|
2014-12-04 08:15:55 -05:00
|
|
|
*/
|
|
|
|
|
class SubadminMiddleware extends Middleware {
|
|
|
|
|
/** @var ControllerMethodReflector */
|
|
|
|
|
protected $reflector;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ControllerMethodReflector $reflector
|
|
|
|
|
* @param bool $isSubAdmin
|
2018-02-26 09:32:17 -05:00
|
|
|
* @param IL10N $l10n
|
2014-12-04 08:15:55 -05:00
|
|
|
*/
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
ControllerMethodReflector $reflector,
|
|
|
|
|
protected $isSubAdmin,
|
|
|
|
|
private IL10N $l10n,
|
|
|
|
|
) {
|
2014-12-04 08:15:55 -05:00
|
|
|
$this->reflector = $reflector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if sharing is enabled before the controllers is executed
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2014-12-04 08:15:55 -05:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2017-08-01 11:32:03 -04:00
|
|
|
public function beforeController($controller, $methodName) {
|
2021-07-22 05:41:29 -04:00
|
|
|
if (!$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->reflector->hasAnnotation('AuthorizedAdminSetting')) {
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!$this->isSubAdmin) {
|
2022-09-21 11:44:32 -04:00
|
|
|
throw new NotAdminException($this->l10n->t('Logged in account must be a subadmin'));
|
2014-12-04 08:15:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return 403 page in case of an exception
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2014-12-04 08:15:55 -05:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @param \Exception $exception
|
|
|
|
|
* @return TemplateResponse
|
2016-02-22 03:41:56 -05:00
|
|
|
* @throws \Exception
|
2014-12-04 08:15:55 -05:00
|
|
|
*/
|
2017-08-01 11:32:03 -04:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception) {
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($exception instanceof NotAdminException) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$response = new TemplateResponse('core', '403', [], 'guest');
|
2016-02-22 03:41:56 -05:00
|
|
|
$response->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw $exception;
|
2014-12-04 08:15:55 -05:00
|
|
|
}
|
|
|
|
|
}
|