Programmatically create new Magento administrator

Submitted by deeperdata - 2 years ago

In the managed services world, often I will be given permission to work on a site only to find out that the site owner's own admin account is lacking certain permissions. This is usually more of a nuisance and less of a roadblock if you have the means to create a new administrator account. Generally speaking I try to restore permissions from within the user's admin account first. Sometimes they've just been restricted from certain features but still have access to changing user roles and permissions. In case you are unable to restore full administrative rights or just don't have the time to worry about such trivial details - this script will generate a new user with full permissions. Use responsibly!

// Upload or create this file in the directory of your choosing. Generally I upload it to root, use it then delete it immediately.

<?php
# Create New admin User programmatically.
require_once('app/Mage.php'); // Relative path to Mage.php
umask(0);
Mage::app();

try {
    $user = Mage::getModel('admin/user')
	->setData(array(
	'username'  => 'newadmin',
	'firstname' => 'New',
	'lastname'    => 'Admin',
	'email'     => 'youremail@domain.com',
	'password'  =>'password',
	'is_active' => 1
))->save();

} catch (Exception $e) {
	echo $e->getMessage();
	exit;
}

//Assign Role Id
try {
	$user->setRoleIds(array(1))  //Administrator role id is 1 ,Here you can assign other roles ids
	->setRoleUserId($user->getUserId())
	->saveRelations();
} catch (Exception $e) {
	echo $e->getMessage();
	exit;
}

echo 'User created successfully';

?>
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean