Skip to content

Commit adb8f4d

Browse files
authored
magento2-login-as-customer/issues/29: Destroy impersonated customer sessions on admin logout.
2 parents 740cfd5 + 6d955c8 commit adb8f4d

File tree

15 files changed

+191
-100
lines changed

15 files changed

+191
-100
lines changed

app/code/Magento/LoginAsCustomer/Cron/DeleteExpiredAuthenticationData.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteExpiredAuthenticationData.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ public function __construct(
5050
/**
5151
* @inheritdoc
5252
*/
53-
public function execute(): void
53+
public function execute(int $userId): void
5454
{
5555
$connection = $this->resourceConnection->getConnection();
5656
$tableName = $this->resourceConnection->getTableName('login_as_customer');
5757

58-
$timePoint = date(
59-
'Y-m-d H:i:s',
60-
$this->dateTime->gmtTimestamp() - $this->config->getAuthenticationDataExpirationTime()
61-
);
62-
6358
$connection->delete(
6459
$tableName,
6560
[
66-
'created_at < ?' => $timePoint
61+
'admin_id = ?' => $userId
6762
]
6863
);
6964
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/GetAuthenticationDataBySecret.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public function execute(string $secretKey): AuthenticationDataInterface
8585
/** @var AuthenticationDataInterface $authenticationData */
8686
$authenticationData = $this->authenticationDataFactory->create(
8787
[
88-
'customerId' => (int)$data['admin_id'],
89-
'adminId' => (int)$data['customer_id'],
88+
'customerId' => (int)$data['customer_id'],
89+
'adminId' => (int)$data['admin_id'],
9090
'extensionAttributes' => null,
9191
]
9292
);

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteAuthenticationDataBySecret.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/IsLoginAsCustomerSessionActive.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
namespace Magento\LoginAsCustomer\Model\ResourceModel;
99

1010
use Magento\Framework\App\ResourceConnection;
11-
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface;
11+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface;
1212

1313
/**
1414
* @inheritdoc
1515
*/
16-
class DeleteAuthenticationDataBySecret implements DeleteAuthenticationDataBySecretInterface
16+
class IsLoginAsCustomerSessionActive implements IsLoginAsCustomerSessionActiveInterface
1717
{
1818
/**
1919
* @var ResourceConnection
@@ -32,16 +32,18 @@ public function __construct(
3232
/**
3333
* @inheritdoc
3434
*/
35-
public function execute(string $secret): void
35+
public function execute(int $customerId, int $userId): bool
3636
{
37-
$connection = $this->resourceConnection->getConnection();
3837
$tableName = $this->resourceConnection->getTableName('login_as_customer');
38+
$connection = $this->resourceConnection->getConnection();
39+
40+
$query = $connection->select()
41+
->from($tableName)
42+
->where('customer_id = ?', $customerId)
43+
->where('admin_id = ?', $userId);
44+
45+
$result = $connection->fetchRow($query);
3946

40-
$connection->delete(
41-
$tableName,
42-
[
43-
'secret = ?' => $secret
44-
]
45-
);
47+
return false !== $result;
4648
}
4749
}

app/code/Magento/LoginAsCustomer/etc/crontab.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/code/Magento/LoginAsCustomer/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataBySecret"/>
1414
<preference for="Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteExpiredAuthenticationData"/>
1515
<preference for="Magento\LoginAsCustomerApi\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config"/>
16+
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
1617
</config>

app/code/Magento/LoginAsCustomerApi/Api/DeleteExpiredAuthenticationDataInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
interface DeleteExpiredAuthenticationDataInterface
1616
{
1717
/**
18-
* Delete expired authentication data
18+
* Delete expired authentication data by user id.
1919
*
20+
* @param int $userId
2021
* @return void
2122
*/
22-
public function execute(): void;
23+
public function execute(int $userId): void;
2324
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomerApi\Api;
9+
10+
/**
11+
* Check if Login as Customer session is still active.
12+
*
13+
* @api
14+
*/
15+
interface IsLoginAsCustomerSessionActiveInterface
16+
{
17+
/**
18+
* Check if Login as Customer session is still active.
19+
*
20+
* @param int $customerId
21+
* @param int $userId
22+
* @return bool
23+
*/
24+
public function execute(int $customerId, int $userId): bool;
25+
}

app/code/Magento/LoginAsCustomerSales/Plugin/FrontAddCommentOnOrderPlacementPlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\User\Model\UserFactory;
1313

1414
/**
15-
* Add comment after order placed by admin using login-as-customer.
15+
* Add comment after order placed by admin using Login as Customer.
1616
*
1717
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1818
*/
@@ -41,7 +41,7 @@ public function __construct(
4141
}
4242

4343
/**
44-
* Add comment after order placed by admin using login-as-customer.
44+
* Add comment after order placed by admin using Login as Customer.
4545
*
4646
* @param Order $subject
4747
* @param Order $result

app/code/Magento/LoginAsCustomerUi/Controller/Adminhtml/Login/Login.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
2323
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
2424
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
25+
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
2526
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
2627
use Magento\Store\Model\StoreManagerInterface;
2728

@@ -30,6 +31,8 @@
3031
* Generate secret key and forward to the storefront action
3132
*
3233
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
34+
*
35+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3336
*/
3437
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
3538
{
@@ -70,6 +73,11 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
7073
*/
7174
private $saveAuthenticationData;
7275

76+
/**
77+
* @var DeleteExpiredAuthenticationDataInterface
78+
*/
79+
private $deleteExpiredAuthenticationData;
80+
7381
/**
7482
* @var Url
7583
*/
@@ -83,6 +91,7 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
8391
* @param ConfigInterface $config
8492
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
8593
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
94+
* @param DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
8695
* @param Url $url
8796
*/
8897
public function __construct(
@@ -93,6 +102,7 @@ public function __construct(
93102
ConfigInterface $config,
94103
AuthenticationDataInterfaceFactory $authenticationDataFactory,
95104
SaveAuthenticationDataInterface $saveAuthenticationData,
105+
DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData,
96106
Url $url
97107
) {
98108
parent::__construct($context);
@@ -103,6 +113,7 @@ public function __construct(
103113
$this->config = $config;
104114
$this->authenticationDataFactory = $authenticationDataFactory;
105115
$this->saveAuthenticationData = $saveAuthenticationData;
116+
$this->deleteExpiredAuthenticationData = $deleteExpiredAuthenticationData;
106117
$this->url = $url;
107118
}
108119

@@ -142,15 +153,18 @@ public function execute(): ResultInterface
142153
}
143154

144155
$adminUser = $this->authSession->getUser();
156+
$userId = (int)$adminUser->getId();
145157

146158
/** @var AuthenticationDataInterface $authenticationData */
147159
$authenticationData = $this->authenticationDataFactory->create(
148160
[
149161
'customerId' => $customerId,
150-
'adminId' => (int)$adminUser->getId(),
162+
'adminId' => $userId,
151163
'extensionAttributes' => null,
152164
]
153165
);
166+
167+
$this->deleteExpiredAuthenticationData->execute($userId);
154168
$secret = $this->saveAuthenticationData->execute($authenticationData);
155169

156170
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);

0 commit comments

Comments
 (0)