Throw normal exceptions instead of eating them

Partially addresses https://github.com/owncloud/core/issues/22550

Replaces https://github.com/owncloud/core/pull/20185
This commit is contained in:
Lukas Reschke 2016-02-22 09:41:56 +01:00
parent 8a8209796d
commit 6c96b3d07f
2 changed files with 23 additions and 8 deletions

View File

@ -23,6 +23,7 @@
namespace OC\Settings\Middleware; namespace OC\Settings\Middleware;
use OC\AppFramework\Http; use OC\AppFramework\Http;
use OC\Appframework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Utility\ControllerMethodReflector; use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware; use OCP\AppFramework\Middleware;
@ -58,7 +59,7 @@ class SubadminMiddleware extends Middleware {
public function beforeController($controller, $methodName) { public function beforeController($controller, $methodName) {
if(!$this->reflector->hasAnnotation('NoSubadminRequired')) { if(!$this->reflector->hasAnnotation('NoSubadminRequired')) {
if(!$this->isSubAdmin) { if(!$this->isSubAdmin) {
throw new \Exception('Logged in user must be a subadmin'); throw new NotAdminException('Logged in user must be a subadmin');
} }
} }
} }
@ -69,11 +70,16 @@ class SubadminMiddleware extends Middleware {
* @param string $methodName * @param string $methodName
* @param \Exception $exception * @param \Exception $exception
* @return TemplateResponse * @return TemplateResponse
* @throws \Exception
*/ */
public function afterException($controller, $methodName, \Exception $exception) { public function afterException($controller, $methodName, \Exception $exception) {
if($exception instanceof NotAdminException) {
$response = new TemplateResponse('core', '403', array(), 'guest'); $response = new TemplateResponse('core', '403', array(), 'guest');
$response->setStatus(Http::STATUS_FORBIDDEN); $response->setStatus(Http::STATUS_FORBIDDEN);
return $response; return $response;
} }
throw $exception;
}
} }

View File

@ -10,6 +10,7 @@
namespace OC\Settings\Middleware; namespace OC\Settings\Middleware;
use OC\Appframework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Utility\ControllerMethodReflector; use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
@ -41,8 +42,7 @@ class SubadminMiddlewareTest extends \Test\TestCase {
} }
/** /**
* @expectedException \Exception * @expectedException \OC\Appframework\Middleware\Security\Exceptions\NotAdminException
* @expectedExceptionMessage Logged in user must be a subadmin
*/ */
public function testBeforeControllerAsUserWithExemption() { public function testBeforeControllerAsUserWithExemption() {
$this->reflector $this->reflector
@ -81,9 +81,18 @@ class SubadminMiddlewareTest extends \Test\TestCase {
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
} }
public function testAfterException() { public function testAfterNotAdminException() {
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest'); $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
$expectedResponse->setStatus(403); $expectedResponse->setStatus(403);
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception())); $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException()));
}
/**
* @expectedException \Exception
*/
public function testAfterRegularException() {
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
$expectedResponse->setStatus(403);
$this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
} }
} }