Programmatically create new Magento administrator

Submitted by metaharper - 5 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.

# Create New admin User programmatically.
require_once('app/Mage.php'); // Relative path to Mage.php

try {
    $user = Mage::getModel('admin/user')
	'username'  => 'newadmin',
	'firstname' => 'New',
	'lastname'    => 'Admin',
	'email'     => '[email protected]',
	'password'  =>'password',
	'is_active' => 1

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

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

echo 'User created successfully';

Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean