Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
$server->getContentSecurityPolicyManager(),
$server->getCsrfTokenManager(),
$server->getContentSecurityPolicyNonceManager(),
$server->getAppManager()
$server->getAppManager(),
$server->getL10N('lib')
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -35,7 +36,7 @@
* @package OC\AppFramework\Middleware\Security\Exceptions
*/
class NotAdminException extends SecurityException {
public function __construct($message = 'Logged in user must be an admin') {
public function __construct(string $message) {
parent::__construct($message, Http::STATUS_FORBIDDEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCSController;
use OCP\IL10N;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IRequest;
Expand Down Expand Up @@ -87,6 +88,8 @@ class SecurityMiddleware extends Middleware {
private $cspNonceManager;
/** @var IAppManager */
private $appManager;
/** @var IL10N */
private $l10n;

/**
* @param IRequest $request
Expand All @@ -101,6 +104,7 @@ class SecurityMiddleware extends Middleware {
* @param CSRFTokenManager $csrfTokenManager
* @param ContentSecurityPolicyNonceManager $cspNonceManager
* @param IAppManager $appManager
* @param IL10N $l10n
*/
public function __construct(IRequest $request,
ControllerMethodReflector $reflector,
Expand All @@ -113,7 +117,8 @@ public function __construct(IRequest $request,
ContentSecurityPolicyManager $contentSecurityPolicyManager,
CsrfTokenManager $csrfTokenManager,
ContentSecurityPolicyNonceManager $cspNonceManager,
IAppManager $appManager
IAppManager $appManager,
IL10N $l10n
) {
$this->navigationManager = $navigationManager;
$this->request = $request;
Expand All @@ -127,6 +132,7 @@ public function __construct(IRequest $request,
$this->csrfTokenManager = $csrfTokenManager;
$this->cspNonceManager = $cspNonceManager;
$this->appManager = $appManager;
$this->l10n = $l10n;
}

/**
Expand All @@ -152,7 +158,7 @@ public function beforeController($controller, $methodName) {

if(!$this->reflector->hasAnnotation('NoAdminRequired')) {
if(!$this->isAdminUser) {
throw new NotAdminException();
throw new NotAdminException($this->l10n->t('Logged in user must be an admin'));
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions settings/Middleware/SubadminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
use OCP\IL10N;

/**
* Verifies whether an user has at least subadmin rights.
Expand All @@ -42,15 +43,20 @@ class SubadminMiddleware extends Middleware {
protected $isSubAdmin;
/** @var ControllerMethodReflector */
protected $reflector;
/** @var IL10N */
private $l10n;

/**
* @param ControllerMethodReflector $reflector
* @param bool $isSubAdmin
* @param IL10N $l10n
*/
public function __construct(ControllerMethodReflector $reflector,
$isSubAdmin) {
$isSubAdmin,
IL10N $l10n) {
$this->reflector = $reflector;
$this->isSubAdmin = $isSubAdmin;
$this->l10n = $l10n;
}

/**
Expand All @@ -62,7 +68,7 @@ public function __construct(ControllerMethodReflector $reflector,
public function beforeController($controller, $methodName) {
if(!$this->reflector->hasAnnotation('NoSubadminRequired')) {
if(!$this->isSubAdmin) {
throw new NotAdminException('Logged in user must be a subadmin');
throw new NotAdminException($this->l10n->t('Logged in user must be a subadmin'));
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions tests/Settings/Middleware/SubadminMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OC\Settings\Middleware\SubadminMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IL10N;

/**
* Verifies whether an user has at least subadmin rights.
Expand All @@ -31,16 +32,19 @@ class SubadminMiddlewareTest extends \Test\TestCase {
private $reflector;
/** @var Controller */
private $controller;
/** @var IL10N */
private $l10n;

protected function setUp() {
parent::setUp();
$this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
->disableOriginalConstructor()->getMock();
$this->controller = $this->getMockBuilder(Controller::class)
->disableOriginalConstructor()->getMock();
$this->l10n = $this->createMock(IL10N::class);

$this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true);
$this->subadminMiddleware = new SubadminMiddleware($this->reflector, false);
$this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true, $this->l10n);
$this->subadminMiddleware = new SubadminMiddleware($this->reflector, false, $this->l10n);
}

/**
Expand Down Expand Up @@ -86,7 +90,7 @@ public function testBeforeControllerAsSubAdminWithExemption() {
public function testAfterNotAdminException() {
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
$expectedResponse->setStatus(403);
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException()));
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;

class SecurityMiddlewareTest extends \Test\TestCase {
Expand Down Expand Up @@ -82,8 +80,8 @@ class SecurityMiddlewareTest extends \Test\TestCase {
private $cspNonceManager;
/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
private $appManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
private $l10n;

protected function setUp() {
parent::setUp();
Expand All @@ -98,6 +96,7 @@ protected function setUp() {
$this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
$this->cspNonceManager = $this->createMock(ContentSecurityPolicyNonceManager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->l10n = $this->createMock(IL10N::class);
$this->appManager->expects($this->any())
->method('isEnabledForUser')
->willReturn(true);
Expand All @@ -124,7 +123,8 @@ private function getMiddleware($isLoggedIn, $isAdminUser) {
$this->contentSecurityPolicyManager,
$this->csrfTokenManager,
$this->cspNonceManager,
$this->appManager
$this->appManager,
$this->l10n
);
}

Expand Down Expand Up @@ -541,7 +541,7 @@ public function exceptionProvider() {
new CrossSiteRequestForgeryException(),
],
[
new NotAdminException(),
new NotAdminException(''),
],
];
}
Expand Down