-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
112 lines (97 loc) · 3.77 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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/Backend for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace PpcAuth;
use PpcAuth\Entity\AuthStorage;
use Zend\Db\Adapter\Adapter;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\ModuleManager;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Mvc\MvcEvent;
class Module implements AutoloaderProviderInterface,
ConfigProviderInterface
{
public function init(ModuleManager $mm)
{
}
public function getAutoloaderConfig()
{
// return array(
// 'Zend\Loader\ClassMapAutoloader' => array(
// __DIR__ . '/autoload_classmap.php',
// ),
// 'Zend\Loader\StandardAutoloader' => array(
// 'namespaces' => array(
// // if we're in a namespace deeper than one level we need to fix the \ in the path
// __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
// ),
// ),
// );
}
public function onBootstrap(MvcEvent $e)
{
// You may not need to do this if you're doing it elsewhere in your
// application
$eventManager = $e->getApplication()->getEventManager();
// $moduleRouteListener = new ModuleRouteListener();
// $moduleRouteListener->attach($eventManager);
// $eventManager->getSharedManager()->attach('Backend\Controller\IndexController', MvcEvent::EVENT_DISPATCH,
// array($this, 'checkAuthenticated'));
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkAuthenticated'));
}
public function getServiceConfig()
{
return array(
'factories' => array(
AuthStorage::class => function ($sm) {
return new AuthStorage('PpcAuth');
},
'AuthService' => function ($sm) {
$dbAdapter = $sm->get(Adapter::class);
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,
'PpcAuthUser', 'username', 'password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($sm->get(AuthStorage::class));
return $authService;
},
),
);
}
public function checkAuthenticated(MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
/** @var AuthenticationService $auth */
$auth = $serviceManager->get('AuthService');
if (!$this->isOpenRequest($e)) {
if (!$auth->hasIdentity()) {
$e->getRouteMatch()
->setParam('__NAMESPACE__', 'PpcAuth')
->setParam('controller', 'PpcAuth\Controller\Index')
->setParam('action', 'login');
} else {
$e->getApplication()->getEventManager()->trigger('auth', $this, compact('identity'));
}
}
}
public function isOpenRequest(MvcEvent $e)
{
$config = $this->getConfig();
$controller = $e->getRouteMatch()->getParam('controller');
if (!in_array($controller, $config['PpcAuth']['auth_controllers'])) {
return true;
}
return false;
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}