HummingbirdUK main logo

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Create a Magento admin user programmatically

Home / Blog / Create a Magento admin user programmatically

Written by Giles Bennett

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...

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!

Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.