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

Public Member Functions

 __construct (ConfigInterface $securityConfig, \Magento\Backend\Model\Auth\Session $authSession, \Magento\Security\Model\AdminSessionInfoFactory $adminSessionInfoFactory, \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory $adminSessionInfoCollectionFactory, \Magento\Framework\Stdlib\DateTime\DateTime $dateTime, RemoteAddress $remoteAddress)
 
 processLogin ()
 
 processProlong ()
 
 processLogout ()
 
 getCurrentSession ()
 
 getLogoutReasonMessageByStatus ($statusCode)
 
 getLogoutReasonMessage ()
 
 getSessionsForCurrentUser ()
 
 logoutOtherUserSessions ()
 
 cleanExpiredSessions ()
 

Data Fields

const ADMIN_SESSION_LIFETIME = 86400
 
const LOGOUT_REASON_USER_LOCKED = 10
 

Protected Member Functions

 createNewSession ()
 
 createAdminSessionInfoCollection ()
 

Protected Attributes

 $securityConfig
 
 $authSession
 
 $adminSessionInfoFactory
 
 $adminSessionInfoCollectionFactory
 
 $currentSession
 

Detailed Description

Admin Sessions Manager Model

@api

Since
100.1.0

Definition at line 18 of file AdminSessionsManager.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ConfigInterface  $securityConfig,
\Magento\Backend\Model\Auth\Session  $authSession,
\Magento\Security\Model\AdminSessionInfoFactory  $adminSessionInfoFactory,
\Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory  $adminSessionInfoCollectionFactory,
\Magento\Framework\Stdlib\DateTime\DateTime  $dateTime,
RemoteAddress  $remoteAddress 
)
Parameters
ConfigInterface$securityConfig
\Magento\Backend\Model\Auth\Session$authSession
AdminSessionInfoFactory$adminSessionInfoFactory
CollectionFactory$adminSessionInfoCollectionFactory
\Magento\Framework\Stdlib\DateTime\DateTime$dateTime
RemoteAddress$remoteAddress

Definition at line 86 of file AdminSessionsManager.php.

93  {
94  $this->securityConfig = $securityConfig;
95  $this->authSession = $authSession;
96  $this->adminSessionInfoFactory = $adminSessionInfoFactory;
97  $this->adminSessionInfoCollectionFactory = $adminSessionInfoCollectionFactory;
98  $this->dateTime = $dateTime;
99  $this->remoteAddress = $remoteAddress;
100  }
$dateTime

Member Function Documentation

◆ cleanExpiredSessions()

cleanExpiredSessions ( )

Clean expired Admin Sessions

Returns
$this
Since
100.1.0

Definition at line 276 of file AdminSessionsManager.php.

277  {
278  $this->createAdminSessionInfoCollection()->deleteSessionsOlderThen(
279  $this->dateTime->gmtTimestamp() - self::ADMIN_SESSION_LIFETIME
280  );
281 
282  return $this;
283  }

◆ createAdminSessionInfoCollection()

createAdminSessionInfoCollection ( )
protected
Returns
\Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
Since
100.1.0

Definition at line 311 of file AdminSessionsManager.php.

312  {
313  return $this->adminSessionInfoCollectionFactory->create();
314  }

◆ createNewSession()

createNewSession ( )
protected

Create new record

Returns
$this
Since
100.1.0

Definition at line 291 of file AdminSessionsManager.php.

292  {
293  $this->adminSessionInfoFactory
294  ->create()
295  ->setData(
296  [
297  'session_id' => $this->authSession->getSessionId(),
298  'user_id' => $this->authSession->getUser()->getId(),
299  'ip' => $this->remoteAddress->getRemoteAddress(),
300  'status' => AdminSessionInfo::LOGGED_IN
301  ]
302  )->save();
303 
304  return $this;
305  }

◆ getCurrentSession()

getCurrentSession ( )

Get current session record

Returns
AdminSessionInfo
Since
100.1.0

Definition at line 173 of file AdminSessionsManager.php.

174  {
175  if (!$this->currentSession) {
176  $this->currentSession = $this->adminSessionInfoFactory->create();
177  $this->currentSession->load($this->authSession->getSessionId(), 'session_id');
178  }
179 
180  return $this->currentSession;
181  }

◆ getLogoutReasonMessage()

getLogoutReasonMessage ( )

Get message with explanation of logout reason

Returns
string
Since
100.1.0

Definition at line 226 of file AdminSessionsManager.php.

227  {
228  return $this->getLogoutReasonMessageByStatus(
229  $this->getCurrentSession()->getStatus()
230  );
231  }

◆ getLogoutReasonMessageByStatus()

getLogoutReasonMessageByStatus (   $statusCode)

Get logout reason message by status

Parameters
int$statusCode
Returns
string
Since
100.1.0

Definition at line 190 of file AdminSessionsManager.php.

191  {
192  switch ((int)$statusCode) {
194  $reasonMessage = null;
195  break;
197  $reasonMessage = __(
198  'Someone logged into this account from another device or browser.'
199  .' Your current session is terminated.'
200  );
201  break;
203  $reasonMessage = __(
204  'Your current session is terminated by another user of this account.'
205  );
206  break;
208  $reasonMessage = __(
209  'Your account is temporarily disabled. Please try again later.'
210  );
211  break;
212  default:
213  $reasonMessage = __('Your current session has been expired.');
214  break;
215  }
216 
217  return $reasonMessage;
218  }
__()
Definition: __.php:13

◆ getSessionsForCurrentUser()

getSessionsForCurrentUser ( )

Get sessions for current user

Returns
\Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
Since
100.1.0

Definition at line 239 of file AdminSessionsManager.php.

240  {
241  return $this->createAdminSessionInfoCollection()
242  ->filterByUser($this->authSession->getUser()->getId(), \Magento\Security\Model\AdminSessionInfo::LOGGED_IN)
243  ->filterExpiredSessions($this->securityConfig->getAdminSessionLifetime())
244  ->loadData();
245  }

◆ logoutOtherUserSessions()

logoutOtherUserSessions ( )

Logout another user sessions

Returns
$this
Since
100.1.0

Definition at line 253 of file AdminSessionsManager.php.

254  {
256  ->filterByUser(
257  $this->authSession->getUser()->getId(),
259  $this->authSession->getSessionId()
260  )
261  ->filterExpiredSessions($this->securityConfig->getAdminSessionLifetime())
262  ->loadData();
263 
264  $collection->setDataToAll('status', \Magento\Security\Model\AdminSessionInfo::LOGGED_OUT_MANUALLY)
265  ->save();
266 
267  return $this;
268  }

◆ processLogin()

processLogin ( )

Handle all others active sessions according Sharing Account Setting

Returns
$this
Since
100.1.0

Definition at line 108 of file AdminSessionsManager.php.

109  {
110  $this->createNewSession();
111 
112  $olderThen = $this->dateTime->gmtTimestamp() - $this->securityConfig->getAdminSessionLifetime();
113  if (!$this->securityConfig->isAdminAccountSharingEnabled()) {
114  $result = $this->createAdminSessionInfoCollection()->updateActiveSessionsStatus(
116  $this->getCurrentSession()->getUserId(),
117  $this->getCurrentSession()->getSessionId(),
118  $olderThen
119  );
120  if ($result) {
121  $this->getCurrentSession()->setIsOtherSessionsTerminated(true);
122  }
123  }
124 
125  return $this;
126  }

◆ processLogout()

processLogout ( )

Handle logout process

Returns
$this
Since
100.1.0

Definition at line 156 of file AdminSessionsManager.php.

157  {
158  $this->getCurrentSession()->setData(
159  'status',
161  );
162  $this->getCurrentSession()->save();
163 
164  return $this;
165  }

◆ processProlong()

processProlong ( )

Handle Prolong process

Returns
$this
Since
100.1.0

Definition at line 134 of file AdminSessionsManager.php.

135  {
136  if ($this->lastProlongIsOldEnough()) {
137  $this->getCurrentSession()->setData(
138  'updated_at',
139  date(
140  \Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT,
141  $this->authSession->getUpdatedAt()
142  )
143  );
144  $this->getCurrentSession()->save();
145  }
146 
147  return $this;
148  }

Field Documentation

◆ $adminSessionInfoCollectionFactory

$adminSessionInfoCollectionFactory
protected

Definition at line 52 of file AdminSessionsManager.php.

◆ $adminSessionInfoFactory

$adminSessionInfoFactory
protected

Definition at line 46 of file AdminSessionsManager.php.

◆ $authSession

$authSession
protected

Definition at line 40 of file AdminSessionsManager.php.

◆ $currentSession

$currentSession
protected

Definition at line 58 of file AdminSessionsManager.php.

◆ $securityConfig

$securityConfig
protected

Definition at line 34 of file AdminSessionsManager.php.

◆ ADMIN_SESSION_LIFETIME

const ADMIN_SESSION_LIFETIME = 86400

Admin Session lifetime (sec)

Definition at line 23 of file AdminSessionsManager.php.

◆ LOGOUT_REASON_USER_LOCKED

const LOGOUT_REASON_USER_LOCKED = 10

Logout reason when current user has been locked out

Definition at line 28 of file AdminSessionsManager.php.


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