]> source.dussan.org Git - nextcloud-server.git/commitdiff
Throw normal exceptions instead of eating them
authorLukas Reschke <lukas@owncloud.com>
Mon, 22 Feb 2016 08:41:56 +0000 (09:41 +0100)
committerLukas Reschke <lukas@owncloud.com>
Mon, 22 Feb 2016 08:41:56 +0000 (09:41 +0100)
Partially addresses https://github.com/owncloud/core/issues/22550

Replaces https://github.com/owncloud/core/pull/20185

settings/middleware/subadminmiddleware.php
tests/settings/middleware/subadminmiddlewaretest.php

index 00f221721a626e2ca2f1591405b7f6f633a5ec7f..8e138bdc1a83e267b447daaea2e32566244979b0 100644 (file)
@@ -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;
@@ -58,7 +59,7 @@ class SubadminMiddleware extends 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');
                        }
                }
        }
@@ -69,11 +70,16 @@ class SubadminMiddleware extends Middleware {
         * @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;
        }
 
 }
index d0da19f60e11500bd47a1bf94b44504415c57b09..2b76e4beaa94efb7d0c9f70b457a07fb2533dbf3 100644 (file)
@@ -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;
@@ -41,8 +42,7 @@ class SubadminMiddlewareTest extends \Test\TestCase {
        }
 
        /**
-        * @expectedException \Exception
-        * @expectedExceptionMessage Logged in user must be a subadmin
+        * @expectedException \OC\Appframework\Middleware\Security\Exceptions\NotAdminException
         */
        public function testBeforeControllerAsUserWithExemption() {
                $this->reflector
@@ -81,9 +81,18 @@ class SubadminMiddlewareTest extends \Test\TestCase {
                $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());
        }
 }