Skip to content

Commit 3fc2c27

Browse files
cmizziandig
authored andcommitted
Fix laravel session handling (#85)
1 parent d91f728 commit 3fc2c27

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

Bootstraps/Laravel.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getApplication()
7070
if (file_exists('bootstrap/start.php')) {
7171
$this->app = require_once 'bootstrap/start.php';
7272
$this->app->boot();
73-
73+
7474
return $this->app;
7575
}
7676

@@ -80,6 +80,24 @@ public function getApplication()
8080

8181
$kernel = $this->app->make($isLaravel ? 'Illuminate\Contracts\Http\Kernel' : 'Laravel\Lumen\Application');
8282

83+
$this->app->afterResolving('auth', function($auth) {
84+
$auth->extend('session', function($app, $name, $config) {
85+
$provider = $app['auth']->createUserProvider($config['provider']);
86+
$guard = new \PHPPM\Laravel\SessionGuard($name, $provider, $app['session.store'], null, $app);
87+
$guard->setCookieJar($app['cookie']);
88+
$guard->setDispatcher($app['events']);
89+
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
90+
91+
return $guard;
92+
});
93+
});
94+
95+
$app = $this->app;
96+
$this->app->extend('session.store', function() use ($app) {
97+
$manager = $app['session'];
98+
return $manager->driver();
99+
});
100+
83101
return $kernel;
84102
}
85103

@@ -97,5 +115,21 @@ public function preHandle($app)
97115
public function postHandle($app)
98116
{
99117
//reset debugbar if available
118+
119+
$this->resetProvider('\Illuminate\Cookie\CookieServiceProvider');
120+
$this->resetProvider('\Illuminate\Session\SessionServiceProvider');
121+
}
122+
123+
/**
124+
* @param string $providerName
125+
*/
126+
protected function resetProvider($providerName)
127+
{
128+
if (!$this->app->getProvider($providerName))
129+
{
130+
return;
131+
}
132+
133+
$this->app->register($providerName, [], true);
100134
}
101135
}

Bridges/HttpKernel.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1818
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
1919
use Symfony\Component\HttpKernel\TerminableInterface;
20+
use Illuminate\Contracts\Http\Kernel;
2021

2122

2223
class HttpKernel implements BridgeInterface
@@ -106,6 +107,10 @@ public function handle(ServerRequestInterface $request)
106107
if ($this->application instanceof TerminableInterface) {
107108
$this->application->terminate($syRequest, $syResponse);
108109
}
110+
111+
if ($this->application instanceof Kernel) {
112+
$this->application->terminate($syRequest, $syResponse);
113+
}
109114

110115
if ($this->bootstrap instanceof HooksInterface) {
111116
$this->bootstrap->postHandle($this->application);

Laravel/SessionGuard.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace PHPPM\Laravel;
4+
5+
use Illuminate\Contracts\Auth\UserProvider;
6+
use Illuminate\Foundation\Application;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9+
10+
class SessionGuard extends \Illuminate\Auth\SessionGuard
11+
{
12+
13+
/**
14+
* App instance
15+
*
16+
* @var mixed|\Illuminate\Foundation\Application $app
17+
*/
18+
protected $app;
19+
20+
/**
21+
* Create a new authentication guard.
22+
*
23+
* @param string $name
24+
* @param \Illuminate\Contracts\Auth\UserProvider $provider
25+
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
26+
* @param \Symfony\Component\HttpFoundation\Request $request
27+
* @param mixed|\Illuminate\Foundation\Application $app
28+
* @return void
29+
*/
30+
public function __construct($name,
31+
UserProvider $provider,
32+
SessionInterface $session,
33+
Request $request = null,
34+
Application $app)
35+
{
36+
$this->name = $name;
37+
$this->session = $session;
38+
$this->request = $request;
39+
$this->provider = $provider;
40+
$this->app = $app;
41+
}
42+
43+
/**
44+
* Set the current request instance.
45+
*
46+
* @param \Symfony\Component\HttpFoundation\Request $request
47+
* @return $this
48+
*/
49+
public function setRequest(Request $request)
50+
{
51+
// reset the current state
52+
$this->reset();
53+
54+
// retrieve a new session from the app
55+
$this->session = $this->app->make('session');
56+
57+
return parent::setRequest($request);
58+
}
59+
60+
/**
61+
* Reset the state of current class instance.
62+
*
63+
* @return void
64+
*/
65+
protected function reset()
66+
{
67+
$this->user = null;
68+
$this->lastAttempted = null;
69+
$this->viaRemember = false;
70+
$this->loggedOut = false;
71+
$this->tokenRetrievalAttempted = false;
72+
}
73+
}

0 commit comments

Comments
 (0)