|
|
|
|
@ -36,6 +36,8 @@ use OCP\AppFramework\Http\Response; |
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
|
|
|
|
use OCP\AppFramework\Middleware; |
|
|
|
|
use OCP\AppFramework\OCSController; |
|
|
|
|
use OCP\Group\ISubAdmin; |
|
|
|
|
use OCP\IGroupManager; |
|
|
|
|
use OCP\IL10N; |
|
|
|
|
use OCP\INavigationManager; |
|
|
|
|
use OCP\IRequest; |
|
|
|
|
@ -53,6 +55,9 @@ use ReflectionMethod; |
|
|
|
|
* check fails |
|
|
|
|
*/ |
|
|
|
|
class SecurityMiddleware extends Middleware { |
|
|
|
|
private ?bool $isAdminUser = null; |
|
|
|
|
private ?bool $isSubAdmin = null; |
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
|
private IRequest $request, |
|
|
|
|
private ControllerMethodReflector $reflector, |
|
|
|
|
@ -61,8 +66,8 @@ class SecurityMiddleware extends Middleware { |
|
|
|
|
private LoggerInterface $logger, |
|
|
|
|
private string $appName, |
|
|
|
|
private bool $isLoggedIn, |
|
|
|
|
private bool $isAdminUser, |
|
|
|
|
private bool $isSubAdmin, |
|
|
|
|
private IGroupManager $groupManager, |
|
|
|
|
private ISubAdmin $subAdminManager, |
|
|
|
|
private IAppManager $appManager, |
|
|
|
|
private IL10N $l10n, |
|
|
|
|
private AuthorizedGroupMapper $groupAuthorizationMapper, |
|
|
|
|
@ -71,6 +76,22 @@ class SecurityMiddleware extends Middleware { |
|
|
|
|
) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function isAdminUser(): bool { |
|
|
|
|
if ($this->isAdminUser === null) { |
|
|
|
|
$user = $this->userSession->getUser(); |
|
|
|
|
$this->isAdminUser = $user && $this->groupManager->isAdmin($user->getUID()); |
|
|
|
|
} |
|
|
|
|
return $this->isAdminUser; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function isSubAdmin(): bool { |
|
|
|
|
if ($this->isSubAdmin === null) { |
|
|
|
|
$user = $this->userSession->getUser(); |
|
|
|
|
$this->isSubAdmin = $user && $this->subAdminManager->isSubAdmin($user); |
|
|
|
|
} |
|
|
|
|
return $this->isSubAdmin; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This runs all the security checks before a method call. The |
|
|
|
|
* security checks are determined by inspecting the controller method |
|
|
|
|
@ -114,10 +135,10 @@ class SecurityMiddleware extends Middleware { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!$authorized && $this->hasAnnotationOrAttribute($reflectionMethod, 'AuthorizedAdminSetting', AuthorizedAdminSetting::class)) { |
|
|
|
|
$authorized = $this->isAdminUser; |
|
|
|
|
$authorized = $this->isAdminUser(); |
|
|
|
|
|
|
|
|
|
if (!$authorized && $this->hasAnnotationOrAttribute($reflectionMethod, 'SubAdminRequired', SubAdminRequired::class)) { |
|
|
|
|
$authorized = $this->isSubAdmin; |
|
|
|
|
$authorized = $this->isSubAdmin(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!$authorized) { |
|
|
|
|
@ -139,14 +160,14 @@ class SecurityMiddleware extends Middleware { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ($this->hasAnnotationOrAttribute($reflectionMethod, 'SubAdminRequired', SubAdminRequired::class) |
|
|
|
|
&& !$this->isSubAdmin |
|
|
|
|
&& !$this->isAdminUser |
|
|
|
|
&& !$this->isSubAdmin() |
|
|
|
|
&& !$this->isAdminUser() |
|
|
|
|
&& !$authorized) { |
|
|
|
|
throw new NotAdminException($this->l10n->t('Logged in account must be an admin or sub admin')); |
|
|
|
|
} |
|
|
|
|
if (!$this->hasAnnotationOrAttribute($reflectionMethod, 'SubAdminRequired', SubAdminRequired::class) |
|
|
|
|
&& !$this->hasAnnotationOrAttribute($reflectionMethod, 'NoAdminRequired', NoAdminRequired::class) |
|
|
|
|
&& !$this->isAdminUser |
|
|
|
|
&& !$this->isAdminUser() |
|
|
|
|
&& !$authorized) { |
|
|
|
|
throw new NotAdminException($this->l10n->t('Logged in account must be an admin')); |
|
|
|
|
} |
|
|
|
|
|