1
|
|
|
<?php |
2
|
|
|
namespace Staticus\Auth; |
3
|
|
|
|
4
|
|
|
use Staticus\Acl\Roles; |
5
|
|
|
use Staticus\Config\ConfigInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Zend\Session\ManagerInterface; |
9
|
|
|
use Zend\Session\SessionManager; |
10
|
|
|
use Zend\Stratigility\MiddlewareInterface; |
11
|
|
|
|
12
|
|
|
class AuthSessionMiddleware implements MiddlewareInterface |
13
|
|
|
{ |
14
|
|
|
protected $config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ManagerInterface|SessionManager |
18
|
|
|
*/ |
19
|
|
|
protected $manager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var UserInterface|User |
23
|
|
|
*/ |
24
|
|
|
protected $user; |
25
|
|
|
|
26
|
|
|
public function __construct(ConfigInterface $config, ManagerInterface $manager, UserInterface $user) |
27
|
|
|
{ |
28
|
|
|
$this->config = $config->get('auth.session'); |
29
|
|
|
$this->manager = $manager; |
30
|
|
|
$this->user = $user; |
31
|
|
|
} |
32
|
|
|
public function __invoke( |
|
|
|
|
33
|
|
|
ServerRequestInterface $request, |
34
|
|
|
ResponseInterface $response, |
35
|
|
|
callable $next = null |
36
|
|
|
) |
37
|
|
|
{ |
38
|
|
|
/** @link http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html */ |
39
|
|
|
if (array_key_exists('Zend_Auth', $_SESSION)) { |
40
|
|
|
|
41
|
|
|
/** @var \Zend\Stdlib\ArrayObject $auth */ |
42
|
|
|
$auth = $_SESSION['Zend_Auth']; |
43
|
|
|
if ($auth->offsetExists('storage')) { |
44
|
|
|
|
45
|
|
|
/** @var StdClass $storage */ |
46
|
|
|
$storage = $auth->storage; |
47
|
|
|
if (property_exists($storage, 'user_id')) { |
48
|
|
|
$this->user->login($storage->user_id, [Roles::USER]); |
49
|
|
|
$this->user->setNamespace(UserInterface::NAMESPACES . DIRECTORY_SEPARATOR . $storage->user_id); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $next($request, $response); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: