2013-12-13 159 views
0

我創建了簡單的Zend身份驗證。一切工作正常...而在我的Bootstrap.php我添加行$objFront->registerPlugin(new My_Controller_Plugin_Acl(), 1);Zend Framework - ACL身份驗證

我的圖書館/我的/控制器/插件/ Acl.php文件看起來

<?php 
// library/My/Controller/Plugin/ACL.php 
class My_Controller_Plugin_ACL extends Zend_Controller_Plugin_Abstract 
{ 
    protected $_defaultRole = 'guest'; 

    public function preDispatch(Zend_Controller_Request_Abstract $request){ 
     $auth = Zend_Auth::getInstance(); 
     $acl = new My_Acl(); 
     $mysession = new Zend_Session_Namespace('mysession'); 

     if($auth->hasIdentity()) { 
      $user = $auth->getIdentity(); 
      if(!$acl->isAllowed($user->role, $request->getControllerName() . '::' . $request->getActionName())) { 
       $mysession->destination_url = $request->getPathInfo(); 

       return Zend_Controller_Action_HelperBroker::getStaticHelper('redirector')->setGotoUrl('auth/noauth'); 
      } 
     } else { 
      if(!$acl->isAllowed($this->_defaultRole, $request->getControllerName() . '::' . $request->getActionName())) { 
       $mysession->destination_url = $request->getPathInfo(); 

       return Zend_Controller_Action_HelperBroker::getStaticHelper('redirector')->setGotoUrl('auth/login'); 
      } 
     } 
    } 
} 

我的圖書館/我的/ Acl.php文件看起來

<?php 
// library/My/Acl.php 
class My_Acl extends Zend_Acl 
{ 
    public function __construct() 
    { 
        // Add a new role called "guest" 
        $this->addRole(new Zend_Acl_Role('guest')); 
        // Add a role called user, which inherits from guest 
        $this->addRole(new Zend_Acl_Role('user'), 'guest'); 
        // Add a role called admin, which inherits from user 
        $this->addRole(new Zend_Acl_Role('admin'), 'user'); 
  
        // Add some resources in the form controller::action 
        $this->add(new Zend_Acl_Resource('error::error')); 
        $this->add(new Zend_Acl_Resource('auth::login')); 
        $this->add(new Zend_Acl_Resource('auth::logout')); 
        $this->add(new Zend_Acl_Resource('index::index')); 
  
        // Allow guests to see the error, login and index pages 
        $this->allow('guest', 'error::error'); 
        $this->allow('guest', 'auth::login'); 
        $this->allow('guest', 'index::index'); 
        // Allow users to access logout and the index action from the user controller 
        $this->allow('user', 'auth::logout'); 
        $this->allow('user', 'user::index'); 
        // Allow admin to access admin controller, index action 
        $this->allow('user', 'admin::index'); 
  
        // You will add here roles, resources and authorization specific to your application, the above are some examples 
    } 
} 

的bootstrap.php文件

<?php 
require_once '../library/Zend/Loader.php'; 
Zend_Loader::registerAutoload(); 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initSession(){ 
     Zend_Session::start(); 
    } 

    protected function _initPlugins() 
    { 
     $autoloader = Zend_Loader_Autoloader::getInstance(); 
     $autoloader->registerNamespace('My_'); 

     $objFront = Zend_Controller_Front::getInstance(); 
     // $objFront->registerPlugin(new My_Controller_Plugin_Acl(), 1); 
     return $objFront; 
    } 
} 

我不知道問題出在哪裏。有人可以檢查我的代碼並給我建議嗎?

謝謝。

+1

你得到什麼錯誤? –

回答

0

的問題是,你沒有

// $objFront->registerPlugin(new My_Controller_Plugin_Acl(), 1); 
的註釋行

類的名稱是

class My_Controller_Plugin_ACL 

通知ACL轉換爲大寫的Acl不喜歡在你的註釋代碼:) 並請下次問你的問題。並告訴我們什麼是問題:)