-
Notifications
You must be signed in to change notification settings - Fork 2
/
Module.php
executable file
·115 lines (98 loc) · 3.61 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Users;
use Users\Model\UsersTable;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Users\Service\UserMailServices;
use Users\Service\UserEncryption;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
)
)
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\UsersTable' => function ($serviceManager)
{
$dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$table = new UsersTable($dbAdapter);
return $table;
},
'Users\Model\AuthStorage' => function ($serviceManager)
{
return new \Users\Model\AuthStorage('authStorage');
},
'Users\Service\UserMailServices' => function ($serviceManager)
{
return new UserMailServices($serviceManager);
},
'Users\Service\UserEncryption' => function ($serviceManager)
{
return new UserEncryption(null, $serviceManager);
},
'AuthService' => function ($serviceManager)
{
$dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users', 'email', 'password');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($serviceManager->get('Users\Model\AuthStorage'));
return $authService;
}
)
);
}
public function onBootstrap($e)
{
$app = $e->getApplication();
$em = $app->getEventManager();
$sm = $app->getServiceManager();
$config = $sm->get('Config');
$list = $config['whitelist'];
$auth = $sm->get('AuthService');
$em->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth, $sm)
{
$match = $e->getRouteMatch();
// No route match, this is a 404
if (! $match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$name = $sm->get('request')
->getUri()
->getPath();
if (strpos($name, 'reset-password') || in_array($name, $list)) {
return;
}
// User is authenticated
if ($auth->hasIdentity()) {
return;
}
// Redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(), array(
'name' => 'users'
));
$response = $e->getResponse();
$response->getHeaders()
->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, - 100);
}
}