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
|
//Module.php
<?php
class Module
{
//....
public function getServiceConfig()
{
return array(
'invokables' => array(
'MyUser\Form\Login' => 'MyUser\Form\Login',
'MyUser\Authentication\Storage\Session' => 'MyUser\Authentication\Storage\Session',
),
'factories' => array(
'MyUserOptions' => function ($sm) {
$config = $sm->get('Config');
return new Options\ModuleOptions(isset($config['MyUser']) ? $config['MyUser'] : array());
},
'MyUserAuthStorage' => function($sm) {
$options = $sm->get('MyUserOptions');
return new \MyUser\Authentication\Storage\Session('sdauth');
},
'MyUserAuthService' => function ($sm) {
$dbAdapter = $sm->get('bigdataSdv1');
$authAdapter = new \MyUser\Authentication\Adapter\DbTable($dbAdapter, 'utilisateurs', 'login', 'password', '?');
//$authAdapter = new \Zend\Authentication\Adapter\DbTable($dbAdapter, 'utilisateurs', 'login', 'password', '?');
$authService = new \Zend\Authentication\AuthenticationService();
$authService->setAdapter($authAdapter);
$authService->setStorage($sm->get('MyUserAuthStorage'));
return $authService;
},
),
);
}
public function getViewHelperConfig()
{
return array(
'factories' => array(
'MyUserDisplayName' => function ($sm) {
$locator = $sm->getServiceLocator();
$viewHelper = new View\Helper\MyUserDisplayName;
$viewHelper->setAuthService($locator->get('MyUserAuthService'));
return $viewHelper;
},
),
);
}
public function onBootstrap(MvcEvent $e)
{
$em = $e->getApplication()->getEventManager();
//Check Authentication
$em->attach('route', array($this, 'checkAuthenticated'));
//Override Layout
//$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', array($this, 'overrideLayout'), 100);
$em->attach('dispatch', array($this, 'overrideLayout'));
}
/**
* Override layout for action "login" and "logout"
* @param MvcEvent $e
*/
public function overrideLayout(MvcEvent $e)
{
//$controller = $e->getTarget();
//$controllerClass = get_class($controller);
//$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$routeMatch = $e->getRouteMatch();
if ( in_array($routeMatch->getParam('action'), array('login','logout') ) ) {
$controller = $e->getTarget();
$controller->layout('layout/layout.phtml');
}
}
/**
* Return true if "login" or "logout" Action is open
* @param MvcEvent $e
* @return boolean
*/
public function isOpenRequest(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
if ( in_array($routeMatch->getParam('action'), array('login','logout') ) ) {
return true;
}
return false;
}
/**
* Check if the user is authenticated
* @param MvcEvent $e
*/
public function checkAuthenticated(MvcEvent $e)
{
if ( !$this->isOpenRequest($e) ) {
$sm = $e->getApplication()->getServiceManager();
if (! $sm->get('MyUserAuthService')->getStorage()->read($sm->get('MyUserAuthService')->getStorage()->getSessionId())) {
$e->getRouteMatch()
->setParam('controller', 'MyUser\Controller\User')
->setParam('action', 'login');
}
}
}
} |
Partager