Skip to content

Instantly share code, notes, and snippets.

@svenvarkel
Last active January 29, 2019 07:33
Show Gist options
  • Save svenvarkel/6666822 to your computer and use it in GitHub Desktop.
Save svenvarkel/6666822 to your computer and use it in GitHub Desktop.
Code used to create Magento admin user from command line.
#!/usr/bin/env php
<?php
/**
* create_magento_admin_user.php
*
* Place this file to utils folder, one folder
* up from your web root. If your web root is something
* else than htdocs then change the path below accordingly.
*
* This script creates Magento admin user to current
* Magento database.
* Magento has to be installed first!
*
* @author Sven Varkel <[email protected]>
*/
require_once '../htdocs/app/Mage.php';
$mage = Mage::app();
echo "This script creates admin user to current Magento instance\nPlease enter data as asked.\n";
/**
* Prompts for password and returns password string
*
* @return string password
*/
function promptForPassword()
{
$fh = fopen('php://stdin', 'r');
print 'Password: ';
`/bin/stty -echo`;
$password = rtrim(fgets($fh, 64)) or die('Could not read password from input');
`/bin/stty echo`;
print "\n";
fclose($fh);
return $password;
}
$firstname = readline('First name: ');
$lastname = readline('Last name: ');
$email = readline('Please enter your e-mail: ');
$defaultUserName = \strtolower($firstname) . \strtolower($lastname);
$username = readline('Please enter username (default: ' . $defaultUserName . '): ');
if ($username == '')
$username = $defaultUserName;
$password = \promptForPassword();
/**
* @var \Mage_Admin_Model_User
*/
$adminUser = Mage::getModel('Admin/User');
$adminUser = $adminUser->loadByUsername($username);
if (!$adminUser->getId()) {
$adminUser->setUsername($username);
$adminUser->setEmail($email);
$adminUser->setFirstname($firstname);
$adminUser->setLastname($lastname);
$adminUser->setPassword($password);
$adminUser->save();
$userId = $adminUser->getId();
$adminRole = Mage::getModel('Admin/Role');
$adminRole->setUserId($userId);
$adminRole->setParentId(1);
$adminRole->setRoleType('U');
$adminRole->setTreeLevel(2);
$adminRole->setRoleName(ucfirst($adminUser->getUsername()));
$adminRole->save();
echo "Thank you! Your admin account has been created. We sent you e-mail with your admin account details\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment