default locale

This commit is contained in:
Simon Vieille 2015-11-24 18:57:06 +01:00
parent a2daa58502
commit ca4246d3af
7 changed files with 46 additions and 13 deletions

View file

@ -42,10 +42,10 @@ $app['routes'] = $app->share($app->extend('routes', function ($routes, $app) {
*/ */
$app->get('/', function (Request $request) use ($app) { $app->get('/', function (Request $request) use ($app) {
$accept = AcceptHeader::fromString($request->headers->get('Accept-Language')); $accept = AcceptHeader::fromString($request->headers->get('Accept-Language'));
$cookie = $request->cookies->get('locale'); $cookieValue = $request->cookies->get('locale');
if (!empty($cookie) && in_array($cookie, $app['locales'])) { if (!empty($cookieValue) && in_array($cookieValue, $app['locales'])) {
$foundLocale = $cookie; $foundLocale = $cookieValue;
} else { } else {
$foundLocale = $app['translator']->getLocale(); $foundLocale = $app['translator']->getLocale();
@ -64,6 +64,8 @@ $app->get('/', function (Request $request) use ($app) {
}); });
$app->after(function(Request $request, Response $response) use ($app) { $app->after(function(Request $request, Response $response) use ($app) {
$cookie = new Cookie('locale', $request->attributes->get('_locale'), strtotime('+1 month')); $value = $request->cookies->get('locale');
$cookie = new Cookie('locale', $value, strtotime('+1 month'));
$response->headers->setCookie($cookie); $response->headers->setCookie($cookie);
}); });

View file

@ -3,10 +3,11 @@
use Gist\Service\UserProvider; use Gist\Service\UserProvider;
use Silex\Provider\SecurityServiceProvider; use Silex\Provider\SecurityServiceProvider;
use Gist\Service\SaltGenerator; use Gist\Service\SaltGenerator;
use Silex\Provider\SessionServiceProvider;
use Gist\Security\AuthenticationProvider; use Gist\Security\AuthenticationProvider;
use Gist\Security\AuthenticationListener; use Gist\Security\AuthenticationListener;
use Gist\Security\AuthenticationEntryPoint; use Gist\Security\AuthenticationEntryPoint;
use Gist\Security\LogoutSuccessHandler;
use Silex\Provider\SessionServiceProvider;
use Symfony\Component\Security\Http\HttpUtils; use Symfony\Component\Security\Http\HttpUtils;
$app['enable_registration'] = true; $app['enable_registration'] = true;
@ -25,7 +26,6 @@ $app['user.provider'] = $app->share(function ($app) {
$app->register(new SessionServiceProvider()); $app->register(new SessionServiceProvider());
$app['security.authentication_listener.factory.form'] = $app->protect(function ($name, $options) use ($app) { $app['security.authentication_listener.factory.form'] = $app->protect(function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.form'] = $app->share(function ($app) { $app['security.authentication_provider.'.$name.'.form'] = $app->share(function ($app) {
return new AuthenticationProvider($app['user.provider']); return new AuthenticationProvider($app['user.provider']);
@ -37,7 +37,7 @@ $app['security.authentication_listener.factory.form'] = $app->protect(function (
$app['security.authentication_provider.'.$name.'.form'] $app['security.authentication_provider.'.$name.'.form']
); );
}); });
return [ return [
'security.authentication_provider.'.$name.'.form', 'security.authentication_provider.'.$name.'.form',
'security.authentication_listener.'.$name.'.form', 'security.authentication_listener.'.$name.'.form',
@ -45,7 +45,7 @@ $app['security.authentication_listener.factory.form'] = $app->protect(function (
'pre_auth' 'pre_auth'
]; ];
}); });
$app->register( $app->register(
new SecurityServiceProvider(), new SecurityServiceProvider(),
[ [
@ -56,12 +56,11 @@ $app->register(
'form' => [ 'form' => [
'login_path' => '_login', 'login_path' => '_login',
'check_path' => '/login_check', 'check_path' => '/login_check',
'always_use_default_target_path' => true, 'always_use_default_target_path' => false,
'default_target_path' => '/', 'default_target_path' => '/',
], ],
'logout' => [ 'logout' => [
'path' => '/logout', 'path' => '/logout',
'target' => '/',
], ],
'users' => $app->share(function () use ($app) { 'users' => $app->share(function () use ($app) {
return $app['user.provider']; return $app['user.provider'];
@ -73,3 +72,12 @@ $app->register(
] ]
] ]
); );
$app['security.authentication.logout_handler._proto'] = $app->protect(function ($name, $options) use ($app) {
return $app->share(function () use ($name, $options, $app) {
return new LogoutSuccessHandler(
$app['security.http_utils'],
isset($options['target_url']) ? $options['target_url'] : '/'
);
});
});

View file

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

View file

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

View file

@ -34,6 +34,8 @@
<p> <p>
<input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}"> <input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}">
</p> </p>
<input type="hidden" name="_target_path" value="{{ path('my') }}" />
</div> </div>
</div> </div>
</div> </div>

View file

@ -44,7 +44,7 @@
</a> </a>
</li> </li>
<li> <li>
<a href="{{ path('logout') }}"> <a href="{{ path('logout', {target_url: path('home')}) }}">
{{ 'app.menu.my.logout.title'|trans }} {{ 'app.menu.my.logout.title'|trans }}
</a> </a>
</li> </li>

View file

@ -0,0 +1,22 @@
<?php
namespace Gist\Security;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class LogoutSuccessHandler
* @author Simon Vieille <simon@deblan.fr>
*/
class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
public function onLogoutSuccess(Request $request)
{
$targetUrl = $request->query->get('target_url') ? $request->query->get('target_url') : '/';
return new RedirectResponse($targetUrl);
}
}