Skip to content

Instantly share code, notes, and snippets.

@tiagosampaio
Created December 5, 2017 00:28
Show Gist options
  • Save tiagosampaio/eed8d29588a1cbaa1d47b066f7d39cc8 to your computer and use it in GitHub Desktop.
Save tiagosampaio/eed8d29588a1cbaa1d47b066f7d39cc8 to your computer and use it in GitHub Desktop.
Code Example Customer Model in Magento 2
<?php
/**
* Authenticate customer
*
* @param string $login
* @param string $password
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* Use \Magento\Customer\Api\AccountManagementInterface::authenticate
*/
public function authenticate($login, $password)
{
$this->loadByEmail($login);
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
throw new EmailNotConfirmedException(
__('This account is not confirmed.')
);
}
if (!$this->validatePassword($password)) {
throw new InvalidEmailOrPasswordException(
__('Invalid login or password.')
);
}
$this->_eventManager->dispatch(
'customer_customer_authenticated',
['model' => $this, 'password' => $password]
);
return true;
}
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
*/
public function loadByEmail($customerEmail)
{
$this->_getResource()->loadByEmail($this, $customerEmail);
return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment