diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 3ab8181..a56671e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -38,7 +38,8 @@ class Application extends App { $container->registerService('OnlyLoggedInMiddleware', function (IAppContainer $c) { return new OnlyLoggedInMiddleware( $c->query('ControllerMethodReflector'), - $c->query('ServerContainer')->getUserSession() + $c->query('ServerContainer')->getUserSession(), + $c->query('ServerContainer')->getUrlGenerator() ); }); diff --git a/lib/Middleware/OnlyLoggedInMiddleware.php b/lib/Middleware/OnlyLoggedInMiddleware.php index 23abf1f..6a3bfdf 100644 --- a/lib/Middleware/OnlyLoggedInMiddleware.php +++ b/lib/Middleware/OnlyLoggedInMiddleware.php @@ -22,8 +22,10 @@ namespace OCA\User_SAML\Middleware; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Middleware; use OCP\AppFramework\Utility\IControllerMethodReflector; +use OCP\IURLGenerator; use OCP\IUserSession; /** @@ -37,15 +39,21 @@ class OnlyLoggedInMiddleware extends Middleware { private $reflector; /** @var IUserSession */ private $userSession; + /** @var IURLGenerator */ + private $urlGenerator; /** * @param IControllerMethodReflector $reflector * @param IUserSession $userSession */ - public function __construct(IControllerMethodReflector $reflector, - IUserSession $userSession) { + public function __construct( + IControllerMethodReflector $reflector, + IUserSession $userSession, + IURLGenerator $urlGenerator + ) { $this->reflector = $reflector; $this->userSession = $userSession; + $this->urlGenerator = $urlGenerator; } /** @@ -63,12 +71,12 @@ class OnlyLoggedInMiddleware extends Middleware { * @param \OCP\AppFramework\Controller $controller * @param string $methodName * @param \Exception $exception - * @return JSONResponse + * @return RedirectResponse * @throws \Exception */ public function afterException($controller, $methodName, \Exception $exception) { if($exception->getMessage() === 'User is already logged-in') { - return new JSONResponse('User is already logged-in', 403); + return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/')); } throw $exception; diff --git a/tests/unit/Middleware/OnlyLoggedInMiddlewareTest.php b/tests/unit/Middleware/OnlyLoggedInMiddlewareTest.php index 30daa45..a902fcc 100644 --- a/tests/unit/Middleware/OnlyLoggedInMiddlewareTest.php +++ b/tests/unit/Middleware/OnlyLoggedInMiddlewareTest.php @@ -24,10 +24,14 @@ namespace OCA\User_SAML\Tests\Middleware; use OCA\User_SAML\Middleware\OnlyLoggedInMiddleware; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Utility\IControllerMethodReflector; +use OCP\IURLGenerator; use OCP\IUserSession; class OnlyLoggedInMiddlewareTest extends \Test\TestCase { + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + protected $urlGenerator; /** @var IControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */ private $reflector; /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ @@ -38,9 +42,11 @@ class OnlyLoggedInMiddlewareTest extends \Test\TestCase { protected function setUp(): void { $this->reflector = $this->createMock(IControllerMethodReflector::class); $this->userSession = $this->createMock(IUserSession::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->onlyLoggedInMiddleware = new OnlyLoggedInMiddleware( $this->reflector, - $this->userSession + $this->userSession, + $this->urlGenerator ); parent::setUp(); @@ -101,8 +107,14 @@ class OnlyLoggedInMiddlewareTest extends \Test\TestCase { } public function testAfterExceptionWithAlreadyLoggedInException() { + $homeUrl = 'https://my.nxt.cld/'; + $this->urlGenerator->expects($this->atLeastOnce()) + ->method('getAbsoluteURL') + ->with('/') + ->willReturn($homeUrl); + $exception = new \Exception('User is already logged-in'); - $expected = new JSONResponse('User is already logged-in', 403); + $expected = new RedirectResponse($homeUrl); $this->assertEquals($expected, $this->onlyLoggedInMiddleware->afterException($this->createMock(Controller::class), 'bar', $exception)); } }