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;
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');
}
}
}
* @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;
}
}
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;
}
/**
- * @expectedException \Exception
- * @expectedExceptionMessage Logged in user must be a subadmin
+ * @expectedException \OC\Appframework\Middleware\Security\Exceptions\NotAdminException
*/
public function testBeforeControllerAsUserWithExemption() {
$this->reflector
$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());
}
}