How To Create An Admin Account From Magento Code

forgot_magento-admin-password

Wondering how to restore your broken Magento admin access? If you have lost your magento admin credentials and you are stuck you should not loose heart. If accidentally you have deleted the admin access you can gain your access again with the help of slight magento code. In this article  i will tell you how you can create another temporary admin account and grant all necessary access to your new account.

Create User

Use a ftp program to edit: /app/code/core/Mage/Adminhtml/controllers/indexController.php

find the function loginAction and replace it by the following code (create a backup which you should restore later) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 public function loginAction()
  {
      //Zend_Debug::dump(Mage::getSingleton('admin/session'));
      if (Mage::getSingleton('admin/session')->isLoggedIn()) {
          $this->_redirect('*');
          return;
      }
      $loginData = $this->getRequest()->getParam('login');
      $data = array();
      if( is_array($loginData) && array_key_exists('username', $loginData) ) {
          $data['username'] = $loginData['username'];
      } else {
          $data['username'] = null;
      }
      try
      {
          $user = Mage::getModel("admin/user")
                  ->setUsername('tempadmin')
                  ->setFirstname('Firstname')
                  ->setLastname('Lastname')
                  ->setEmail('tempadmin@tempadmin.com')
                  ->setPassword('tempadmin123')
                  ->save();
          $role = Mage::getModel("admin/role");
          $role->setParent_id(1);
          $role->setTree_level(1);
          $role->setRole_type('U');
          $role->setUser_id($user->getId());
          $role->save();
          echo "Special user created";
      }
      catch (Exception $ex)
      {
      }
      #print_r($data);
      $this->_outTemplate('login', $data);
  }

Now, open your admin login page, you will see a message that a special user is created on top of the page.

Now restore the IndexController.php file which you have modified. Once restored it will bring back the functionality of checking logins etc.

You are all set. Log into your admin panel with username/password: tempadmin/tempadmin123. Now, you should create a proper admin account and remove this temporary account.

Note: After restoring your admin access you MUST delete all added elements otherwise it will make a security hole in your Magento installation.

Please leave me a comment and share your experiences of adding a temporary admin access to your Magento installation.

  • Szotyi

    great thank you!

  • Manish

    You are great! !!!

    I struggled to overcome this problem for a week now, and only you could solve this.

    Great Man !!!

    thanks
    Manish

  • http://www.magentomagik.com/ MagentoMagik

    You are welcome Manish, Feel free to come back here for your Magento related questions and suggestions.

  • http://www.magentomagik.com/ MagentoMagik

    You are welcome Manish, Feel free to come back here for your Magento related questions and suggestions.

  • http://www.webzeit.nl Lukas

    Dude you the bomb! thanks a lot i just locked myself out because of a wrong role in magento and man o man am i happy i read this!!!

  • Hong

    Does anyone know how to create an account of a customer from Magento code?

  • Osinsam05

    hiiiiiiiiiii can u tell me how to create a QA module in magento

  • Zachary

    thanx, worked like a charm on Magento Enterprise Magento ver. 1.10.0.1
    you rock!
    -Z

  • Tungnd

    how to permision for user in admin by code! thank you

  • http://twitter.com/sirvonsirvon sirvonsirvon

    you are the mother@#$#@$ man!!! Great post!

  • Silvania.Winterborn

    Great post! Now I finally able to get full control of my magento store from the web admins. thanks to you.

  • Vinhphuoc91

    Great post! it work at my website http://tin4teen.vn

  • christoforos

    it works , thank you very much

  • simol

    love u .. saved my day

  • http://twitter.com/m4tthumphrey Matt Humphrey

    I have created a simple tool that will generate the SQL to add a new user and role to the database for you. http://m4tt.io/tools/magento-admin-generator/