2

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.

5

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']));
}
1
  • the code is not working on checkout registration (onpage, checkout) is there any solution ? – PFK Jul 5 '16 at 10:16
1

I think you're lacking a $customer->save();

Furthermore, magento has a nice getPost() function that allows some sort of failsafe.

->getPost()

  • Argument #1 : get the desired post variable
  • Argument #2 : return value if variable is not set

That said, your code will look like this:


public $_singletonFlag = false;

public function storeMD5Password($observer)
{
    // Prevent the save_handler to call itself and end-up with infinite loops.
    if($this->singletonFlag) {
        return;
    }
    $this->singletonFlag = true;

    $event = $observer->getEvent();
    $customer = $event->getCustomer();
    if ($customer instanceof Mage_Customer_Model_Customer) {

        if ($new_password = Mage::app()->getRequest()->getPost('password', false) && strlen($new_password) > 0) {
            $customer->setData('md5_password', md5($new_password));
            $customer->save(); 
        }
    }
}
3
  • Could you elaborate a bit more? Where exactly should this be added? – 7ochem Jan 16 '16 at 15:14
  • Yes, I'll update the snippet – Lapidi Jan 18 '16 at 14:47
  • Hi , i have tested the code but unfortunately it is not working when user do the registration in check out (onpage from magento). itried to register the config in evets globally but didnt work. any idea? – PFK Jul 4 '16 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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