Friday 30 March 2012

Programmatically Create New Magento Admin User


<?php
/*
 * Create New Admin User
 */


//define USERNAME, EMAIL and PASSWORD below whta ever you want and uncomment this 3 lines (remove # sign)


#define('USERNAME','admin');
#define('EMAIL','xyz@xyz.com');
#define('PASSWORD','admin123');




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' => 'Gz',
'lastname' => 'Chauhan',
'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('Inchoo')
->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__);


?>

create php file like newuser.php and past above code in that file, put newuser.php file in magento root and run script like http://yourdomain.com/newuser.php thats it, now you can login in magento admin using username and password that you enter above code

1 comment:

  1. Hello

    Great Stuff!

    Can you please tell how can we restrict the api resources based on product attribute?

    ReplyDelete