mirror of
https://github.com/netzbegruenung/user_saml.git
synced 2024-05-03 09:33:46 +02:00
redirects to homepage instead showing error on blank page
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
d0fb26127d
commit
31bc57a4e9
|
@ -38,7 +38,8 @@ class Application extends App {
|
||||||
$container->registerService('OnlyLoggedInMiddleware', function (IAppContainer $c) {
|
$container->registerService('OnlyLoggedInMiddleware', function (IAppContainer $c) {
|
||||||
return new OnlyLoggedInMiddleware(
|
return new OnlyLoggedInMiddleware(
|
||||||
$c->query('ControllerMethodReflector'),
|
$c->query('ControllerMethodReflector'),
|
||||||
$c->query('ServerContainer')->getUserSession()
|
$c->query('ServerContainer')->getUserSession(),
|
||||||
|
$c->query('ServerContainer')->getUrlGenerator()
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,10 @@
|
||||||
namespace OCA\User_SAML\Middleware;
|
namespace OCA\User_SAML\Middleware;
|
||||||
|
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
|
use OCP\AppFramework\Http\RedirectResponse;
|
||||||
use OCP\AppFramework\Middleware;
|
use OCP\AppFramework\Middleware;
|
||||||
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
||||||
|
use OCP\IURLGenerator;
|
||||||
use OCP\IUserSession;
|
use OCP\IUserSession;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,15 +39,21 @@ class OnlyLoggedInMiddleware extends Middleware {
|
||||||
private $reflector;
|
private $reflector;
|
||||||
/** @var IUserSession */
|
/** @var IUserSession */
|
||||||
private $userSession;
|
private $userSession;
|
||||||
|
/** @var IURLGenerator */
|
||||||
|
private $urlGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IControllerMethodReflector $reflector
|
* @param IControllerMethodReflector $reflector
|
||||||
* @param IUserSession $userSession
|
* @param IUserSession $userSession
|
||||||
*/
|
*/
|
||||||
public function __construct(IControllerMethodReflector $reflector,
|
public function __construct(
|
||||||
IUserSession $userSession) {
|
IControllerMethodReflector $reflector,
|
||||||
|
IUserSession $userSession,
|
||||||
|
IURLGenerator $urlGenerator
|
||||||
|
) {
|
||||||
$this->reflector = $reflector;
|
$this->reflector = $reflector;
|
||||||
$this->userSession = $userSession;
|
$this->userSession = $userSession;
|
||||||
|
$this->urlGenerator = $urlGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,12 +71,12 @@ class OnlyLoggedInMiddleware extends Middleware {
|
||||||
* @param \OCP\AppFramework\Controller $controller
|
* @param \OCP\AppFramework\Controller $controller
|
||||||
* @param string $methodName
|
* @param string $methodName
|
||||||
* @param \Exception $exception
|
* @param \Exception $exception
|
||||||
* @return JSONResponse
|
* @return RedirectResponse
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function afterException($controller, $methodName, \Exception $exception) {
|
public function afterException($controller, $methodName, \Exception $exception) {
|
||||||
if($exception->getMessage() === 'User is already logged-in') {
|
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;
|
throw $exception;
|
||||||
|
|
|
@ -24,10 +24,14 @@ namespace OCA\User_SAML\Tests\Middleware;
|
||||||
use OCA\User_SAML\Middleware\OnlyLoggedInMiddleware;
|
use OCA\User_SAML\Middleware\OnlyLoggedInMiddleware;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
|
use OCP\AppFramework\Http\RedirectResponse;
|
||||||
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
||||||
|
use OCP\IURLGenerator;
|
||||||
use OCP\IUserSession;
|
use OCP\IUserSession;
|
||||||
|
|
||||||
class OnlyLoggedInMiddlewareTest extends \Test\TestCase {
|
class OnlyLoggedInMiddlewareTest extends \Test\TestCase {
|
||||||
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
||||||
|
protected $urlGenerator;
|
||||||
/** @var IControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var IControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $reflector;
|
private $reflector;
|
||||||
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
@ -38,9 +42,11 @@ class OnlyLoggedInMiddlewareTest extends \Test\TestCase {
|
||||||
protected function setUp(): void {
|
protected function setUp(): void {
|
||||||
$this->reflector = $this->createMock(IControllerMethodReflector::class);
|
$this->reflector = $this->createMock(IControllerMethodReflector::class);
|
||||||
$this->userSession = $this->createMock(IUserSession::class);
|
$this->userSession = $this->createMock(IUserSession::class);
|
||||||
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||||
$this->onlyLoggedInMiddleware = new OnlyLoggedInMiddleware(
|
$this->onlyLoggedInMiddleware = new OnlyLoggedInMiddleware(
|
||||||
$this->reflector,
|
$this->reflector,
|
||||||
$this->userSession
|
$this->userSession,
|
||||||
|
$this->urlGenerator
|
||||||
);
|
);
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
@ -101,8 +107,14 @@ class OnlyLoggedInMiddlewareTest extends \Test\TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAfterExceptionWithAlreadyLoggedInException() {
|
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');
|
$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));
|
$this->assertEquals($expected, $this->onlyLoggedInMiddleware->afterException($this->createMock(Controller::class), 'bar', $exception));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue