2012-03-20 46 views
1

我試圖找到一種方法,以便控制器中的一個特定操作排除在身份驗證之外並公開。如何在Zend框架中排除控制器操作與身份驗證

我需要這樣做的API集成的目的。

有沒有一個地方我可以看看改變或包含任何功能來做到這一點?

下面是我在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; 
    } 


} 
+0

Wh在方法你使用默認情況下使其他行動/控制器私人? – 2012-03-20 14:50:41

+0

您是否使用插件來管理認證? – jbrtrnd 2012-03-20 14:52:39

+0

@TimFountain不好意思,但是我需要檢查這個設置的文件?我有應用程序文件夾,但我檢查bootstrap.php? – 2012-03-20 14:58:39

回答

1

這是我如何做到這一點有我的ACL控制插件角色+資源,其中資源是{{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'); 
      } 

     } 
    } 
} 
1

使用擴展Zend_Controller_Plugin_Abstract,然後在插件代碼插件

public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     $controller = $request->controller; 
     $action = $request->action; 
     if (strtolower($controller) == 'zona' && strtolower($action) == 'xml') 
      { 
       return ; 
       } 
     else 
     { 
     //check auth 
     } 
}