Skip to content

Commit

Permalink
Throw normal exceptions instead of eating them
Browse files Browse the repository at this point in the history
Partially addresses #22550

Replaces #20185
  • Loading branch information
LukasReschke committed Feb 22, 2016
1 parent 8a82097 commit 6c96b3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 10 additions & 4 deletions settings/middleware/subadminmiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OC\Settings\Middleware;

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

throw $exception;
}

}
17 changes: 13 additions & 4 deletions tests/settings/middleware/subadminmiddlewaretest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace OC\Settings\Middleware;

use OC\Appframework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
Expand Down Expand Up @@ -41,8 +42,7 @@ protected function setUp() {
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Logged in user must be a subadmin
* @expectedException \OC\Appframework\Middleware\Security\Exceptions\NotAdminException
*/
public function testBeforeControllerAsUserWithExemption() {
$this->reflector
Expand Down Expand Up @@ -81,9 +81,18 @@ public function testBeforeControllerAsSubAdminWithExemption() {
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
}

public function testAfterException() {
public function testAfterNotAdminException() {
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
$expectedResponse->setStatus(403);
$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->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception()));
$this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
}
}

0 comments on commit 6c96b3d

Please sign in to comment.