Zend Framework ACL for Admin section of site



好的,所以我的 zf 应用程序带有用户登录系统,如果凭据在数据库中,它会将人定向到受限区域。但是,我希望使用相同的登录表单来检查用户角色是否为管理员(在数据库中),以及是否将他们定向到管理员部分......我正在努力弄清楚如何使用Zend_Acl做到这一点。ACL 的唯一用途是允许具有管理员角色的用户进入管理员部分,我对采埃孚相当陌生,所以我不确定最好的方法是什么。这是我当前的身份验证控制器代码

http://codepaste.net/6hzydv

谢谢

您可以创建一个控制器插件,该插件将根据用户的角色决定用户是否具有访问控制器/视图的权限。像这样:

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/

阅读 Zend Acl 上的文档,了解如何授予权限并按用户类型继承权限。然后看看这个相当简单的Zend教程在Zend Acl和MVC。

基本上,您只需要执行以下操作:

  1. 在引导程序中的某个位置设置 ACL 规则
  2. 板条箱error/denied.phtml(或您使用的任何分机)
  3. Zend_Controller_Plugin_Acl为你做肮脏的工作

相关内容

  • 没有找到相关文章

最新更新