Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Authentication Class Reference
Inheritance diagram for Authentication:
AuthenticationInterface

Public Member Functions

 __construct (CustomerRepositoryInterface $customerRepository, CustomerRegistry $customerRegistry, ConfigInterface $backendConfig, \Magento\Framework\Stdlib\DateTime $dateTime, Encryptor $encryptor)
 
 processAuthenticationFailure ($customerId)
 
 unlock ($customerId)
 
 isLocked ($customerId)
 
 authenticate ($customerId, $password)
 

Data Fields

const LOCKOUT_THRESHOLD_PATH = 'customer/password/lockout_threshold'
 
const MAX_FAILURES_PATH = 'customer/password/lockout_failures'
 

Protected Member Functions

 getLockThreshold ()
 
 getMaxFailures ()
 

Protected Attributes

 $customerRegistry
 
 $backendConfig
 
 $dateTime
 
 $encryptor
 
 $customerRepository
 

Detailed Description

Class Authentication @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 20 of file Authentication.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CustomerRepositoryInterface  $customerRepository,
CustomerRegistry  $customerRegistry,
ConfigInterface  $backendConfig,
\Magento\Framework\Stdlib\DateTime  $dateTime,
Encryptor  $encryptor 
)
Parameters
CustomerRepositoryInterface$customerRepository
CustomerRegistry$customerRegistry
ConfigInterface$backendConfig
\Magento\Framework\Stdlib\DateTime$dateTime
Encryptor$encryptor

Definition at line 71 of file Authentication.php.

77  {
78  $this->customerRepository = $customerRepository;
79  $this->customerRegistry = $customerRegistry;
80  $this->backendConfig = $backendConfig;
81  $this->dateTime = $dateTime;
82  $this->encryptor = $encryptor;
83  }

Member Function Documentation

◆ authenticate()

authenticate (   $customerId,
  $password 
)

{Authenticate customer

Parameters
int$customerId
string$password
Returns
boolean
Exceptions
InvalidEmailOrPasswordException
UserLockedException
}

Implements AuthenticationInterface.

Definition at line 166 of file Authentication.php.

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  }
__()
Definition: __.php:13

◆ getLockThreshold()

getLockThreshold ( )
protected

Get lock threshold

Returns
int

Definition at line 139 of file Authentication.php.

140  {
141  return $this->backendConfig->getValue(self::LOCKOUT_THRESHOLD_PATH) * 60;
142  }

◆ getMaxFailures()

getMaxFailures ( )
protected

Get max failures

Returns
int

Definition at line 149 of file Authentication.php.

150  {
151  return $this->backendConfig->getValue(self::MAX_FAILURES_PATH);
152  }

◆ isLocked()

isLocked (   $customerId)

{Check if a customer is locked

Parameters
int$customerId
Returns
boolean
}

Implements AuthenticationInterface.

Definition at line 157 of file Authentication.php.

158  {
159  $currentCustomer = $this->customerRegistry->retrieve($customerId);
160  return $currentCustomer->isCustomerLocked();
161  }

◆ processAuthenticationFailure()

processAuthenticationFailure (   $customerId)

{Process customer authentication failure

Parameters
int$customerId
Returns
void
}

Implements AuthenticationInterface.

Definition at line 88 of file Authentication.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ unlock()

unlock (   $customerId)

{Unlock customer

Parameters
int$customerId
Returns
void
}

Implements AuthenticationInterface.

Definition at line 125 of file Authentication.php.

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  }

Field Documentation

◆ $backendConfig

$backendConfig
protected

Definition at line 42 of file Authentication.php.

◆ $customerRegistry

$customerRegistry
protected

Definition at line 35 of file Authentication.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 57 of file Authentication.php.

◆ $dateTime

$dateTime
protected

Definition at line 47 of file Authentication.php.

◆ $encryptor

$encryptor
protected

Definition at line 52 of file Authentication.php.

◆ LOCKOUT_THRESHOLD_PATH

const LOCKOUT_THRESHOLD_PATH = 'customer/password/lockout_threshold'

Configuration path to customer lockout threshold

Definition at line 25 of file Authentication.php.

◆ MAX_FAILURES_PATH

const MAX_FAILURES_PATH = 'customer/password/lockout_failures'

Configuration path to customer max login failures number

Definition at line 30 of file Authentication.php.


The documentation for this class was generated from the following file: