symfony - Session should logout after 10 min in symfony2 -
i tried far,
path corebundle / dependencyinjection / configuration.php
namespace funstaff\corebundle\dependencyinjection; use symfony\component\config\definition\builder\treebuilder; use symfony\component\config\definition\configurationinterface; class configuration implements configurationinterface { /** * {@inheritdoc} */ public function getconfigtreebuilder() { $treebuilder = new treebuilder(); $rootnode = $treebuilder->root('funstaff_core'); $rootnode ->children() ->scalarnode('timeout')->defaultvalue(900) ->isrequired()->end() ->end(); return $treebuilder; } }
we add code in file funstaffcoreextension.php
namespace funstaff\corebundle\dependencyinjection; use symfony\component\dependencyinjection\containerbuilder; use symfony\component\config\filelocator; use symfony\component\httpkernel\dependencyinjection\extension; use symfony\component\dependencyinjection\loader; class funstaffcoreextension extends extension { /** * {@inheritdoc} */ public function load(array $configs, containerbuilder $container) { $configuration = new configuration(); $config = $this->processconfiguration($configuration, $configs); $loader = new loader\xmlfileloader($container, new filelocator(__dir__.'/../resources/config')); $loader->load('services.yml'); $container->setparameter('core.timeout', $config['timeout']); } }
in config.yml.
funstaff_core: timeout: 600
i created requestlistener.php file in following path: funstaffcorebundle / request / listener.
namespace funstaff\corebundle\request\listener; use symfony\component\eventdispatcher\eventsubscriberinterface; use symfony\component\httpfoundation\session\session; use symfony\component\security\core\securitycontext; use symfony\component\httpkernel\event\getresponseevent; use symfony\component\httpkernel\httpkernelinterface; class requestlistener implements eventsubscriberinterface { protected $session; protected $securitycontext; protected $timeout; /** * construct * * @param session $session */ public function __construct(session $session, securitycontext $securitycontext, $timeout) { $this->session = $session; $this->securitycontext = $securitycontext; $this->timeout = $timeout; } /** * subscribed events * * @return array event list */ public static function getsubscribedevents() { return array( 'kernel.request' => 'onkernelrequest', ); } /** * on kernel request */ public function onkernelrequest(getresponseevent $event) { if (httpkernelinterface::master_request !== $event->getrequesttype()) { return; } $meta = $this->session->getmetadatabag(); $lastused = $meta->getlastused(); if (null !== $lastused && (time() - $lastused) > $this->timeout) { $this->securitycontext->settoken(null); $this->session->invalidate(); } } }
& in resources / config / services.yml
services: timeout.request.listener: class: funstaff\corebundle\request\listener\requestlistener arguments: [ @session, @security.context, %funstaff.timeout% ] tags: - { name: kernel.event_subscriber }
but not working session timeout. left me? please have idea let me know.
i found solution.
create folder handler & in handler create file .php
<?php /* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ namespace my\demobundle\handler; use symfony\component\httpkernel\httpkernelinterface; use symfony\component\httpkernel\event\getresponseevent; use symfony\component\httpfoundation\session\sessioninterface; use symfony\component\routing\routerinterface; use symfony\component\httpfoundation\redirectresponse; use symfony\component\security\core\securitycontextinterface; /** * description of sessionidlehandler * */ class sessionidlehandler { protected $session; protected $securitycontext; protected $router; protected $maxidletime; public function __construct(sessioninterface $session, securitycontextinterface $securitycontext, routerinterface $router, $maxidletime = 0) { $this->session = $session; $this->securitycontext = $securitycontext; $this->router = $router; $this->maxidletime = $maxidletime; } public function onkernelrequest(getresponseevent $event) { if (httpkernelinterface::master_request != $event->getrequesttype()) { return; } if ($this->maxidletime > 0) { $this->session->start(); $lapse = time() - $this->session->getmetadatabag()->getlastused(); if ($lapse > $this->maxidletime) { $this->securitycontext->settoken(null); $this->session->getflashbag()->set('info', 'you have been logged out due inactivity.'); //redirect url logout $event->setresponse(new redirectresponse($this->router->generate('logout'))); } } } }
in service.yml file
services: my.handler.session_idle: class: my\demobundle\handler\sessionidlehandler arguments: ["@session", "@security.context", "@router", %session_max_idle_time%] tags: - { name: kernel.event_listener, event: kernel.request, method: onkernelrequest }
set parameter (in second) in parameter.yml
session_max_idle_time: 600
its works perfectly.
Comments
Post a Comment