【问题标题】:Admin section in ZendFramework applicationZendFramework 应用程序中的管理部分
【发布时间】:2012-04-24 13:53:46
【问题描述】:

我目前有一个应用程序使用 Zend_Auth 进行用户访问。该站点有一个管理部分,我希望一个在我的数据库中具有管理员角色的用户在使用他的凭据时被允许访问。 Zend_Acl 是唯一的方法吗?因为我想做的事情似乎有点复杂,或者我的问题是否有更简单的解决方案?

我已经考虑过这个问题,我现在想知道是否可以有两个身份验证控制器,一个用于用户,一个用于我的管理部分?

【问题讨论】:

    标签: zend-framework zend-auth zend-acl


    【解决方案1】:

    我最近做了这样的事情。为检查用户凭据的管理模块创建一个前端控制器插件。比如:

    class Admin_Plugin_Auth extends Zend_Controller_Plugin_Abstract
    {    
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            if ($request->getModuleName() != 'admin'){
                return;
            }
            $auth = Zend_Auth::getInstance();
            if (!$auth->hasIdentity()){
                // send him to login
            }
            $user = $auth->getIdentity();
            if (!$user->isAdmin()){ // or however you check
                // send him to a fail page
            }
        }    
    }
    

    【讨论】:

    • 对我来说听起来很合理,所以如果我在“/application”中创建一个名为 Acl.php 的文件,然后在引导程序中注册插件,它将在启动时初始化?
    • 是的,尽管您可以选择放置位置、名称以及注册方式。就个人而言,我的位于管理模块(文件:application/modules/admin/plugins/Auth.php)中,名为Admin_Plugin_Auth。我确保模块 Bootstrap 扩展了Zend_Application_Module_Bootstrap(它注册了一个资源加载器,将文件夹application/modules/admin/plugins 映射到类前缀Admin_Plugin_)。并在模块Bootstrap中注册了插件(文件:application/modules/admin/Bootstrap.php)。
    • 有一天这些信息会对我有所帮助,因为我的应用程序还不需要 acl .. 还是谢谢
    【解决方案2】:

    如果用户是管理员,我决定在我的数据库中使用“is_admin”字段的方法,如果它设置为 1。然后我使用这个代码:

    public function init()
    {
        $this->auth=Zend_Auth::getInstance();
        if ($this->auth->getStorage()->read()->is_admin) {
        $route = array('controller'=>'admin', 'action'=>'index');
        } else {
            $route = array('controller'=>'index', 'action'=>'index');
        $this->_helper->redirector->gotoRoute($route);
        }
    }
    

    如果用户不是管理员,这会将用户从管理区域重定向,如果他们是管理员,则允许他们访问。在我的应用程序中简单使用比 ACL 更容易实现。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多