【问题标题】:zend acl Modular implementation?zend acl 模块化实现?
【发布时间】:2012-05-18 14:16:03
【问题描述】:

我正在尝试在我的项目中实现 zend ACL,我面临三个问题。 为了解释问题,这是代码:

我的库插件类

class Mylib_Controller_Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {

    private $_acl = null;
    private $_auth = null;

    public function __construct(Zend_Acl $acl, Zend_Auth $auth) {
        $this->_acl = $acl;
        $this->auth = $auth;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $module = $request->getModuleName();
        $recourse = $request->getControllerName();
        $action = $request->getActionName();
            $identity = $this->auth->getStorage()->read();
    if (!isset($identity->Role)) {

            $role = 'default';
        } else {
        $role = $identity->Role;
        }

        if (!$this->_acl->isAllowed($role, $module, $recourse)) {
            $request->setModuleName('Admin')
                    ->setControllerName('User')
                    ->setActionName('index');
        }


    }

}

这是我在模型文件夹中的 ACL 类

class Application_Model_DbTable_LibraryAcl extends Zend_Acl {

    public function __construct() {
        $this->addRole(new Zend_acl_Role('default'));
        $this->addRole(new Zend_acl_Role('User'));
        $this->addRole(new Zend_acl_Role('Admin'), 'User');



        $this->add(new Zend_Acl_Resource('Admin'))
                ->add(new Zend_Acl_Resource('default'))

        ;



        $this->allow('Admin')
                ->deny(array('User', 'default'));

    }

}

这是 bootstarp 中的 _initAppAutoload

 $acl = new Application_Model_DbTable_LibraryAcl();

        $auth = Zend_Auth::getInstance();


        $fc = Zend_Controller_Front::getInstance();

        $fc->setControllerDirectory(array('default' => '/../application/modules/default/controllers',
            'Admin' => '/../application/modules/Admin/controllers'));
        $fc->registerPlugin(new Hyderlib_Controller_Plugin_AccessCheck($acl, $auth));

1) 第一个问题是如何在 Application_Model_DbTable_LibraryAcl 中指定我有一个带有管理和默认文件夹的模块化实现,或者我如何为每个模块创建资源树?

2)我的数据库中没有默认角色,但我想让这个默认用户在不创建帐户的情况下拥有一些 previligaes(这就是为什么我检查角色的身份,如果没有我设置它默认)。这是最好的做法,甚至是合乎逻辑的吗?

3) 我如何在 _isAllowed 方法中检查 Mylib_Controller_Plugin_AccessCheck 类的操作,而不仅仅是模块和控制器。?

此外,这种重定向方式也给我一个错误,即重定向不正确

【问题讨论】:

    标签: zend-framework default acl roles modular


    【解决方案1】:

    来了,

    1) 第一个问题是如何在 Application_Model_DbTable_LibraryAcl 我有一个 具有管理和默认文件夹的模块化实现,或者我如何为每个文件夹创建资源树 模块?

    对于基本实现,您已经非常接近了:

    class Application_Model_DbTable_LibraryAcl extends Zend_Acl {
    
    public function __construct() {
        //add Roles
        //default role has very limited access
        $this->addRole(new Zend_acl_Role('default'));
        //User inherits all default access
        $this->addRole(new Zend_acl_Role('User'), 'default');
        //Admin inherits all User and Default acces
        $this->addRole(new Zend_acl_Role('Admin'), 'User');
    
        //add resources, caps don't seem to be a problem.
        //add Admin module resource
        $this->add(new Zend_Acl_Resource('admin'));
        //add Admin module Index controller resource, specify admin as parent 
        $this->add(new Zend_Acl_Resource('index'), 'admin');
        //add default module access
        $this->add(new Zend_Acl_Resource('default'));
    
        //add access rules
        //default in Zend_Acl is to deny all
    
        //everyone has access to the front page and the error page
        $this->allow(NULL, 'default', array('index', 'error'));
        $this->allow(NULL, 'default', array('index', 'index'));
    
        //add default user rules
        //allow default access to login logout
        $this->allow('default', 'default', array('login', 'logout'));
    
        //add crud access for User
        $this->allow('User', 'default', array('add', 'update'));
    
        //admin can do all
        $this->allow('Admin', NULL);
      }
    
    }
    

    这可能并不完美,但应该让您知道该怎么做。您可以根据需要测试结果。

    2) 我的数据库中没有默认角色,但我想这样做 默认用户有一些 previligaes 而不创建 帐户(这就是为什么我检查角色的身份,如果没有 我将其设置为默认值)。这是最好的做法,甚至 合乎逻辑?

    对我有用,要检查用户角色真的很难,直到你知道他们是谁。检查一下。

    3) 我如何签入我的 Mylib_Controller_Plugin_AccessCheck 类 该操作的 _isAllowed 方法也不仅仅是模块和 控制器。?

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
            $module   = $request->getModuleName();
            $recourse = $request->getControllerName();
            $action   = $request->getActionName();
                $identity = $this->auth->getStorage()->read();
        if (!isset($identity->Role)) {
                $role = 'default';
            } else {
                $role = $identity->Role;
            }
            //default role is default, if role is not allowed and not set to default send to error controller.
            if (!$this->_acl->isAllowed($role, $module, $recourse, $action)) {
                if ($role == 'default'){
                    $request->setModuleName('default')
                            ->setControllerName('index')
                            ->setActionName('login');
                } else {
                    $request->setModuleName('default')
                            ->setControllerName('error')
                            ->setActionName('noauth');
            }
    

    将动作名称添加到isAllowed() 似乎适用于我的应用程序,但测试并没有非常广泛。所以谨慎使用。我和你一样,仍在努力理解这些概念。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多