From 02d2ffa8e2987c8dacf34a11c2ad789719b0b81d Mon Sep 17 00:00:00 2001 From: Fabio Date: Thu, 15 Jun 2017 16:23:51 +0200 Subject: [PATCH 1/4] Fixes for Laravel Session, reset between requests --- Bootstraps/Laravel.php | 36 +++++++++++++++++++- Bridges/HttpKernel.php | 4 +++ Laravel/SessionGuard.php | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 Laravel/SessionGuard.php diff --git a/Bootstraps/Laravel.php b/Bootstraps/Laravel.php index a0b145c..cade4de 100644 --- a/Bootstraps/Laravel.php +++ b/Bootstraps/Laravel.php @@ -70,7 +70,7 @@ public function getApplication() if (file_exists('bootstrap/start.php')) { $this->app = require_once 'bootstrap/start.php'; $this->app->boot(); - + return $this->app; } @@ -80,6 +80,24 @@ public function getApplication() $kernel = $this->app->make($isLaravel ? 'Illuminate\Contracts\Http\Kernel' : 'Laravel\Lumen\Application'); + $this->app->afterResolving('auth', function($auth) { + $auth->extend('session', function($app, $name, $config) { + $provider = $app['auth']->createUserProvider($config['provider']); + $guard = new \PHPPM\Laravel\SessionGuard($name, $provider, $app['session.store'], null, $app); + $guard->setCookieJar($app['cookie']); + $guard->setDispatcher($app['events']); + $guard->setRequest($app->refresh('request', $guard, 'setRequest')); + + return $guard; + }); + }); + + $app = $this->app; + $this->app->extend('session.store', function() use ($app) { + $manager = $app['session']; + return $manager->driver(); + }); + return $kernel; } @@ -97,5 +115,21 @@ public function preHandle($app) public function postHandle($app) { //reset debugbar if available + + $this->resetProvider('\Illuminate\Cookie\CookieServiceProvider'); + $this->resetProvider('\Illuminate\Session\SessionServiceProvider'); + } + + /** + * @param string $providerName + */ + protected function resetProvider($providerName) + { + if (!$this->app->getProvider($providerName)) + { + return; + } + + $this->app->register($providerName, [], true); } } diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index 5fcc395..f53975e 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -106,6 +106,10 @@ public function handle(ServerRequestInterface $request) if ($this->application instanceof TerminableInterface) { $this->application->terminate($syRequest, $syResponse); } + + if (is_a($this->application, '\Illuminate\Contracts\Http\Kernel')) { + $this->application->terminate($syRequest, $syResponse); + } if ($this->bootstrap instanceof HooksInterface) { $this->bootstrap->postHandle($this->application); diff --git a/Laravel/SessionGuard.php b/Laravel/SessionGuard.php new file mode 100644 index 0000000..773536f --- /dev/null +++ b/Laravel/SessionGuard.php @@ -0,0 +1,73 @@ +name = $name; + $this->session = $session; + $this->request = $request; + $this->provider = $provider; + $this->app = $app; + } + + /** + * Set the current request instance. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * @return $this + */ + public function setRequest(Request $request) + { + // reset the current state + $this->reset(); + + // retrieve a new session from the app + $this->session = $this->app->make('session'); + + return parent::setRequest($request); + } + + /** + * Reset the state of current class instance. + * + * @return void + */ + protected function reset() + { + $this->user = null; + $this->lastAttempted = null; + $this->viaRemember = false; + $this->loggedOut = false; + $this->tokenRetrievalAttempted = false; + } +} From f09d719d809f7a1ed10772f3bb220a53bbfe41f6 Mon Sep 17 00:00:00 2001 From: Fabio Date: Fri, 16 Jun 2017 15:30:33 +0200 Subject: [PATCH 2/4] =?UTF-8?q?using=20=E2=80=98instanceof=E2=80=99=20inst?= =?UTF-8?q?ead=20of=20=E2=80=98is=5Fa()=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bridges/HttpKernel.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index f53975e..c4ea3e8 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse; use Symfony\Component\HttpKernel\TerminableInterface; +use Illuminate\Contracts\Http\Kernel; class HttpKernel implements BridgeInterface @@ -107,7 +108,7 @@ public function handle(ServerRequestInterface $request) $this->application->terminate($syRequest, $syResponse); } - if (is_a($this->application, '\Illuminate\Contracts\Http\Kernel')) { + if ($this->application instanceof Kernel) { $this->application->terminate($syRequest, $syResponse); } From 7e58b1403b39c024b30387d453ae188802535883 Mon Sep 17 00:00:00 2001 From: Fabio Date: Fri, 16 Jun 2017 16:21:42 +0200 Subject: [PATCH 3/4] indent --- Bridges/HttpKernel.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index c4ea3e8..23dabc4 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -107,10 +107,10 @@ public function handle(ServerRequestInterface $request) if ($this->application instanceof TerminableInterface) { $this->application->terminate($syRequest, $syResponse); } - - if ($this->application instanceof Kernel) { - $this->application->terminate($syRequest, $syResponse); - } + + if ($this->application instanceof Kernel) { + $this->application->terminate($syRequest, $syResponse); + } if ($this->bootstrap instanceof HooksInterface) { $this->bootstrap->postHandle($this->application); From 0ba06d65b7fb0e53eaf0b254ca90184da84dd74c Mon Sep 17 00:00:00 2001 From: Cyril Mizzi Date: Wed, 24 Jan 2018 15:05:48 +0100 Subject: [PATCH 4/4] fixes session guard bad indent and rebases on master --- Laravel/SessionGuard.php | 122 +++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/Laravel/SessionGuard.php b/Laravel/SessionGuard.php index 773536f..df08d01 100644 --- a/Laravel/SessionGuard.php +++ b/Laravel/SessionGuard.php @@ -9,65 +9,65 @@ class SessionGuard extends \Illuminate\Auth\SessionGuard { - - /** - * App instance - * - * @var mixed|\Illuminate\Foundation\Application $app - */ - protected $app; - - /** - * Create a new authentication guard. - * - * @param string $name - * @param \Illuminate\Contracts\Auth\UserProvider $provider - * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session - * @param \Symfony\Component\HttpFoundation\Request $request - * @param mixed|\Illuminate\Foundation\Application $app - * @return void - */ - public function __construct($name, - UserProvider $provider, - SessionInterface $session, - Request $request = null, - Application $app) - { - $this->name = $name; - $this->session = $session; - $this->request = $request; - $this->provider = $provider; - $this->app = $app; - } - - /** - * Set the current request instance. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * @return $this - */ - public function setRequest(Request $request) - { - // reset the current state - $this->reset(); - - // retrieve a new session from the app - $this->session = $this->app->make('session'); - - return parent::setRequest($request); - } - - /** - * Reset the state of current class instance. - * - * @return void - */ - protected function reset() - { - $this->user = null; - $this->lastAttempted = null; - $this->viaRemember = false; - $this->loggedOut = false; - $this->tokenRetrievalAttempted = false; - } + + /** + * App instance + * + * @var mixed|\Illuminate\Foundation\Application $app + */ + protected $app; + + /** + * Create a new authentication guard. + * + * @param string $name + * @param \Illuminate\Contracts\Auth\UserProvider $provider + * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session + * @param \Symfony\Component\HttpFoundation\Request $request + * @param mixed|\Illuminate\Foundation\Application $app + * @return void + */ + public function __construct($name, + UserProvider $provider, + SessionInterface $session, + Request $request = null, + Application $app) + { + $this->name = $name; + $this->session = $session; + $this->request = $request; + $this->provider = $provider; + $this->app = $app; + } + + /** + * Set the current request instance. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * @return $this + */ + public function setRequest(Request $request) + { + // reset the current state + $this->reset(); + + // retrieve a new session from the app + $this->session = $this->app->make('session'); + + return parent::setRequest($request); + } + + /** + * Reset the state of current class instance. + * + * @return void + */ + protected function reset() + { + $this->user = null; + $this->lastAttempted = null; + $this->viaRemember = false; + $this->loggedOut = false; + $this->tokenRetrievalAttempted = false; + } }