2009-07-22 24 views
0

我刚刚创建了一个插件,以便与我的网站一起确保用户在执行操作前已通过身份验证。这是插件:异步干扰我的ACL插件

class Booze_Plugin_AclPlugin extends Zend_Controller_Plugin_Abstract{ 

public function preDispatch(Zend_Controller_Request_Abstract $request) 
{ 
    $auth = Booze_Permissions_Auth::getInstance(); 
    $acl = Zend_Registry::get('acl'); 

    if(!$auth->hasIdentity()) 
    { 
     $role = Booze_Permissions_Roles::GUEST; 
    } 
    else 
    { 
     $role = $auth->getUser()->role; 
    } 

    $resource = $request->getControllerName(); 
    $privilege = $request->getActionName(); 

    if(!$acl->isAllowed($role, $resource, $privilege)) 
    { 
     Booze_Log::log("ACLPlugin: Sent to login"); 

     $request->setControllerName('login'); 
     $request->setActionName('index'); 
     $request->setDispatched(false); 
    } 
} 

}

这似乎对于不是异步页工作。不过,我有一个负责执行异步函数的控制器。这是控制器(相关部分):

class AsyncController extends Zend_Controller_Action{ 

public function init() 
{ 

    if(!$this->getRequest()->isXMLHttpRequest()) 
    { 
     $this->_forward('index', 'index'); 
    } 

    $this->_helper->viewRenderer->setNoRender(); 
    $this->_helper->getHelper('layout')->disableLayout(); 
} 

public function addcommentAction() 
{ 
    $params = $this->getDecoded('comment', true); 
    $params_array = (array)$params; 

    $auth = Booze_Permissions_Auth::getInstance(); 
    if(!$auth->hasIdentity()) 
    { 
     $this->getResponse()->setBody("Guest"); 
    } 

    $params_array['user_id'] = $auth->getUser('user_id'); 
    $params_array['store_id'] = Booze_Storage::get('store_id'); 

    if($this->comments->insertComment($params_array)) 
    { 
     $this->getResponse()->setBody("success"); 
    } 
    else 
    { 
     $this->getResponse()->setBody("CommentFail"); 
    } 
} 

当我调用异步函数addcommentAction,作为嘉宾签署情况下,也不会带我到登录控制器。但是,我从萤火虫中发现,它是发送登录控制器的html,但没有显示在我的浏览器中。在我看来,这必须与事实有关,async控制器在init中关闭了视图渲染和布局。我试图搞乱它,并不能找出一个可靠的方法来确保插件中的重定向总是呈现,并显示出来。任何帮助都会非常出色。

回答

0

我想补充的isAllowed这个插件的()部分:

if(!$acl->isAllowed($role, $resource, $privilege)) 
{ 
    if(!$this->getRequest()->isXMLHttpRequest()) 
    { 
     //@todo getResponse 
     $response->setBody(Zend_Json::encode(array('result'=>'fail', 'reason'=>'Need login'); 
     $response->sendResponse(); 
     exit; 
    } 

    Booze_Log::log("ACLPlugin: Sent to login"); 

    $request->setControllerName('login'); 
    $request->setActionName('index'); 
    $request->setDispatched(false); 
} 

比对您的异步页处理incloming JSON时,结果是“失败”。

0

你试过做一个header()重定向吗? 像:

$this->_helper->getHelper('Redirector')->setCode(303) 
             ->setExit(false) 
             ->setGotoSimple('index', 'login'); 

编辑:正如您在您的评论sayd,你就得到正确的HTML(登录) - 无法您在登录操作检查,如果请求是异步,返回一些错误代码你然后处理你的JavaScript请求代码?

+0

嘿,我不知道如何获得帮助插件。当我尝试过它没有工作,因为$这似乎是指插件而不是它制定的控制器。 – Ethan 2009-07-23 16:50:34

+0

@Enghan:`$ redirector = Zend_Controller_Action_HelperBroker :: getStaticHelper('Redirector');` – jason 2009-07-24 15:30:25