From 948a8e549c8535a9ae80dd733c1275f2bd2bb2b6 Mon Sep 17 00:00:00 2001 From: Fabio Date: Thu, 15 Jun 2017 16:23:51 +0200 Subject: [PATCH 1/3] Fixes for Laravel Session, reset between requests --- Bootstraps/Laravel.php | 68 ++++++++++++++++++++++++++++--------- Bridges/HttpKernel.php | 4 +++ Laravel/SessionGuard.php | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 Laravel/SessionGuard.php diff --git a/Bootstraps/Laravel.php b/Bootstraps/Laravel.php index 52e231b..18ae89e 100644 --- a/Bootstraps/Laravel.php +++ b/Bootstraps/Laravel.php @@ -75,23 +75,59 @@ public function getApplication() } $kernel = $this->app->make('Illuminate\Contracts\Http\Kernel'); + + $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; } - - /** - * @param \Illuminate\Contracts\Foundation\Application $app - */ - public function preHandle($app) - { - //reset const LARAVEL_START, to get the correct timing - } - - /** - * @param \Illuminate\Contracts\Foundation\Application $app - */ - public function postHandle($app) - { - //reset debugbar if available - } + + /** + * @param \Illuminate\Contracts\Foundation\Application $app + */ + public function preHandle($app) + { + //reset const LARAVEL_START, to get the correct timing + } + + /** + * @param \Illuminate\Contracts\Foundation\Application $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 eed3a1f..4461e62 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -110,6 +110,10 @@ public function onRequest(ReactRequest $request, HttpResponse $response) 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 aa41a87ed5bd530a5c5ae7083f57099c59018ef7 Mon Sep 17 00:00:00 2001 From: Fabio Date: Fri, 16 Jun 2017 15:30:33 +0200 Subject: [PATCH 2/3] =?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 4461e62..0871997 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -15,6 +15,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 { @@ -111,7 +112,7 @@ public function onRequest(ReactRequest $request, HttpResponse $response) $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 35acd8996a3a70f03854b0fa69a38e72b7867fbd Mon Sep 17 00:00:00 2001 From: Fabio Date: Fri, 16 Jun 2017 16:21:42 +0200 Subject: [PATCH 3/3] indent --- Bridges/HttpKernel.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index 0871997..98917ea 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -111,10 +111,10 @@ public function onRequest(ReactRequest $request, HttpResponse $response) 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);