2012-07-29 103 views
0

我有安裝Zend_Acl把這樣的工作:Zend_Acl中和Zend_Navigation不能與模塊名工作作爲資源

$acl->addRole(new Zend_Acl_Role('admin')); 
$acl->addRole(new Zend_Acl_Role('user')); 
$acl->add(new Zend_Acl_Resource('frontoffice')); 
$acl->add(new Zend_Acl_Resource('backoffice')); 
$acl->deny('user'); 
$acl->allow('user', null, 'frontoffice'); 
$acl->allow('admin'); 

所以角色「管理員」擁有存取權限的一切,「用戶」只有到frontoffice訪問。 Frontoffice是模塊的名稱,後臺是模塊的名稱。 Acl在自定義插件中檢查:

<?php 
class Custom_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract 
{ 
public function preDispatch(Zend_Controller_Request_Abstract $request) 
{ 
    $loginController = 'auth'; 
    $loginAction  = 'index'; 

    $auth = Zend_Auth::getInstance(); 

    // If user is not logged in and is not requesting login page 
    // - redirect to login page. 
    if (!$auth->hasIdentity() 
      && $request->getControllerName() != $loginController 
      && $request->getActionName()  != $loginAction) { 

     $redirector =  Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector'); 
     $redirector->gotoSimpleAndExit($loginAction, $loginController); 
    } 

    // User is logged in or on login page. 

    if ($auth->hasIdentity()) { 
     // Is logged in 
     // Let's check the credential; 
    $registry = Zend_Registry::getInstance(); 
     $acl = $registry->get('acl'); 

     $identity = $auth->getIdentity(); 
     // role is a column in the user table (database) 
     $isAllowed = $acl->isAllowed($identity->role, null, 
            $request->getModuleName()); 
     if (!$isAllowed) { 
      $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector'); 
      $redirector->gotoUrlAndExit('/'); 
     } 
    } 
} 
} 
?> 

現在,我的資源的名稱是當前模塊的名稱。如果我將acl插入到Zend_Navigation中,並將菜單項的資源設置爲frontoffice,則菜單項會同時消失,以供用戶和管理員使用,但它們都應能夠查看它。這是自舉導航代碼:

protected function _initNavigation() 
{ 
    $this->bootstrap('layout'); 
    $layout = $this->getResource('layout'); 
    $view = $layout->getView(); 
    $navigation = new Zend_Navigation($this->getOption('navigation')); 
    $auth = Zend_Auth::getInstance(); 
    $role = $auth->getIdentity()->role; 
    $view->navigation($navigation)->setAcl(Zend_Registry::get('acl')) 
         ->setRole($role); 
} 

有沒有人有關於如何解決這個問題的建議?提前致謝!

回答

0

據我所知,你不能在ACL中直接定義模塊。您可以使用下面的語法定義每個模塊的每個控制器:

$this->add(new Zend_Acl_Resource("module_name:controller_name"); 
+0

好了,我不希望因爲我有一個簡單的系統,應該只使用模塊名來調節訪問使用該符號。我的Acl通過我的插件工作正常,它只是不適合用Zend_Navigation。 – rbnvrw 2012-07-29 15:53:37