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::getSingleton("core/session", array("name"=>"adminhtml"));
        $userModel = Mage::getModel("admin/user")->load(1);
        $session = Mage::getSingleton("admin/session");

To switch to admin role before any task. you just need to call this function prior to you code


class Custom_Model_Observer {
     public function my_custom_function() {
          // your code go here

i hope you will find this snippet useful 🙂