php - Authenticate with the Silex Security examples -
having hard time in getting `symfony/security/ work silex 2.0, i'm trying create complete implementation of basic snippets http://silex.sensiolabs.org/doc/master/providers/security.html.
it works without errors, /admin
route alway shows login link , never logout one.
there 3 possible causes: authentication not work, not stored in session, or template not see it.
the full code on github (https://github.com/aoloe/php-silex-demo-security-path). below can find relevant files.
web/index.php
:
<?php error_reporting(e_all); ini_set('display_errors', 1); define('app_basedir', dirname(__dir__)); include_once(app_basedir.'/vendor/autoload.php'); $app = new aoloe\demo\application(); use symfony\component\httpfoundation\request; $app->get('/admin', function(request $request) use ($app) { return $app['twig']->render('admin.twig', [ // 'content' => ($app['security.authorization_checker']->isgranted('role_admin') ? 'logged in' : 'not logged in'), 'content' => 'admin area', ]); }); $app->get('/login', function(request $request) use ($app) { return $app['twig']->render( 'login.twig', [ 'error' => $app['security.last_error']($request), 'last_username' => $app['session']->get('_security.last_username') ] ); })->bind('login'); $app->get('/admin/logout', function(request $request) use ($app) { return $app->redirect($app['url_generator']->generate('home')); }); $app->get('/', function(request $request) use ($app) { return $app['twig']->render('index.twig', [ ]); })->bind('home'); $app->run();
app/application.php
:
<?php namespace aoloe\demo; use \silex\application silexapplication; class application extends silexapplication { public function __construct() { parent::__construct(); $app = $this; $app['debug'] = true; date_default_timezone_set('europe/zurich'); $app['monolog.options'] = [ 'monolog.logfile' => app_basedir.'/var/logs/app.log', 'monolog.name' => 'app', // 'monolog.level' => 300, // = logger::warning ]; $app->register(new \silex\provider\monologserviceprovider(), $app['monolog.options']); $app->register(new \silex\provider\securityserviceprovider()); $app->register(new \silex\provider\sessionserviceprovider()); $app['security.firewalls'] = [ 'admin' => [ 'pattern' => '^/admin/', 'form' => [ 'login_path' => '/login', 'logout' => [ 'logout_path' => '/admin/logout', 'invalidate_session' => true ], 'default_target_path' => '/admin', 'check_path' => '/admin/login_check' ], 'users' => [ 'admin' => ['role_admin', $app['security.default_encoder']->encodepassword('password', '')], ], ], ]; /* $app['security.utils'] = function ($app) { return new \symfony\component\security\http\authentication\authenticationutils($app['request_stack']); }; */ $app->boot(); $app->register(new \silex\provider\twigserviceprovider(), array( 'twig.path' => app_basedir.'/resources/template', )); } }
resources/template/login.twig
:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>title</title> </head> <body> <form action="{{ path('admin_login_check') }}" method="post"> {{ error }} <p>admin/password</p> <input type="text" name="_username" value="{{ last_username }}" /> <input type="password" name="_password" value="" /> <input type="submit" value="login" /> </form> </body> </html>
resources/template/admin.twig
:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>title</title> </head> <body> <h1>admin</h1> <p>{{ content }}</p> <p> {% if is_granted('role_admin') %} <a href="{{ path('logout') }}">logout</a> {% else %} <a href="{{ path('login') }}">login</a> {% endif %} </p> </body> </html>
how can authentication work?
Comments
Post a Comment