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) {
|
||||
return new OnlyLoggedInMiddleware(
|
||||
$c->query('ControllerMethodReflector'),
|
||||
$c->query('ServerContainer')->getUserSession()
|
||||
$c->query('ServerContainer')->getUserSession(),
|
||||
$c->query('ServerContainer')->getUrlGenerator()
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue