站点的Zend Framework ACL for Admin部分

时间:2012-04-10 16:04:53

标签: zend-framework zend-acl

好的,所以我的zf应用程序带有用户登录系统,如果凭据在数据库中,则会将人员引导到受限区域。但是,我希望使用相同的登录表单来检查用户角色是否为admin(在数据库中),如果它是直接指向管理部分...我正在努力使用Zend_Acl来解决这个问题。另外ACL的唯一用途是允许具有admin角色的用户进入admin部分,我对ZF相当新,所以我不确定最好的方法是什么..这是我目前的AuthController代码

http://codepaste.net/6hzydv

由于

2 个答案:

答案 0 :(得分:2)

您可以创建一个控制器插件,用于决定用户是否有权根据其角色访问控制器/视图。类似的东西:

class My_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $auth = Zend_Registry::getInstance()->get('auth');
        $acl = new Zend_Acl();

        // for default module
        if ($request->getModuleName() == 'default') {

            // access resources (controllers)
            // usually there will be more access resources
            $acl->add(new Zend_Acl_Resource('index'));
            $acl->add(new Zend_Acl_Resource('error'));

            // access roles
            $acl->addRole(new Zend_Acl_Role('guest'));
            $acl->addRole(new Zend_Acl_Role('user'));
            $acl->addRole(new Zend_Acl_Role('administrator'));

            // access rules
            $acl->allow('guest'); // allow guests everywhere
            $acl->allow('user'); // allow users everywhere
            $acl->allow('administrator'); // allow administrators everywhere

            $role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
            ? $auth->getIdentity()->role : 'guest';
            $controller = $request->getControllerName();
            $action = $request->getActionName();

            if (!$acl->isAllowed($role, $controller, $action)) {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
                $redirector->gotoUrlAndExit('error/denied');
            }

        }
        // for member module
        else if ($request->getModuleName() == 'member') {

            // access resources (controllers)
            // usually there will be more access resources
            $acl->add(new Zend_Acl_Resource('index'));
            $acl->add(new Zend_Acl_Resource('error'));

            // access roles
            $acl->addRole(new Zend_Acl_Role('guest'));
            $acl->addRole(new Zend_Acl_Role('user'));
            $acl->addRole(new Zend_Acl_Role('administrator'));

            // access rules
            $acl->allow('user'); // allow users everywhere
            $acl->allow('administrator'); // allow administrators everywhere

            $role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
            ? $auth->getIdentity()->role : 'guest';
            $controller = $request->getControllerName();
            $action = $request->getActionName();

            if (!$acl->isAllowed($role, $controller, $action)) {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
                $redirector->gotoUrlAndExit('error/denied');
            }

        }
        // for admin module
        else if ($request->getModuleName() == 'admin') {

            // access resources (controllers)
            // usually there will be more access resources
            $acl->add(new Zend_Acl_Resource('index'));
            $acl->add(new Zend_Acl_Resource('error'));

            // access roles
            $acl->addRole(new Zend_Acl_Role('guest'));
            $acl->addRole(new Zend_Acl_Role('user'));
            $acl->addRole(new Zend_Acl_Role('administrator'));

            // access rules
            $acl->allow('administrator'); // allow administrators everywhere

            $role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
            ? $auth->getIdentity()->role : 'guest';
            $controller = $request->getControllerName();
            $action = $request->getActionName();

            if (!$acl->isAllowed($role, $controller, $action)) {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
                $redirector->gotoUrlAndExit('error/denied');
            }

        }
    }
}

您可以在此处查看分步教程:http://blog.richardknop.com/2009/06/user-login-and-authentication-with-zend_auth-and-zend_acl/

答案 1 :(得分:0)

阅读Zend Acl上的文档,了解如何授予权限并按用户类型继承权限。然后公平地查看simple Zend tutorial on Zend Acl & MVC

基本上你只需要做以下事情:

  1. 在引导程序中的某处设置ACL规则
  2. crate error/denied.phtml(或您正在使用的任何分机)
  3. Zend_Controller_Plugin_Acl为你做脏事