-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.php
More file actions
156 lines (131 loc) · 4.6 KB
/
Module.php
File metadata and controls
156 lines (131 loc) · 4.6 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace WdgSimpleAdminBundle;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
class Module
{
protected $whitelist = array('zfcuser/login');
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$sm = $app->getServiceManager();
$config = $sm->get("config");
$list = $config["wdg-simple-admin-bundle"]["auth"]["whitelist"];
$zfcuserOptions = $sm->get('zfcuser_module_options');
$auth = $sm->get('zfcuser_auth_service');
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function($e2) use ($list, $auth, $zfcuserOptions)
{
$route_match = $e2->getRouteMatch();
// No route match, this is a 404 it will get handled normally
if (!$route_match instanceof RouteMatch)
{
return;
}
$is_xhttp = $e2->getRequest()->isXmlHttpRequest();
$route_name = $route_match->getMatchedRouteName();
// Route is whitelisted it does not require authorization
if (in_array($route_name, $list))
{
return;
}
$destination_route = null;
$response_code = null;
if(!$auth->hasIdentity())
{
// We are on a route that requires auth and has no identity
$response_code = 401;
if(!$is_xhttp)
{
//Standard http request redirect to login
$destination_route = 'zfcuser/login';
}
}
else
{
//We have an identity
if(
$zfcuserOptions->getEnableUserState() &&
!in_array($auth->getIdentity()->getState(), $zfcuserOptions->getAllowedLoginStates())
)
{
//User is disabled
$response_code = 403;
if(!$is_xhttp)
{
//Standard http request redirect to login
$destination_route = 'zfcuser/logout';
}
}
// User is authorized just continue
else return;
}
if(!$destination_route)
{
throw new HttpException($response_code, "Unauthorized");
}
// Redirect to the user login page, as an example
$router = $e2->getRouter();
$url = $router->assemble(array(), array(
'name' => $destination_route
));
$response = $e2->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode($response_code);
$response->sendHeaders();
exit;
}, 100);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function getConfig()
{
$config = array();
$configFiles = array(
'module.zfcadmin.config.php',
'module.zfcuser.config.php',
'module.zfcuseradmin.config.php',
'module.config.php',
'routes.config.php',
);
foreach ($configFiles as $configFile)
{
$config = \Zend\Stdlib\ArrayUtils::merge($config, include __DIR__ . '/config/' . $configFile);
}
return $config;
}
/**
* {@InheritDoc}
*/
public function getControllerConfig()
{
return include __DIR__ . '/config/controllers.config.php';
}
/**
* {@InheritDoc}
*/
public function getServiceConfig()
{
$config = array();
$configFiles = array(
'service.config.php',
'form.config.php'
);
foreach ($configFiles as $configFile)
{
$config = \Zend\Stdlib\ArrayUtils::merge($config, include __DIR__ . '/config/' . $configFile);
}
return $config;
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}