2

背景参考参见:Magento:如何让观察者在外部脚本中工作?

我想问从外部脚本“复制”前端控制器操作的首选方法是什么。我正在为 Magento EE 1.12 创建一个外部 SSO 登录。

我的代码在 php 文件中如下所示。您可以通过创建 test.php 并将我的用户 (185) 替换为您的用户 ID 来测试它。导航到该页面,然后再次导航。您会注意到您已登录和退出,但是在管理员中它不会显示您在线。继续阅读...

<?php

    umask(0);
    require_once 'app/Mage.php';

    Mage::app("default");

    // Set the session to frontend according to many suggestions
    Mage::getSingleton("core/session", array("name" => "frontend"));

    // Alan Storm suggestion to load specific area in Magento (frontend)
    Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

    // Load My Specific User
    $user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

    // Get the session object
    $session = Mage::getSingleton('customer/session');

    // Make it look good for debugging
    echo "<PRE>";

    // Toggle the customer logged in / logged out upon page load
    if ($session->isLoggedIn())
    {
        try
        {
            $session->session->setCustomer($user);
            echo "LOGGED IN<br>";
            var_dump($session->getCustomer()->getIsJustConfirmed());
        } catch (Mage_Core_Exception $e) {
            $message = $e->getMessage();
            var_dump($message);
        }

    } else {
        $session->logout();
    }

    var_dump($session->getCustomer());
    echo $session->isLoggedIn() ? $user->getName().' is online!' : 'not logged in';

?>

此代码登录用户,但是没有依赖于 config.xml 中前端区域的 controller_action_predispatch 和 controller_action_postdispatch 事件的控制器的 Mage_Log、Mage_Persistent 或任何其他模块都不会触发。

Mage_Log 是这种情况的一个完美示例,它监视 customer_login 并触发 bindCustomerLogin() 函数(因为我使用了上面 Alan Storm 的建议)但控制器调度没有触发,导致模块无法正常工作。

这些其他模块怎么可能被外部脚本(或观察 controller_front_init_routers 事件的全局观察者)触发?

编辑:解决方案 这是上述 benmarks 建议的最终结果......我正在模拟 Mage_Customer 控制器。下面的脚本演示了如何执行一个完整的 magento 登录。它没有经过广泛测试,但确实显示用户已在后端登录。这是迄今为止我见过的最完整的解决方案。

public function autoMageLogin() {
                // Include the controller itself so the object can be used
                include ('/var/www/app/code/core/Mage/Customer/controllers/AccountController.php');

                // Configure Magento to think its using the frontend
                Mage::getSingleton("core/session", array("name" => "frontend"));
                Mage::getConfig()->init();
                Mage::getConfig()->loadEventObservers('frontend');
                Mage::app()->addEventArea('frontend');
                Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

                // Prepare the request as if it were coming from the specific
                // controller (I've chosed Mage_Customer as my chosen controller
                // to 'emulate' in php code
                $request = Mage::app()->getRequest();
                $request->setRouteName('customer');
                $request->setControllerModule('Mage_Customer');
                $request->setRoutingInfo('');
                $request->setModuleName('customer');
                $request->setControllerName('account');
                $request->setModuleKey('module');
                $request->setControllerKey('account');
                $request->setActionName('loginPost');
                $request->setActionKey('action');


                $response = Mage::app()->getResponse();

                // Instantiate a new AccountController object using the modified request
                // and the modified response (see the include() above)
                $accountControl = new Mage_Customer_AccountController($request, $response);

                // Dispatch events associated to the controller
                Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $accountControl));
                Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' => $accountControl));
                Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' => $accountControl));


                // Load the current user
                $user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

                // Grab the current session
                $session = Mage::getSingleton('customer/session');

                // From this point forward, emulate the controller actions    
                if (!$session->isLoggedIn()){
                        try{

                                $session->setCustomerAsLoggedIn($user);
                                $session->renewSession();
                                echo "LOGGED IN<br>";
                        } catch (Mage_Core_Exception $e) {
                                $message = $e->getMessage();
                                var_dump($message);
                        }

                } else {
                        echo "LOGGED OUT<br>";

                        $session->logout();
                }


                    // Now fire the post controller action events
                    Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=>$accountControl));
                    Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=>$accountControl));
                    Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$accountControl));


                // log to the screen and be done
                var_dump($session->getCustomer());
                echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';

                die();

        }
4

2 回答 2

3

您将需要使用原始参数手动分派事件,例如

Mage::dispatchEvent(
    'controller_action_predispatch',
    array('controller_action',Mage::app()->getRequest()
);

有关更多信息,请参阅Mage_Core_Controller_Varien_Action::preDispatch()(链接)。请注意,pre-dispatch 和 post-dispatch 方法基于 routename 参数调度动态事件,这可能是也可能不是问题。

于 2013-01-07T20:26:13.713 回答
0

如果您将外部脚本重写为自定义控制器和操作,那么所有事件都会自然触发。但是,您首先必须有理由将其设为外部。

于 2013-01-07T21:24:56.437 回答