1
0
Fork 0
forked from deblan/gist

authentication done

This commit is contained in:
Simon Vieille 2015-11-23 20:28:09 +01:00
parent 51d736cb1a
commit a6f20de8cf
9 changed files with 42 additions and 65 deletions

View file

@ -7,6 +7,7 @@ use Silex\Provider\SessionServiceProvider;
use Gist\Security\AuthenticationProvider;
use Gist\Security\AuthenticationListener;
use Gist\Security\AuthenticationEntryPoint;
use Symfony\Component\Security\Http\HttpUtils;
$app['enable_registration'] = true;
$app['enable_login'] = true;
@ -25,26 +26,22 @@ $app['user.provider'] = $app->share(function ($app) {
$app->register(new SessionServiceProvider());
$app['security.authentication_listener.factory.form_login'] = $app->protect(function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.form_login'] = $app->share(function ($app) {
$app['security.authentication_listener.factory.form'] = $app->protect(function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.form'] = $app->share(function ($app) {
return new AuthenticationProvider($app['user.provider']);
});
$app['security.authentication_listener.'.$name.'.form_login'] = $app->share(function ($app) use ($name) {
$app['security.authentication_listener.'.$name.'.form'] = $app->share(function ($app) use ($name) {
return new AuthenticationListener(
$app['security.token_storage'],
$app['security.authentication_provider.'.$name.'.form_login']
$app['security.authentication_provider.'.$name.'.form']
);
});
$app['security.authentication.entry_point.'.$name.'.form_login'] = $app->share(function ($app) use ($name) {
return new AuthenticationEntryPoint($app['url_generator']);
});
return [
'security.authentication_provider.'.$name.'.form_login',
'security.authentication_listener.'.$name.'.form_login',
'security.authentication.entry_point.'.$name.'.form_login',
'security.authentication_provider.'.$name.'.form',
'security.authentication_listener.'.$name.'.form',
null,
'pre_auth'
];
});
@ -54,15 +51,16 @@ $app->register(
[
'security.firewalls' => [
'default' => [
'pattern' => '^/[a-z]{2}/',
'pattern' => '^/',
'anonymous' => true,
'http' => false,
'form_login' => [
'login_path' => '/login',
'check_path' => '/login_check',
'form' => [
'login_path' => '_login',
'check_path' => '_login_check',
'always_use_default_target_path' => true,
'default_target_path' => $app['url_generator']->generate('my'),
],
'logout' => [
'logout_path' => '/logout'
'path' => '/logout',
],
'users' => $app->share(function () use ($app) {
return $app['user.provider'];

View file

@ -37,8 +37,8 @@ _login:
_login_check:
path: /login_check
_logout:
path: /logout
logout:
path: /my/logout
my:
path: /my

View file

@ -94,20 +94,30 @@ class Controller
{
$app = $this->getApp();
$securityContext = $app['security'];
$securityContext = $app['security.token_storage'];
$securityToken = $securityContext->getToken();
if (!$securityToken) {
return null;
}
return $securityToken->getUser();
$user = $securityToken->getUser();
if (!is_object($user)) {
return null;
}
return $user;
}
public function render($template, array $params)
public function render($template, array $params = null)
{
$app = $this->getApp();
if (null === $params) {
$params = [];
}
if (!isset($params['user'])) {
$params['user'] = $this->getUser();
}

View file

@ -7,6 +7,7 @@ use Gist\Model\User;
use Gist\Form\UserRegisterForm;
use Gist\Form\UserLoginForm;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
/**
* Class LoginController

View file

@ -13,5 +13,7 @@ class MyController extends Controller
public function myAction(Request $request)
{
$app = $this->getApp();
return $this->render('My/my.html.twig');
}
}

View file

@ -0,0 +1,5 @@
{% extends 'base.html.twig' %}
{% block body %}
foo
{% endblock %}

View file

@ -37,14 +37,14 @@
</a>
</li>
{% if user != 'anon.' %}
{% if user %}
<li>
<a href="{{ path('my') }}">
{{ 'app.menu.my.my.title'|trans }}
</a>
</li>
<li>
<a href="{{ path('_logout') }}">
<a href="{{ path('logout') }}">
{{ 'app.menu.my.logout.title'|trans }}
</a>
</li>

View file

@ -1,41 +0,0 @@
<?php
namespace Gist\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
/**
* Class AuthenticationEntryPoint
* @author Simon Vieille <simon@deblan.fr>
*/
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
protected $urlGenerator;
public function __construct(UrlGenerator $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function start(Request $request, AuthenticationException $authException = null)
{
if ($request->isXmlHttpRequest()) {
$response = new Response(json_encode([]), 401);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
if ($authException->getMessage() !== 'Full authentication is required to access this resource.') {
$params = ['error' => 1];
} else {
$params = [];
}
return new RedirectResponse($this->urlGenerator->generate('_login', $params));
}
}

View file

@ -7,8 +7,10 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\HttpFoundation\Request;
/**
* Class AuthenticationListener