2013-03-22 98 views
0

你好,我設法在ZF2中實現了acl和認證,但現在我有兩個主要問題。在他/未登錄後(在引導文件中),我無法重定向用戶,而我的另一個任務是對mysql執行查詢,因爲我必須在登錄後檢查用戶權限。下面的代碼都是Module。 PHP。你可以幫我嗎?到現在我也登錄表單的它工作好(它的工作原理沒有ACL現在)zend框架中的認證+ acl 2

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); 

    } 
} 


} 
+1

這只是一個提示,我不知道你是否知道他們,但有兩個偉大的模塊在那裏做這個。他們被稱爲ZfcUser和BjyAuthorize。你可能想看看他們。看到他們如何工作也可以幫助你解決你的問題。 – Sam 2013-03-22 20:08:34

+1

現在我正在努力學習ZF2,我想獨自完成所有工作。我想了解ZF2的工作原理。這是我的主要想法。我想也許有人找到我的差距並幫助我,之後我可以繼續工作。 – 2013-03-22 20:22:55

回答

0

這裏是我的代碼的一些例子:

$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

+0

或者您可以使用BjyAuthorize + ZFCUSER模塊...我沒有使用它們,因爲性能 – 2013-03-22 21:04:34

+0

此代碼可能會在控制器中工作,但在Module.php中它不起作用。 redirect()是undefined,getParams()也是。我想檢查引導程序中的用戶身份驗證,因爲這將適用於每個模塊。如果我在引導程序中執行此操作,則不需要檢查用戶標識是否在每個模塊中都登錄。 – 2013-03-22 23:36:48

+0

我從我的module.php複製它...這裏是我完整的module.php - > https://gist.github.com/anonymous/5227267 – 2013-03-23 10:40:27

0

我正在尋找類似的東西;我做了一些挖掘和發現下列

讓你在onBootstrap

$routeMatch = $e->getRouteMatch(); 
$controllerParamName = \Zend\Mvc\ModuleRouteListener::ORIGINAL_CONTROLLER; 
$controller = $routeMatch->getParam($controllerParamName); 
$action = $routeMatch->getParam('action'); 
$route = $routeMatch->getMatchedRouteName(); 

檢查連接,如果用戶已經登錄的功能裏面以下;如果你不被重定向到登錄事件

同時重定向你可以通過登錄事件中這3個變量(控制器,動作路線) 這些將默認爲null ,如果他們成功登錄之後定義,那麼你將重定向到控制器,動作的結合,路線

我還在寫代碼,我會發布一次我成功 希望這有助於