April 5, 2015

Magento how to change admin username and password using code?


Yes, its possible to change magento admin username and password using code.

Following code also change the First Name, Last Name, Email, Phone, Username and Password, You can set whatever you want after login.


Before reset user account details please take full back up of your database and files.

Create User

Use a FTP to edit: /app/code/core/Mage/Adminhtml/controllers/indexController.php
And then find the ‘function loginAction{….}’ and replace it by the following .


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.

Important

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

Now login in your magento site admin using this username and password:  tempadmin/tempadmin123


Enjoy It!

2 comments:

The Future of Technology: Emerging Trends to Watch in 2025

Introduction As we navigate through 2025, the technological landscape continues to evolve at an unprecedented pace. Innovations that once se...