Magento – magento 1.9 save password both md5 and salt

magento-1.9password

I have 2 web site, one is Magento and other is a web APP, the second system uses md5 , therefore we need to save the md5 password and export to web App(unfortunately we can not change the web app to use salt) , I have tried to save the password in md5 but in some cases is not generated, here is my code:

in app/code/local/MAE/config.xml

        <customer_save_before>
            <observers>
                <md5password>
                    <class>membership/observer</class>
                    <method>storeMD5Password</method>
                </md5password>
            </observers>
        </customer_save_before>

in app/code/local/MAE/membership/Observer.php

public function storeMD5Password($observer)
{
    $event = $observer->getEvent();
    $customer = $event->getCustomer();
    $postData = Mage::app()->getRequest()->getPost();

    if ($customer instanceof Mage_Customer_Model_Customer) {
        if (isset($postData['password'])) {
            $customer->setData('md5_password', md5($postData['password']));
        }
    }
}

but in some strange cases, it is not saving the password,

here are the cases which i tested and it works:

  • registration page
  • change password in my account
  • buying products and register(but looks like there are some failure in this part

probably it better to force to save by every login like if user successfully log in with user name and password (salt), the system update the md5 password. please let me know how to do that! or if you have any better idea.

Best Answer

Actually i think it should be enough to update the password

  • when a customer signs up via register
  • when a customer signs up during checkout
  • after a password change in the customer acount
  • after a password change from the Magento backend

The password should not change inbetween so I think updating it on every login is not necessary.

Regarding the "strange cases" where it's not saving the password. I'm not completely sure what happens here but maybe it helps to check if the password is not empty before you update the attribute (with maybe an empty value).

if (isset($postData['password'])) {
    $customer->setData('md5_password', md5($postData['password']));
}

could be updated to:

if (isset($postData['password']) && $postData['password']!='') {
    $customer->setData('md5_password', md5($postData['password']));
}
Related Topic