Skip to content

Commit f631b97

Browse files
committed
ACP2E-1972: check correct visitor created_At
1 parent b0c72f3 commit f631b97

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

app/code/Magento/Customer/Model/ResourceModel/Visitor.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function fetchCreatedAt(int $visitorId): ?int
110110
['created_at' => 'visitor_table.created_at']
111111
)->where(
112112
'visitor_table.visitor_id = ?',
113-
(string) $visitorId
113+
$visitorId,
114+
\Zend_Db::BIGINT_TYPE
114115
)->limit(
115116
1
116117
);
@@ -121,6 +122,62 @@ public function fetchCreatedAt(int $visitorId): ?int
121122
return strtotime($lookup['created_at']);
122123
}
123124

125+
/**
126+
* Gets created at value for the visitor id by customer id
127+
*
128+
* @param int $customerId
129+
* @return int|null
130+
*/
131+
public function fetchCreatedAtByCustomer(int $customerId): ?int
132+
{
133+
$connection = $this->getConnection();
134+
$select = $connection->select()->from(
135+
['visitor_table' => $this->getTable('customer_visitor')],
136+
['created_at' => 'visitor_table.created_at']
137+
)->where(
138+
'visitor_table.customer_id = ?',
139+
$customerId,
140+
\Zend_Db::INT_TYPE
141+
)->order(
142+
'visitor_table.visitor_id DESC'
143+
)->limit(
144+
1
145+
);
146+
$lookup = $connection->fetchRow($select);
147+
if (empty($lookup) || $lookup['created_at'] == null) {
148+
return null;
149+
}
150+
return strtotime($lookup['created_at']);
151+
}
152+
153+
/**
154+
* Gets created at value for the visitor id by customer id
155+
*
156+
* @param int $customerId
157+
* @return int|null
158+
*/
159+
public function fetchLastVisitAtByCustomer(int $customerId): ?int
160+
{
161+
$connection = $this->getConnection();
162+
$select = $connection->select()->from(
163+
['visitor_table' => $this->getTable('customer_visitor')],
164+
['last_visit_at' => 'visitor_table.last_visit_at']
165+
)->where(
166+
'visitor_table.customer_id = ?',
167+
$customerId,
168+
\Zend_Db::INT_TYPE
169+
)->order(
170+
'visitor_table.visitor_id DESC'
171+
)->limit(
172+
1
173+
);
174+
$lookup = $connection->fetchRow($select);
175+
if (empty($lookup) || $lookup['last_visit_at'] == null) {
176+
return null;
177+
}
178+
return strtotime($lookup['last_visit_at']);
179+
}
180+
124181
/**
125182
* Update visitor session created at column value
126183
*
@@ -136,4 +193,20 @@ public function updateCreatedAt(int $visitorId, int $timestamp): void
136193
$this->getConnection()->quoteInto('visitor_id = ?', $visitorId)
137194
);
138195
}
196+
197+
/**
198+
* Update visitor session visitor id column value
199+
*
200+
* @param int $visitorId
201+
* @param int $customerId
202+
* @return void
203+
*/
204+
public function updateCustomerId(int $visitorId, int $customerId): void
205+
{
206+
$this->getConnection()->update(
207+
$this->getTable('customer_visitor'),
208+
['customer_id' => $customerId],
209+
$this->getConnection()->quoteInto('visitor_id = ?', $visitorId)
210+
);
211+
}
139212
}

app/code/Magento/Customer/Model/Session/SessionCleaner.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ public function clearFor(int $customerId): void
9898
$timestamp = $dateTime->getTimestamp();
9999
$this->customerResourceModel->updateSessionCutOff($customerId, $timestamp);
100100
if ($this->sessionManager->getVisitorData() !== null) {
101+
101102
$visitorId = $this->sessionManager->getVisitorData()['visitor_id'];
103+
$this->visitorResourceModel->updateCustomerId((int) $visitorId, $customerId);
102104
$this->visitorResourceModel->updateCreatedAt((int) $visitorId, $timestamp + 1);
103105
}
104106
}
105107
}
106-

app/code/Magento/Customer/Model/Session/Validators/CutoffValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public function validate(SessionManagerInterface $session): void
6868
) {
6969
$cutoff = $this->customerResource->findSessionCutOff((int) $visitor['customer_id']);
7070
$sessionCreationTime = $this->visitorResource->fetchCreatedAt((int) $visitor['visitor_id']);
71+
$secondSessionCreationTime = $this->visitorResource->fetchCreatedAtByCustomer((int) $visitor['customer_id']);
72+
73+
if ($secondSessionCreationTime > $sessionCreationTime) {
74+
$sessionCreationTime = $secondSessionCreationTime;
75+
}
7176
if (isset($cutoff, $sessionCreationTime) && $cutoff > $sessionCreationTime) {
7277
throw new SessionException(
7378
new Phrase('The session has expired, please login again.')

0 commit comments

Comments
 (0)