你好,我设法在ZF2中实现acl和身份验证,但现在我有两个主要问题。我不能重定向用户后,他是/是不登录(在引导文件),我的另一个任务是做查询mysql,因为我必须检查用户权限,他登录后。下面的代码全部是Module.php。你能帮我吗?到现在为止,我做了登录表单,它工作得很好。(目前没有acl也可以)
namespace Application;
use Zend'Mvc'ModuleRouteListener;
use Zend'Mvc'MvcEvent;
use Zend'ModuleManager'Feature'AutoloaderProviderInterface;
use Zend'Authentication'Storage;
use Zend'Authentication'AuthenticationService;
use Zend'Authentication'Adapter'DbTable as DbTableAuthAdapter;
class Module
{
protected $loginTable;
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this -> initAcl($e);
$e -> getApplication() -> getEventManager() -> attach('route', array($this, 'checkAcl'));
$app = $e->getApplication();
$locator = $app->getServiceManager();
$authAdapter = $locator->get('AuthService');
if($authAdapter->hasIdentity() === true){
//is logged in
}else{
//user is not logged in...redirect to home
}
}
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(
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend'Db'Adapter'Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user', 'username', 'password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
),
);
}
public function initAcl(MvcEvent $e) {
$acl = new 'Zend'Permissions'Acl'Acl();
$roles = include __DIR__ . '/config/module.acl.roles.php';
$allResources = array();
foreach ($roles as $role => $resources) {
$role = new 'Zend'Permissions'Acl'Role'GenericRole($role);
$acl -> addRole($role);
$allResources = array_merge($resources, $allResources);
//adding resources
foreach ($resources as $resource) {
$acl -> addResource(new 'Zend'Permissions'Acl'Resource'GenericResource($resource));
}
//adding restrictions
foreach ($allResources as $resource) {
$acl -> allow($role, $resource);
}
}
//testing
//var_dump($acl->isAllowed('admin','home'));
//true
//setting to view
$e -> getViewModel() -> acl = $acl;
}
public function checkAcl(MvcEvent $e) {
$route = $e -> getRouteMatch() -> getMatchedRouteName();
$userRole = 'guest';
if (!$e -> getViewModel() -> acl -> isAllowed($userRole, $route)) {
$response = $e -> getResponse();
//location to page or what ever
$response -> getHeaders() -> addHeaderLine('Location', $e -> getRequest() -> getBaseUrl() . '/404');
$response -> setStatusCode(303);
}
}
}
下面是我的一些代码示例:
$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');
}
if (!$is_login) {
return $controller->redirect()->toRoute('adminwithlang/adminauthlogin');
}
示例要点:https://gist.github.com/anonymous/5227267
我正在寻找类似的东西;我做了一些调查,发现了以下
在onBootstrap
上附加的函数中获取以下内容$routeMatch = $e->getRouteMatch( );
$controllerParamName = 'Zend'Mvc'ModuleRouteListener::ORIGINAL_CONTROLLER;
$controller = $routeMatch->getParam( $controllerParamName );
$action = $routeMatch->getParam( 'action' );
$route = $routeMatch->getMatchedRouteName( );
检查用户是否已登录;如果不是,则重定向到登录事件
当重定向时,你可以传递这3个变量(controller, action, route)在您的登录事件中,这些将默认为空如果它们被定义了那么在成功登录后你将重定向到controller, action, route的组合
我仍然在写代码,我将发布一旦我成功希望对大家有所帮助