Zend Framework - ACL身份验证

时间:2013-12-13 19:04:21

标签: zend-framework authentication acl

我创建简单的Zend身份验证。一切都很好......在我的Bootstrap.php中我添加了行$objFront->registerPlugin(new My_Controller_Plugin_Acl(), 1);

我的库/ My / Controller / Plugin / 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');
            }
        }
    }
}

我的库/ My / 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 :(得分:0)

问题在于您所做的注释行

// $objFront->registerPlugin(new My_Controller_Plugin_Acl(), 1);

班级名称是

class My_Controller_Plugin_ACL

注意你的注释代码中的大写ACL不是Acl :) 请下次问你的问题。并告诉我们问题是什么:)