如何在 Zend 框架中从身份验证中排除控制器操作 [英] How to exclude a controller action from authentication in Zend framework

查看:25
本文介绍了如何在 Zend 框架中从身份验证中排除控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法,以便将控制器中的一个特定操作排除在身份验证之外并公开.

I'm trying to find a way so that one specific action in controller is excluded from auth and made public.

出于 API 集成的目的,我需要这样做.

I need this to be done for API integration purposes.

有什么地方可以让我更改或包含任何功能来执行此操作吗?

Is there a place I could look at to change or include any functions to do this?

下面是我在 Bootstrap.php 中的内容

Below is what I have in Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH));

        $options = array(
            'layout'     => 'layout',
            'layoutPath' => APPLICATION_PATH.'/views/layouts/',
        );

        $layout = Zend_Layout::startMvc($options);

        return $moduleLoader;
    }


}

推荐答案

这就是我做我的 ACL 控制器插件的方式,它具有角色 + 资源,其中资源是 {{module}}_{{controller}} => 数组的组合({{actions}})

This is how i do my ACL controller plugin that has roles + resources where resources are combos of {{module}}_{{controller}} => array({{actions}})

<?php
class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // set up acl
        $obj_acl = new Zend_Acl();

        // add the roles
    $obj_acl->addRole(new Zend_Acl_Role('guest'));
    $obj_acl->addRole(new Zend_Acl_Role('member'), 'guest');
    $obj_acl->addRole(new Zend_Acl_Role('admin'), 'member');

        // define all role/resource/actions
        $arr_role_resources = array(
            // role     => array of resources
            'guest'     => array(
                'default_index'                             => array('index', 'about-us', 'testimonials', 'interns', 'staff', 'contact-us'),
                'default_error'                             => array('error', 'denied'),
                'default_account'                           => array('index', 'login', 'register', 'logout', 'forgot-password'),
                'store_index'                               => array('index'),
                'store_category'                            => array('index', 'list', 'view'),
                'store_search'                          => array('index', 'results',),
                'store_product'                             => array('index', 'view', 'ajax-variant'),
                'store_cart'                                    => array('index', 'view', 'empty', 'checkout', 'payment', 'review', 'confirmation', 'apply-coupon'),
                'store-admin_index'                     => array('login')
            ),
            'member'        => array(
                'default_account'                           => array('index', 'me', 'update', 'change-password', 'orders', 'view-order'),
            ),
            'admin' => array(
                'store-admin_index'                     => array('index'),
                'store-admin_category'              => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_customers'             => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_customer-group'    => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_orders'                    => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_product'                   => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_coupon'                    => array('index', 'list', 'create', 'update', 'delete'),
                'store-admin_import'                    => array('index', 'list', 'create', 'update', 'delete'),
            )
        );

        // create a list of registered resources
        $registered_resources = array();

        // add the resources for each role
        foreach($arr_role_resources as $role => $arr_resource)
        {
            foreach($arr_resource as $name_resource => $subset)
            {
                // If the resource hasn't been added add it
                if(!in_array($name_resource, $registered_resources))
                {
                    // register the resource
                    $obj_acl->add(new Zend_Acl_Resource($name_resource));

                    // remember that we registered this resource
                    $registered_resources[] = $name_resource;
                }

                // add the subset of privileges this role has for this resource
            $obj_acl->allow($role, $name_resource, $subset);                
            }
        }

        // Admin can do anything by default
        $obj_acl->allow('admin', null);

        // fetch the current user's role
        $obj_auth = Zend_Auth::getInstance();
        $role       = 'guest';
        if($obj_auth->hasIdentity())
        {
            $role = strtolower($obj_auth->getIdentity()->role);
        }

        try
        {
            // requested resource
            $resource       = $request->module . '_' . $request->controller;
            $action     = $request->action;

            // Check to see if user's role has access to the current resource
            if(!$obj_acl->isAllowed($role, $resource, $action))
            {
                // direct users to an error page
                if($request->module == 'store-admin')
                {
                    $request->setModuleName('store-admin');
              $request->setControllerName('index');
                    $request->setActionName('login');
                }
                else
                {
              $request->setControllerName('error');
                    $request->setActionName('denied');
                }
            }           
        } 
        catch(Zend_Acl_Exception $e) {

            echo $e->getMessage();

            // direct users to an error page
      if($request->module == 'store-admin')
            {
                $request->setModuleName('store-admin');
          $request->setControllerName('index');
                $request->setActionName('login');
            }
            else
            {
          $request->setControllerName('error');
                $request->setActionName('denied');
            }

        }
    }
}

这篇关于如何在 Zend 框架中从身份验证中排除控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆