Created
March 23, 2013 10:39
-
-
Save anonymous/5227267 to your computer and use it in GitHub Desktop.
Module.php for learning how to redirect to loging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Module Bootstrap | |
* | |
* @package PatrickAdmin | |
*/ | |
namespace PatrickAdmin; | |
use Zend\Mvc\MvcEvent; | |
use Zend\Authentication\AuthenticationService; | |
/** | |
* Module Bootstrap | |
* | |
* @package PatrickAdmin | |
*/ | |
class Module | |
{ | |
public function onBootstrap(MvcEvent $e) { | |
$eventManager = $e->getApplication()->getEventManager(); | |
$application = $e->getApplication(); | |
$sm = $application->getServiceManager(); | |
$membersService = $sm->get('Members'); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$ormconfig = $sm->get('doctrine.configuration.orm_default'); | |
$ormconfig->addFilter("member", "\PatrickCore\Script\ORM\Functional\MemberAccessFilter"); | |
// | |
$currentUser = $membersService->getCurrentUser(); | |
$uid = $currentUser->getId(); | |
$filter = $em->getFilters()->enable("member"); | |
$filter->setParameter('member', $uid); | |
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function($e) { | |
$controller = $e->getTarget(); | |
$auth = new AuthenticationService(); | |
$is_login = $auth->hasIdentity(); | |
//check if action is login | |
$params = $e->getApplication()->getMvcEvent()->getRouteMatch()->getParams(); | |
if ($params['action'] == 'login') { | |
if ($is_login) { | |
return $controller->redirect()->toRoute('adminwithlang/adminindex'); | |
} | |
} else { | |
if (!$is_login) { | |
return $controller->redirect()->toRoute('adminwithlang/adminauthlogin'); | |
} | |
} | |
// } else { | |
// $controller->layout('layout/layout.phtml'); | |
// if (!is_a($controller, 'PatrickAdmin\Controller\AuthController')) { | |
// if (!$is_login) { | |
// return $controller->redirect()->toRoute('adminauthlogin'); | |
// echo "here"; | |
// exit; | |
// } | |
// } else { | |
// $params = $e->getApplication()->getMvcEvent()->getRouteMatch()->getParams(); | |
// if ($params['action'] != "login") { | |
// if (!$is_login) { | |
// return $controller->redirect()->toRoute('adminauthlogin'); | |
// } else { | |
// return $controller->redirect()->toRoute('adminindex'); | |
// } | |
// } else { | |
// if ($is_login) { | |
// return $controller->redirect()->toRoute('adminindex'); | |
// } | |
// } | |
// } | |
}); | |
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, function($e) { | |
$result = $e->getResult(); | |
$result->setTerminal(TRUE); | |
}); | |
} | |
public function getAutoloaderConfig() | |
{ | |
return array( | |
'Zend\Loader\ClassMapAutoloader' => array( | |
__DIR__ . '/autoload_classmap.php', | |
), | |
'Zend\Loader\StandardAutoloader' => array( | |
'namespaces' => array( | |
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, | |
), | |
), | |
); | |
} | |
public function getConfig() | |
{ | |
return include __DIR__ . '/config/module.config.php'; | |
} | |
public function getServiceConfig() | |
{ | |
return array( | |
'factories' => array( | |
'db-adapter' => function($sm) { | |
return $sm->get('db'); | |
}, | |
), | |
); | |
} | |
public function getViewHelperConfig() { | |
return array( | |
'factories' => array( | |
'Member' => function ($serviceManager) { | |
$memberService = $serviceManager->getServiceLocator()->get('Members'); | |
return new \PatrickAdmin\View\Helper\Member($memberService); | |
}, | |
'UpcomingEvents' => function ($serviceManager) { | |
$entitymanager = $serviceManager->getServiceLocator()->get('doctrine.entitymanager.orm_default'); | |
return new \PatrickAdmin\View\Helper\UpcomingEvents($entitymanager); | |
} | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment