Magento – Switch To Admin Role Programmatically
Many a tasks can be only performed by the admin user only. The need for automated task on backend is obvious, i faced this issue many times and i’m going to share my solution to you soon. This article will show you how to run task in admin role the right way.
This code will provide admin access. After this code we can perform admin tasks.
<?phpclass Custom_Model_Observer { // call this function to switch to admin mode private function _initAdmin() { Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); Mage::getSingleton("core/session", array("name"=>"adminhtml")); $userModel = Mage::getModel("admin/user")->load(1); $session = Mage::getSingleton("admin/session"); $session->setUser($userModel); }To switch to admin role before any task. you just need to call this function prior to you code
Example
<?php class Custom_Model_Observer { ….. public function my_custom_function() { $this->_initAdmin(); // your code go here }i hope you will find this snippet useful 🙂