Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
Auth Class Reference

Public Member Functions

 __construct (\Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Backend\Helper\Data $backendData, \Magento\Backend\Model\Auth\StorageInterface $authStorage, \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\Framework\Data\Collection\ModelFactory $modelFactory)
 
 setAuthStorage ($storage)
 
 getAuthStorage ()
 
 getUser ()
 
 getCredentialStorage ()
 
 login ($username, $password)
 
 logout ()
 
 isLoggedIn ()
 

Static Public Member Functions

static throwException (Phrase $msg=null)
 

Protected Member Functions

 _initCredentialStorage ()
 

Protected Attributes

 $_authStorage
 
 $_credentialStorage
 
 $_backendData
 
 $_eventManager
 
 $_coreConfig
 
 $_modelFactory
 

Detailed Description

Backend Auth model

@api

Since
100.0.2

Definition at line 19 of file Auth.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Framework\Event\ManagerInterface$eventManager
\Magento\Backend\Helper\Data$backendData
\Magento\Backend\Model\Auth\StorageInterface$authStorage
\Magento\Backend\Model\Auth\Credential\StorageInterface$credentialStorage
\Magento\Framework\App\Config\ScopeConfigInterface$coreConfig
\Magento\Framework\Data\Collection\ModelFactory$modelFactory

Definition at line 63 of file Auth.php.

70  {
71  $this->_eventManager = $eventManager;
72  $this->_backendData = $backendData;
73  $this->_authStorage = $authStorage;
74  $this->_credentialStorage = $credentialStorage;
75  $this->_coreConfig = $coreConfig;
76  $this->_modelFactory = $modelFactory;
77  }

Member Function Documentation

◆ _initCredentialStorage()

_initCredentialStorage ( )
protected

Initialize credential storage from configuration

Returns
void

Definition at line 123 of file Auth.php.

124  {
125  $this->_credentialStorage = $this->_modelFactory->create(
126  \Magento\Backend\Model\Auth\Credential\StorageInterface::class
127  );
128  }

◆ getAuthStorage()

getAuthStorage ( )

Return auth storage. If auth storage was not defined outside - returns default object of auth storage

Returns
\Magento\Backend\Model\Auth\StorageInterface @codeCoverageIgnore

Definition at line 102 of file Auth.php.

103  {
104  return $this->_authStorage;
105  }

◆ getCredentialStorage()

getCredentialStorage ( )

Return credential storage object

Returns
null|\Magento\Backend\Model\Auth\Credential\StorageInterface @codeCoverageIgnore

Definition at line 136 of file Auth.php.

137  {
139  }

◆ getUser()

getUser ( )

Return current (successfully authenticated) user, an instance of \Magento\Backend\Model\Auth\Credential\StorageInterface

Returns
\Magento\Backend\Model\Auth\Credential\StorageInterface

Definition at line 113 of file Auth.php.

114  {
115  return $this->getAuthStorage()->getUser();
116  }

◆ isLoggedIn()

isLoggedIn ( )

Check if current user is logged in

Returns
bool

Definition at line 216 of file Auth.php.

217  {
218  return $this->getAuthStorage()->isLoggedIn();
219  }

◆ login()

login (   $username,
  $password 
)

Perform login process

Parameters
string$username
string$password
Returns
void
Exceptions

Definition at line 149 of file Auth.php.

150  {
151  if (empty($username) || empty($password)) {
153  __(
154  'The account sign-in was incorrect or your account is disabled temporarily. '
155  . 'Please wait and try again later.'
156  )
157  );
158  }
159 
160  try {
161  $this->_initCredentialStorage();
162  $this->getCredentialStorage()->login($username, $password);
163  if ($this->getCredentialStorage()->getId()) {
164  $this->getAuthStorage()->setUser($this->getCredentialStorage());
165  $this->getAuthStorage()->processLogin();
166 
167  $this->_eventManager->dispatch(
168  'backend_auth_user_login_success',
169  ['user' => $this->getCredentialStorage()]
170  );
171  }
172 
173  if (!$this->getAuthStorage()->getUser()) {
175  __(
176  'The account sign-in was incorrect or your account is disabled temporarily. '
177  . 'Please wait and try again later.'
178  )
179  );
180  }
181  } catch (PluginAuthenticationException $e) {
182  $this->_eventManager->dispatch(
183  'backend_auth_user_login_failed',
184  ['user_name' => $username, 'exception' => $e]
185  );
186  throw $e;
187  } catch (\Magento\Framework\Exception\LocalizedException $e) {
188  $this->_eventManager->dispatch(
189  'backend_auth_user_login_failed',
190  ['user_name' => $username, 'exception' => $e]
191  );
193  __(
194  $e->getMessage()? : 'The account sign-in was incorrect or your account is disabled temporarily. '
195  . 'Please wait and try again later.'
196  )
197  );
198  }
199  }
__()
Definition: __.php:13
static throwException(Phrase $msg=null)
Definition: Auth.php:229

◆ logout()

logout ( )

Perform logout process

Returns
void

Definition at line 206 of file Auth.php.

207  {
208  $this->getAuthStorage()->processLogout();
209  }

◆ setAuthStorage()

setAuthStorage (   $storage)

Set auth storage if it is instance of \Magento\Backend\Model\Auth\StorageInterface

Parameters
\Magento\Backend\Model\Auth\StorageInterface$storage
Returns
$this
Exceptions

Definition at line 86 of file Auth.php.

87  {
88  if (!$storage instanceof \Magento\Backend\Model\Auth\StorageInterface) {
89  self::throwException(__('Authentication storage is incorrect.'));
90  }
91  $this->_authStorage = $storage;
92  return $this;
93  }
__()
Definition: __.php:13
static throwException(Phrase $msg=null)
Definition: Auth.php:229

◆ throwException()

static throwException ( Phrase  $msg = null)
static

Throws specific Backend Authentication \Exception

Parameters
\Magento\Framework\Phrase$msg
Returns
void
Exceptions

Definition at line 229 of file Auth.php.

230  {
231  if ($msg === null) {
232  $msg = __('An authentication error occurred. Verify and try again.');
233  }
234  throw new AuthenticationException($msg);
235  }
__()
Definition: __.php:13

Field Documentation

◆ $_authStorage

$_authStorage
protected

Definition at line 24 of file Auth.php.

◆ $_backendData

$_backendData
protected

Definition at line 36 of file Auth.php.

◆ $_coreConfig

$_coreConfig
protected

Definition at line 48 of file Auth.php.

◆ $_credentialStorage

$_credentialStorage
protected

Definition at line 29 of file Auth.php.

◆ $_eventManager

$_eventManager
protected

Definition at line 43 of file Auth.php.

◆ $_modelFactory

$_modelFactory
protected

Definition at line 53 of file Auth.php.


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