6

I have build a plugin on the authenticate() method in Magento\Customer\Model\AccountManagement with beforeAuthenticate().

It is working and I can change the firstname of something else of the customer.

I'm trying to find the password in an other system, and set that password to the customer on the moment of login. Everything works, and I get the information.

The only problem is saving the password to Magento2.

I tried:

    $customer = $this->customerRepository->getById($customerId);
    $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
    $customerSecure->setPasswordHash($this->encryptor->getHash($password, true));
    $this->customerRepository->save($customer);

and:

    $customer =  $this->customerFactory->create();
    $customer->load($_customer->getId());
    $customer->setPassword($password);
    $customer->save();

None of them seems to resetting / setting my password.

And ideas how to fix it ?

5

The save method of the class Magento\Customer\Model\ResourceModel\CustomerRepository has an extra argument passwordHash

Use this argument to set a new password:

$customer = $this->customerRepository->getById($customerId);
$this->customerRepository->save($customer, $this->encryptor->getHash($password, true));
  • what is problem with this code? $customer = $this->customerFactory->create(); $customer->load($_customer->getId()); $customer->setPassword($password); $customer->save(); – jafar pinjar Dec 5 '18 at 14:05
4

I had similar problem. I needed to check the hash from the old web site, and if everything is ok - change customer password and authenticate it on frontend (save "new" password to DB). My solution:

namespace Devromans\Customer\Plugin;

/**
 * Class AccountManagementPlugin
 *
 * @package Devromans\Customer\Plugin
 */
class AccountManagementPlugin
{
    /**
     * Customer Repository.
     *
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $_customerRepository;

    /**
     * Encryptor.
     *
     * @var \Magento\Framework\Encryption\Encryptor
     */
    protected $_encryptor;

    /**
     * Customer registry.
     *
     * @var \Magento\Customer\Model\CustomerRegistry
     */
    protected $_customerRegistry;

    /**
     * AccountManagementPlugin constructor.
     *
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
     * @param \Magento\Framework\Encryption\Encryptor           $encryptor
     * @param \Magento\Customer\Model\CustomerRegistry          $customerRegistry
     */
    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\Encryption\Encryptor $encryptor,
        \Magento\Customer\Model\CustomerRegistry $customerRegistry
    ) {
        $this->_customerRepository = $customerRepository;
        $this->_encryptor          = $encryptor;
        $this->_customerRegistry   = $customerRegistry;
    }


    /**
     * Authenticate Customer by Hash from Old site and update info in DB.
     *
     * @param \Magento\Customer\Model\AccountManagement $subject
     * @param array                                     $args
     *
     * @return array
     */
    public function beforeAuthenticate(\Magento\Customer\Model\AccountManagement $subject, ...$args)
    {
        if (!empty($args[0]) && !empty($args[1])) {
            try {
                $customer = $this->_customerRepository->get($args[0]);

                /* Logic for validation hash from old website here */

                $passwordHash = $this->_encryptor->getHash($args[1], true);
                $customerSecure = $this->_customerRegistry->retrieveSecureData($customer->getId());
                $customerSecure->setRpToken(null);
                $customerSecure->setRpTokenCreatedAt(null);
                $customerSecure->setPasswordHash($passwordHash);
                $this->_customerRepository->save($customer, $passwordHash);
                $this->_customerRegistry->remove($customer->getId());
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
                return $args;
            }
        }

        return $args;
    }
}

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.