Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Authentication.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
15 
21 {
25  const LOCKOUT_THRESHOLD_PATH = 'customer/password/lockout_threshold';
26 
30  const MAX_FAILURES_PATH = 'customer/password/lockout_failures';
31 
35  protected $customerRegistry;
36 
42  protected $backendConfig;
43 
47  protected $dateTime;
48 
52  protected $encryptor;
53 
58 
62  private $customerAuthUpdate;
63 
71  public function __construct(
75  \Magento\Framework\Stdlib\DateTime $dateTime,
76  Encryptor $encryptor
77  ) {
78  $this->customerRepository = $customerRepository;
79  $this->customerRegistry = $customerRegistry;
80  $this->backendConfig = $backendConfig;
81  $this->dateTime = $dateTime;
82  $this->encryptor = $encryptor;
83  }
84 
89  {
90  $now = new \DateTime();
91  $lockThreshold = $this->getLockThreshold();
92  $maxFailures = $this->getMaxFailures();
93  $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
94 
95  if (!($lockThreshold && $maxFailures)) {
96  return;
97  }
98  $failuresNum = (int)$customerSecure->getFailuresNum() + 1;
99 
100  $firstFailureDate = $customerSecure->getFirstFailure();
101  if ($firstFailureDate) {
102  $firstFailureDate = new \DateTime($firstFailureDate);
103  }
104 
105  $lockThreshInterval = new \DateInterval('PT' . $lockThreshold . 'S');
106  $lockExpires = $customerSecure->getLockExpires();
107  $lockExpired = ($lockExpires !== null) && ($now > new \DateTime($lockExpires));
108  // set first failure date when this is the first failure or the lock is expired
109  if (1 === $failuresNum || !$firstFailureDate || $lockExpired) {
110  $customerSecure->setFirstFailure($this->dateTime->formatDate($now));
111  $failuresNum = 1;
112  $customerSecure->setLockExpires(null);
113  // otherwise lock customer
114  } elseif ($failuresNum >= $maxFailures) {
115  $customerSecure->setLockExpires($this->dateTime->formatDate($now->add($lockThreshInterval)));
116  }
117 
118  $customerSecure->setFailuresNum($failuresNum);
119  $this->getCustomerAuthUpdate()->saveAuth($customerId);
120  }
121 
125  public function unlock($customerId)
126  {
127  $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
128  $customerSecure->setFailuresNum(0);
129  $customerSecure->setFirstFailure(null);
130  $customerSecure->setLockExpires(null);
131  $this->getCustomerAuthUpdate()->saveAuth($customerId);
132  }
133 
139  protected function getLockThreshold()
140  {
141  return $this->backendConfig->getValue(self::LOCKOUT_THRESHOLD_PATH) * 60;
142  }
143 
149  protected function getMaxFailures()
150  {
151  return $this->backendConfig->getValue(self::MAX_FAILURES_PATH);
152  }
153 
157  public function isLocked($customerId)
158  {
159  $currentCustomer = $this->customerRegistry->retrieve($customerId);
160  return $currentCustomer->isCustomerLocked();
161  }
162 
166  public function authenticate($customerId, $password)
167  {
168  $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
169  $hash = $customerSecure->getPasswordHash();
170  if (!$this->encryptor->validateHash($password, $hash)) {
172  if ($this->isLocked($customerId)) {
173  throw new UserLockedException(__('The account is locked.'));
174  }
175  throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
176  }
177  return true;
178  }
179 
186  private function getCustomerAuthUpdate()
187  {
188  if ($this->customerAuthUpdate === null) {
189  $this->customerAuthUpdate =
190  \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerAuthUpdate::class);
191  }
192  return $this->customerAuthUpdate;
193  }
194 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
__construct(CustomerRepositoryInterface $customerRepository, CustomerRegistry $customerRegistry, ConfigInterface $backendConfig, \Magento\Framework\Stdlib\DateTime $dateTime, Encryptor $encryptor)
authenticate($customerId, $password)