Showing posts with label password. Show all posts
Showing posts with label password. Show all posts

Thursday, February 16, 2012

Create admin user by code

Here is a piece of code that will allow you to create admin users. It's really useful if you forget the admin password: Create a file called adminuser.php on the same level as index.php (in the root of the application) with the following content:
<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);

umask(0);
Mage::app('default');//if you changed the code for the default store view change it here also
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$username = 'yourUsername';//desired username
$firstname = "Firstname";//desired firstname
$lastname = "Lastname";//desired lastname
$email = "email@example.com";//desired email
$pass = 'yourPaSSWordHere';//desired password
$user = Mage::getModel('admin/user')->load($username, 'username');
if ($user->getId()){
 echo "User {$username} already exists";
 exit;
}
$user->setUsername($username)
  ->setFirstname($firstname)
  ->setLastname($lastname)
  ->setEmail($email)
  ->setPassword($pass)
  ;
$result = $user->validate();
if (is_array($result)){
 foreach ($result as $res){
  echo $res."\n";
 }
 exit;
}
try{
 $user->setForceNewPassword(true);
 $user->save();
 $user->setRoleIds(array(1))->saveRelations();
 echo "User {$username} was created";
 exit;
}
catch (Exception $e){
 echo $e->getMessage();
}
Now call in you browser this url www.mysite.com/adminuser.php. You should see a message on the screen with the result of your action. Cheers, Marius.

Saturday, November 21, 2009

How to reset the passwords for all the customers (for Andrew)

This is a response for: http://www.magentocommerce.com/boards/viewthread/67065/
Hi Andrew You can create a new php file.
Let's call it customer.php and place it on the same level as index.php file af Magento. Put this code in the file.
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
if (is_dir('downloader')) {
header("Location: downloader");
} else {
echo $mageFilename." was not found";
}
exit;
}
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
$passwordLength = 10;
$customers = Mage::getModel('customer/customer')->getCollection();
foreach ($customers as $customer){
    //[Update 2012-04-19: thanks Michael]
    $customer->load($customer->getId());
    //[/Update 2012-04-19]
    $customer->setPassword($customer->generatePassword($passwordLength))->save();
    $customer->sendNewAccountEmail();
}
?>


then just call the page in your browser http://mydomain.com/customer.php After you finish delete the customer.php file from your server to avoid running it again.