1

Using Community Edition 1.7.0.2

I found a solution for modifying a customer's password outside of issuing password reset emails.

   $write = Mage::getSingleton('core/resource')->getConnection('core_write');
   $passphrase = "topsecretpassword";
   $salt = "SC";
   $password = md5($salt . $passphrase) . ":SC";

   $write->query("update customer_entity_varchar set value='$password' where entity_id=$customer_id and attribute_id in (select attribute_id from eav_attribute where attribute_code='password_hash' and entity_type=1)");

But I'm not sure where to store this script or how to execute it. I've seen other programmatic solutions similar to this but do not recall the method they used to implement them. Thank you.

2 Answers 2

2

One way to do it is probably to create a shell script. Create a file in shell/ in your Mage root with contents like so:

<?php

require_once('abstract.php');
class QuickFixPass extends Mage_Shell_Abstract
{
    public function run()
    {

        $write = Mage::getSingleton('core/resource')->getConnection('core_write');
        $passphrase = "topsecretpassword";
        $salt = "SC";
        $password = md5($salt . $passphrase) . ":SC";

        $write->query("update customer_entity_varchar set value='$password' where entity_id=$customer_id and attribute_id in (select attribute_id from eav_attribute where attribute_code='password_hash' and entity_type=1)");
    }
}

$shell = new QuickFixPass();
$shell->run();

Then run that shell script:

cd shell && sudo -u www-data php myscript.php

However, in this specific case (and in many others) you should consider using the fine n98-magerun, which has a facility for setting the customer's password.

0
1

The script can be placed inside a .php file; personally I place these files in the scripts/ directory within the magento root, and give it a .htaccess file to prohibit being called via the webserver (for security).

The file will need to bootstrap the Mage class, so the start of the file should contain something fairly similar to this:

<?php
require_once realpath(__DIR__) . "/../app/Mage.php";

if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app('admin')->setUseSessionInUrl(false);

umask(0);

// Begin your script

Once the file exists you can execute it using PHP from the cli with a call such as php scripts/my-script.php

2
  • Isn't much of that work at the top of this script handled for you by shell/abstract.php?
    – kojiro
    Commented Jun 19, 2014 at 20:21
  • 1
    Yep; and abstract.php provides a good base for reusable utility scripts, basically it's your choice--do a little script-style bootstrapping, or extend a class and override methods. For one-off scripts I usually use this approach, but for longer lived tools I add them to shell/ and base them off abstract.php
    – STW
    Commented Jun 19, 2014 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.