Skip to content

Instantly share code, notes, and snippets.

@nikolazic
Created June 5, 2015 16:38
Show Gist options
  • Save nikolazic/da50835f3a194c2f0ed9 to your computer and use it in GitHub Desktop.
Save nikolazic/da50835f3a194c2f0ed9 to your computer and use it in GitHub Desktop.
Add new Magento Admin User
<?php
/**
* Create New Admin User
*
* Uncomment the 3 define lines and fill out the information
*/
//define('USERNAME','admin');
//define('EMAIL','support@gorillagroup.com');
//define('PASSWORD','TyLf%Dax7wD5');
/**
* DO NOT MODIFY BELOW
*/
if(!defined('USERNAME') || !defined('EMAIL') || !defined('PASSWORD')){
echo 'Edit this file and define USERNAME, EMAIL and PASSWORD.';
exit;
}
//load Magento
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}
require_once $mageFilename;
Mage::app();
try {
//create new user write you firstname,lastname
$user = Mage::getModel('admin/user')
->setData(array(
'username' => USERNAME,
'firstname' => 'GEMS',
'lastname' => 'Support',
'email' => EMAIL,
'password' => PASSWORD,
'is_active' => 1
))->save();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
try {
//create new role
$role = Mage::getModel('admin/roles')
->setName('Password Reset')
->setRoleType('G')
->save();
//give "all" privileges to role
Mage::getModel("admin/rules")
->setRoleId($role->getId())
->setResources(array("all"))
->saveRel();
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
exit;
} catch (Exception $e) {
echo 'Error while saving role.';
exit;
}
try {
//assign user to role
$user->setRoleIds(array($role->getId()))
->setRoleUserId($user->getUserId())
->saveRelations();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
echo 'Admin User sucessfully created!<br /><br /><b>THIS FILE WILL NOW TRY TO DELETE ITSELF, BUT PLEASE CHECK TO BE SURE!</b>';
@unlink(__FILE__);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment