Skip to content

Instantly share code, notes, and snippets.

@kingharrison
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingharrison/af94678a89dc5818a9c8 to your computer and use it in GitHub Desktop.
Save kingharrison/af94678a89dc5818a9c8 to your computer and use it in GitHub Desktop.
Zf2 ZFCuser currently logged in detection and session timeout support
public function onBootstrap(MvcEvent $e) {
$eventManager = $e->getApplication ()->getEventManager ();
$sharedManager = $eventManager->getSharedManager ();
$sm = $e->getApplication ()->getServiceManager ();
// This checks to see if the user is logged in.
$eventManager->attach ( MvcEvent::EVENT_DISPATCH, array (
$this,
'checkLogin'
), 100 );
}
// This function is attached to a listener to see if the user is not currenlty logged in
// If they are not logged in they will be redirected to the login page. This check will happen through the
// application so there is no need to keep checking in other modules
public function checkLogin(MvcEvent $e) {
$session = new Container ( 'defaults' );
$this->route = $e->getRouteMatch ();
$this->matchedRouteName = explode ( '/', $this->route->getMatchedRouteName () );
$this->route_root = $this->matchedRouteName [0];
$sm = $e->getApplication ()->getServiceManager ();
$config = $sm->get ( 'config' );
$sessionlength = $config ['k3s_settings'] ['session_length'];
$zfcServiceEvents = $sm->get ( 'ZfcUser\Authentication\Adapter\AdapterChain' )->getEventManager ();
$zfcServiceEvents->attach ( 'authenticate', function ($e) use($session) {
$session->offsetSet ( 'sessionstart', $_SERVER ['REQUEST_TIME'] );
} );
$auth = $sm->get ( 'zfcuser_auth_service' );
//If the user isn't logged in and isnt on the login page, take them there
if (! $auth->hasIdentity () && $this->route_root != 'zfcuser') {
$response = new \Zend\Http\PhpEnvironment\Response ();
$response->getHeaders ()->addHeaderLine ( 'Location', '/user/login' );
$response->setStatusCode ( 302 );
$response->sendHeaders ();
$e->stopPropagation ( true );
return $response;
}
//Check to see if the session has timed out and if it has log them out. also check to see it is not the login route so you
//dont end up in a loop
else if ($auth->hasIdentity () && $session->offsetGet ( 'sessionstart' ) < ($_SERVER ['REQUEST_TIME'] - $sessionlength) && $this->route_root != 'zfcuser') {
$response = new \Zend\Http\PhpEnvironment\Response ();
$response->getHeaders ()->addHeaderLine ( 'Location', '/user/logout' );
$response->setStatusCode ( 302 );
$response->sendHeaders ();
$e->stopPropagation ( true );
return $response;
}
//This will set the session start time and also set some values used throughout our system.
else if ($auth->hasIdentity ()) {
$session->offsetSet ( 'sessionstart', $_SERVER ['REQUEST_TIME'] );
$session->offsetSet ( 'defaultbuyr', '02' );
$session->offsetSet ( 'defaultlocn', '01' );
$session->offsetSet ( 'role', 'buyer' );
}
// echo $sm->get('settings);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment