diff --git a/app/bootstrap.php.d/19-security.php b/app/bootstrap.php.d/70-security.php similarity index 59% rename from app/bootstrap.php.d/19-security.php rename to app/bootstrap.php.d/70-security.php index 68c1e77..54d44ef 100644 --- a/app/bootstrap.php.d/19-security.php +++ b/app/bootstrap.php.d/70-security.php @@ -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']; diff --git a/app/config/routing.yml b/app/config/routing.yml index 2d8a8e1..c24dbaf 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -37,8 +37,8 @@ _login: _login_check: path: /login_check -_logout: - path: /logout +logout: + path: /my/logout my: path: /my diff --git a/src/Gist/Controller/Controller.php b/src/Gist/Controller/Controller.php index 22330f3..b68ab24 100644 --- a/src/Gist/Controller/Controller.php +++ b/src/Gist/Controller/Controller.php @@ -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(); } diff --git a/src/Gist/Controller/LoginController.php b/src/Gist/Controller/LoginController.php index b7803e1..ea59980 100644 --- a/src/Gist/Controller/LoginController.php +++ b/src/Gist/Controller/LoginController.php @@ -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 diff --git a/src/Gist/Controller/MyController.php b/src/Gist/Controller/MyController.php index a343404..1a980dc 100644 --- a/src/Gist/Controller/MyController.php +++ b/src/Gist/Controller/MyController.php @@ -13,5 +13,7 @@ class MyController extends Controller public function myAction(Request $request) { $app = $this->getApp(); + + return $this->render('My/my.html.twig'); } } diff --git a/src/Gist/Resources/views/My/my.html.twig b/src/Gist/Resources/views/My/my.html.twig new file mode 100644 index 0000000..e228a12 --- /dev/null +++ b/src/Gist/Resources/views/My/my.html.twig @@ -0,0 +1,5 @@ +{% extends 'base.html.twig' %} + +{% block body %} + foo +{% endblock %} diff --git a/src/Gist/Resources/views/base.html.twig b/src/Gist/Resources/views/base.html.twig index 8c4bd07..e0c2c1d 100644 --- a/src/Gist/Resources/views/base.html.twig +++ b/src/Gist/Resources/views/base.html.twig @@ -37,14 +37,14 @@ - {% if user != 'anon.' %} + {% if user %}
  • {{ 'app.menu.my.my.title'|trans }}
  • - + {{ 'app.menu.my.logout.title'|trans }}
  • diff --git a/src/Gist/Security/AuthenticationEntryPoint.php b/src/Gist/Security/AuthenticationEntryPoint.php deleted file mode 100644 index 8a09fda..0000000 --- a/src/Gist/Security/AuthenticationEntryPoint.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ -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)); - } -} diff --git a/src/Gist/Security/AuthenticationListener.php b/src/Gist/Security/AuthenticationListener.php index a4b125d..639fced 100644 --- a/src/Gist/Security/AuthenticationListener.php +++ b/src/Gist/Security/AuthenticationListener.php @@ -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