7
7
8
8
namespace Magento \Security \Model ;
9
9
10
+ use Magento \Backend \Model \Auth \Session ;
10
11
use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
12
+ use Magento \Framework \Stdlib \DateTime ;
13
+ use Magento \Security \Model \ResourceModel \AdminSessionInfo \Collection ;
11
14
use Magento \Security \Model \ResourceModel \AdminSessionInfo \CollectionFactory ;
12
15
13
16
/**
14
17
* Admin Sessions Manager Model
15
18
*
16
19
* @api
17
20
* @since 100.1.0
21
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
18
22
*/
19
23
class AdminSessionsManager
20
24
{
@@ -35,7 +39,7 @@ class AdminSessionsManager
35
39
protected $ securityConfig ;
36
40
37
41
/**
38
- * @var \Magento\Backend\Model\Auth\ Session
42
+ * @var Session
39
43
* @since 100.1.0
40
44
*/
41
45
protected $ authSession ;
@@ -73,20 +77,22 @@ class AdminSessionsManager
73
77
*
74
78
* Means that after session was prolonged
75
79
* all other prolongs will be ignored within this period
80
+ *
81
+ * @var int
76
82
*/
77
83
private $ maxIntervalBetweenConsecutiveProlongs = 60 ;
78
84
79
85
/**
80
86
* @param ConfigInterface $securityConfig
81
- * @param \Magento\Backend\Model\Auth\ Session $authSession
87
+ * @param Session $authSession
82
88
* @param AdminSessionInfoFactory $adminSessionInfoFactory
83
89
* @param CollectionFactory $adminSessionInfoCollectionFactory
84
90
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
85
91
* @param RemoteAddress $remoteAddress
86
92
*/
87
93
public function __construct (
88
94
ConfigInterface $ securityConfig ,
89
- \ Magento \ Backend \ Model \ Auth \ Session $ authSession ,
95
+ Session $ authSession ,
90
96
\Magento \Security \Model \AdminSessionInfoFactory $ adminSessionInfoFactory ,
91
97
\Magento \Security \Model \ResourceModel \AdminSessionInfo \CollectionFactory $ adminSessionInfoCollectionFactory ,
92
98
\Magento \Framework \Stdlib \DateTime \DateTime $ dateTime ,
@@ -138,7 +144,7 @@ public function processProlong()
138
144
$ this ->getCurrentSession ()->setData (
139
145
'updated_at ' ,
140
146
date (
141
- \ Magento \ Framework \ Stdlib \ DateTime::DATETIME_PHP_FORMAT ,
147
+ DateTime::DATETIME_PHP_FORMAT ,
142
148
$ this ->authSession ->getUpdatedAt ()
143
149
)
144
150
);
@@ -204,7 +210,7 @@ public function getLogoutReasonMessageByStatus($statusCode)
204
210
case AdminSessionInfo::LOGGED_OUT_BY_LOGIN :
205
211
$ reasonMessage = __ (
206
212
'Someone logged into this account from another device or browser. '
207
- .' Your current session is terminated. '
213
+ . ' Your current session is terminated. '
208
214
);
209
215
break ;
210
216
case AdminSessionInfo::LOGGED_OUT_MANUALLY :
@@ -241,7 +247,7 @@ public function getLogoutReasonMessage()
241
247
/**
242
248
* Get sessions for current user
243
249
*
244
- * @return \Magento\Security\Model\ResourceModel\AdminSessionInfo\ Collection
250
+ * @return Collection
245
251
* @since 100.1.0
246
252
*/
247
253
public function getSessionsForCurrentUser ()
@@ -314,7 +320,9 @@ protected function createNewSession()
314
320
}
315
321
316
322
/**
317
- * @return \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
323
+ * Retrieve new instance of admin session info collection
324
+ *
325
+ * @return Collection
318
326
* @since 100.1.0
319
327
*/
320
328
protected function createAdminSessionInfoCollection ()
@@ -323,24 +331,27 @@ protected function createAdminSessionInfoCollection()
323
331
}
324
332
325
333
/**
326
- * Calculates diff between now and last session updated_at
327
- * and decides whether new prolong must be triggered or not
334
+ * Calculates diff between now and last session updated_at and decides whether new prolong must be triggered or not
328
335
*
329
336
* This is done to limit amount of session prolongs and updates to database
330
337
* within some period of time - X
331
338
* X - is calculated in getIntervalBetweenConsecutiveProlongs()
332
339
*
333
- * @see getIntervalBetweenConsecutiveProlongs()
334
340
* @return bool
341
+ * @see getIntervalBetweenConsecutiveProlongs()
335
342
*/
336
343
private function lastProlongIsOldEnough ()
337
344
{
338
- $ lastProlongTimestamp = strtotime ($ this ->getCurrentSession ()->getUpdatedAt ());
345
+ $ lastUpdatedTime = $ this ->getCurrentSession ()->getUpdatedAt ();
346
+ if ($ lastUpdatedTime === null || is_numeric ($ lastUpdatedTime )) {
347
+ $ lastUpdatedTime = "now " ;
348
+ }
349
+ $ lastProlongTimestamp = strtotime ($ lastUpdatedTime );
339
350
$ nowTimestamp = $ this ->authSession ->getUpdatedAt ();
340
351
341
352
$ diff = $ nowTimestamp - $ lastProlongTimestamp ;
342
353
343
- return (float ) $ diff > $ this ->getIntervalBetweenConsecutiveProlongs ();
354
+ return (float )$ diff > $ this ->getIntervalBetweenConsecutiveProlongs ();
344
355
}
345
356
346
357
/**
@@ -354,7 +365,7 @@ private function lastProlongIsOldEnough()
354
365
*/
355
366
private function getIntervalBetweenConsecutiveProlongs ()
356
367
{
357
- return (float ) max (
368
+ return (float )max (
358
369
1 ,
359
370
min (
360
371
4 * log ((float )$ this ->securityConfig ->getAdminSessionLifetime ()),
0 commit comments