Added logout via AuthController

main
Igor Scheller 6 years ago committed by msquare
parent 2e51fbff9d
commit 6ed891fc04

@ -11,6 +11,9 @@ $route->get('/', function () {
}); });
$route->get('/credits', 'CreditsController@index'); $route->get('/credits', 'CreditsController@index');
// Authentication
$route->get('/logout', 'AuthController@logout');
// Stats // Stats
$route->get('/metrics', 'Metrics\\Controller@metrics'); $route->get('/metrics', 'Metrics\\Controller@metrics');
$route->get('/stats', 'Metrics\\Controller@stats'); $route->get('/stats', 'Metrics\\Controller@stats');

@ -24,14 +24,6 @@ function register_title()
return __('Register'); return __('Register');
} }
/**
* @return string
*/
function logout_title()
{
return __('Logout');
}
/** /**
* Engel registrieren * Engel registrieren
* *
@ -378,16 +370,6 @@ function entry_required()
return '<span class="text-info glyphicon glyphicon-warning-sign"></span>'; return '<span class="text-info glyphicon glyphicon-warning-sign"></span>';
} }
/**
* @return bool
*/
function guest_logout()
{
session()->invalidate();
redirect(page_link_to('start'));
return true;
}
/** /**
* @return string * @return string
*/ */

@ -0,0 +1,36 @@
<?php
namespace Engelsystem\Controllers;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class AuthController extends BaseController
{
/** @var Response */
protected $response;
/** @var SessionInterface */
protected $session;
/** @var UrlGeneratorInterface */
protected $url;
public function __construct(Response $response, SessionInterface $session, UrlGeneratorInterface $url)
{
$this->response = $response;
$this->session = $session;
$this->url = $url;
}
/**
* @return Response
*/
public function logout()
{
$this->session->invalidate();
return $this->response->redirectTo($this->url->to('/'));
}
}

@ -121,4 +121,27 @@ class Response extends SymfonyResponse implements ResponseInterface
return $new; return $new;
} }
/**
* Return an redirect instance
*
* This method retains the immutability of the message and returns
* an instance with the updated status and headers
*
* @param string $path
* @param int $status
* @param array $headers
* @return Response
*/
public function redirectTo($path, $status = 302, $headers = [])
{
$response = $this->withStatus($status);
$response = $response->withHeader('location', $path);
foreach ($headers as $name => $value) {
$response = $response->withAddedHeader($name, $value);
}
return $response;
}
} }

@ -11,5 +11,6 @@ class UrlGeneratorServiceProvider extends ServiceProvider
$urlGenerator = $this->app->make(UrlGenerator::class); $urlGenerator = $this->app->make(UrlGenerator::class);
$this->app->instance(UrlGenerator::class, $urlGenerator); $this->app->instance(UrlGenerator::class, $urlGenerator);
$this->app->instance('http.urlGenerator', $urlGenerator); $this->app->instance('http.urlGenerator', $urlGenerator);
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
} }
} }

@ -183,10 +183,6 @@ class LegacyMiddleware implements MiddlewareInterface
$title = register_title(); $title = register_title();
$content = guest_register(); $content = guest_register();
return [$title, $content]; return [$title, $content];
case 'logout':
$title = logout_title();
$content = guest_logout();
return [$title, $content];
case 'admin_questions': case 'admin_questions':
$title = admin_questions_title(); $title = admin_questions_title();
$content = admin_questions(); $content = admin_questions();

@ -0,0 +1,42 @@
<?php
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Controllers\AuthController;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGeneratorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class AuthControllerTest extends TestCase
{
/**
* @covers \Engelsystem\Controllers\AuthController::__construct
* @covers \Engelsystem\Controllers\AuthController::logout
*/
public function testLogout()
{
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var SessionInterface|MockObject $session */
$session = $this->getMockForAbstractClass(SessionInterface::class);
/** @var UrlGeneratorInterface|MockObject $url */
$url = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
$session->expects($this->once())
->method('invalidate');
$response->expects($this->once())
->method('redirectTo')
->with('https://foo.bar/');
$url->expects($this->once())
->method('to')
->with('/')
->willReturn('https://foo.bar/');
$controller = new AuthController($response, $session, $url);
$controller->logout();
}
}

@ -1,6 +1,6 @@
<?php <?php
namespace Unit\Controllers; namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Controllers\CreditsController; use Engelsystem\Controllers\CreditsController;

@ -85,4 +85,23 @@ class ResponseTest extends TestCase
$response = new Response(); $response = new Response();
$response->withView('foo'); $response->withView('foo');
} }
/**
* @covers \Engelsystem\Http\Response::redirectTo
*/
public function testRedirectTo()
{
$response = new Response();
$newResponse = $response->redirectTo('http://foo.bar/lorem', 301, ['test' => 'ing']);
$this->assertNotEquals($response, $newResponse);
$this->assertEquals(301, $newResponse->getStatusCode());
$this->assertArraySubset(
[
'location' => ['http://foo.bar/lorem'],
'test' => ['ing'],
],
$newResponse->getHeaders()
);
}
} }

@ -3,6 +3,7 @@
namespace Engelsystem\Test\Unit\Http; namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\UrlGenerator; use Engelsystem\Http\UrlGenerator;
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Http\UrlGeneratorServiceProvider; use Engelsystem\Http\UrlGeneratorServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -25,7 +26,8 @@ class UrlGeneratorServiceProviderTest extends ServiceProviderTest
->method('instance') ->method('instance')
->withConsecutive( ->withConsecutive(
[UrlGenerator::class, $urlGenerator], [UrlGenerator::class, $urlGenerator],
['http.urlGenerator', $urlGenerator] ['http.urlGenerator', $urlGenerator],
[UrlGeneratorInterface::class, $urlGenerator]
); );
$serviceProvider = new UrlGeneratorServiceProvider($app); $serviceProvider = new UrlGeneratorServiceProvider($app);

Loading…
Cancel
Save