Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I trigger controllerless core Magento modules that watch <controller_action_predispatch> in <frontend> in config.xml from external script
    primarykey
    data
    text
    <p>For background reference SEE: <a href="https://stackoverflow.com/questions/2817489/magento-how-do-i-get-observers-to-work-in-an-external-script">Magento: How do I get observers to work in an external script?</a></p> <p>I wanted to ask what the preferred method to 'replicate' a frontend controller's action from an external script is. I am creating an external SSO login for Magento EE 1.12. </p> <p>My code exists as the following in a php file. You can test it by creating test.php and replacing my user (185) with whatever your user ID is. Navigate to the page and then again. You will notice you are logged in and out, however in admin it will not show you as being online. Read on...</p> <pre><code>&lt;?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" =&gt; "frontend")); // Alan Storm suggestion to load specific area in Magento (frontend) Mage::app()-&gt;loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND); // Load My Specific User $user = Mage::getModel('customer/customer')-&gt;load(185)-&gt;setWebsiteId(Mage::app()-&gt;getStore()-&gt;getWebsiteId()); // Get the session object $session = Mage::getSingleton('customer/session'); // Make it look good for debugging echo "&lt;PRE&gt;"; // Toggle the customer logged in / logged out upon page load if ($session-&gt;isLoggedIn()) { try { $session-&gt;session-&gt;setCustomer($user); echo "LOGGED IN&lt;br&gt;"; var_dump($session-&gt;getCustomer()-&gt;getIsJustConfirmed()); } catch (Mage_Core_Exception $e) { $message = $e-&gt;getMessage(); var_dump($message); } } else { $session-&gt;logout(); } var_dump($session-&gt;getCustomer()); echo $session-&gt;isLoggedIn() ? $user-&gt;getName().' is online!' : 'not logged in'; ?&gt; </code></pre> <p>This code logs in the user, however none of the Mage_Log, Mage_Persistent, or any other module <strong>without</strong> a controller that relies on the controller_action_predispatch and controller_action_postdispatch event attached to the frontend area in config.xml will ever fire. </p> <p>Mage_Log is a perfect example of this situation where it watches customer_login and fires the bindCustomerLogin() function (since I'm using Alan Storm's suggestion above) but the controller dispatch does not fire, resulting in failure of the module to work properly.</p> <p>How can these other modules ever possibly be triggered from an external script (or a global observer watching the controller_front_init_routers event)?</p> <p><strong>EDIT: SOLUTION</strong> Here is the final results from the suggestions by benmarks above... I am emulating the Mage_Customer controller. The script below demonstrates how to perform a COMPLETE magento login. It is not extensively tested, but it does show the user as being logged in in the backend. It is the most complete solution i've seen to date.</p> <pre><code>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" =&gt; "frontend")); Mage::getConfig()-&gt;init(); Mage::getConfig()-&gt;loadEventObservers('frontend'); Mage::app()-&gt;addEventArea('frontend'); Mage::app()-&gt;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()-&gt;getRequest(); $request-&gt;setRouteName('customer'); $request-&gt;setControllerModule('Mage_Customer'); $request-&gt;setRoutingInfo(''); $request-&gt;setModuleName('customer'); $request-&gt;setControllerName('account'); $request-&gt;setModuleKey('module'); $request-&gt;setControllerKey('account'); $request-&gt;setActionName('loginPost'); $request-&gt;setActionKey('action'); $response = Mage::app()-&gt;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' =&gt; $accountControl)); Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' =&gt; $accountControl)); Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' =&gt; $accountControl)); // Load the current user $user = Mage::getModel('customer/customer')-&gt;load(185)-&gt;setWebsiteId(Mage::app()-&gt;getStore()-&gt;getWebsiteId()); // Grab the current session $session = Mage::getSingleton('customer/session'); // From this point forward, emulate the controller actions if (!$session-&gt;isLoggedIn()){ try{ $session-&gt;setCustomerAsLoggedIn($user); $session-&gt;renewSession(); echo "LOGGED IN&lt;br&gt;"; } catch (Mage_Core_Exception $e) { $message = $e-&gt;getMessage(); var_dump($message); } } else { echo "LOGGED OUT&lt;br&gt;"; $session-&gt;logout(); } // Now fire the post controller action events Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=&gt;$accountControl)); Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=&gt;$accountControl)); Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=&gt;$accountControl)); // log to the screen and be done var_dump($session-&gt;getCustomer()); echo $session-&gt;isLoggedIn() ? $session-&gt;getCustomer()-&gt;getName().' is online!' : 'not logged in'; die(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload