Create a Magento admin user programmatically

There are times when, as with WordPress, you may need to quickly create an admin user but you’ve no access to the database. We covered how to do this in WordPress here, and now it’s the turn of Magento.

It’s not particularly complicated – a simple script will do the job once uploaded into the web root of the Magento installation, and it should look something this…

Creating The Magento Admin User


First let’s hook into Magento – we’ve assumed that the script is in Magento’s root folder, but if not just tweak the file reference to Mage.php.

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();

Now let’s combine all the details that we need for the user into an array like so, and then save the user :

$user = Mage::getModel('admin/user')->setData(array(
'username'  => 'username goes here',
'firstname' => 'first name of user goes here',
'lastname'    => 'surname of user goes here',
'email'     => 'user email goes here',
'password'  =>'user password goes here',
'is_active' => 1
))->save();

We then need to assign a role to the user – the default administrator’s role has an ID of 1, so we’ll use that – and then save the user again.

$user->setRoleIds(array(1))->setRoleUserId($user->getUserId())->saveRelations();

Finally let’s give ourselves some feedback that the user has been created with no problem.

echo "User has been created successfully!";
?>

And that’s it. It’s probably best not to leave the file hanging around, so be sure to delete it after you’re done!

Want to talk to us about your project?