Skip to content

Commit 7b468d2

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- \Magento\LoginAsCustomer\Api\DeleteExpiredAuthenticationDataInterface refactoring
1 parent f893209 commit 7b468d2

12 files changed

+59
-56
lines changed

app/code/Magento/LoginAsCustomer/Api/ConfigInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function isEnabled(): bool;
2929
public function isStoreManualChoiceEnabled(): bool;
3030

3131
/**
32-
* Gte secrets expiration time (in seconds)
32+
* Gte authentication data expiration time (in seconds)
3333
*
3434
* @return int
3535
*/
36-
public function getSecretExpirationTime(): int;
36+
public function getAuthenticationDataExpirationTime(): int;
3737
}

app/code/Magento/LoginAsCustomer/Api/DeleteOutdatedSecretsInterface.php renamed to app/code/Magento/LoginAsCustomer/Api/DeleteExpiredAuthenticationDataInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
namespace Magento\LoginAsCustomer\Api;
99

1010
/**
11-
* Delete outdated secret records
11+
* Delete expired authentication data
1212
*
1313
* @api
1414
*/
15-
interface DeleteOutdatedSecretsInterface
15+
interface DeleteExpiredAuthenticationDataInterface
1616
{
1717
/**
18-
* Delete outdated secret records
18+
* Delete expired authentication data
19+
*
20+
* @return void
1921
*/
2022
public function execute(): void;
2123
}

app/code/Magento/LoginAsCustomer/Cron/DeleteOutdatedSecrets.php renamed to app/code/Magento/LoginAsCustomer/Cron/DeleteExpiredAuthenticationData.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
namespace Magento\LoginAsCustomer\Cron;
99

1010
use Magento\LoginAsCustomer\Api\ConfigInterface;
11-
use Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface;
11+
use Magento\LoginAsCustomer\Api\DeleteExpiredAuthenticationDataInterface;
1212

1313
/**
14-
* @api
14+
* elete expired authentication data cron task
1515
*/
16-
class DeleteOutdatedSecrets implements DeleteOutdatedSecretsInterface
16+
class DeleteExpiredAuthenticationData
1717
{
1818
/**
19-
* @var DeleteOutdatedSecretsInterface
19+
* @var DeleteExpiredAuthenticationDataInterface
2020
*/
2121
private $deleteOldSecretsProcessor;
2222

@@ -26,21 +26,21 @@ class DeleteOutdatedSecrets implements DeleteOutdatedSecretsInterface
2626
private $config;
2727

2828
/**
29-
* @param DeleteOutdatedSecretsInterface $deleteOldSecretsProcessor
29+
* @param DeleteExpiredAuthenticationDataInterface $deleteOldSecretsProcessor
3030
* @param ConfigInterface $config
3131
*/
3232
public function __construct(
33-
DeleteOutdatedSecretsInterface $deleteOldSecretsProcessor,
33+
DeleteExpiredAuthenticationDataInterface $deleteOldSecretsProcessor,
3434
ConfigInterface $config
3535
) {
3636
$this->deleteOldSecretsProcessor = $deleteOldSecretsProcessor;
3737
$this->config = $config;
3838
}
3939

4040
/**
41-
* Delete old secret key records
41+
* elete expired authentication data
4242
*/
43-
public function execute():void
43+
public function execute(): void
4444
{
4545
if ($this->config->isEnabled()) {
4646
$this->deleteOldSecretsProcessor->execute();

app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function execute(AuthenticationDataInterface $authenticationData): void
4040
$this->customerSession->logout();
4141
}
4242

43-
$loggedIn = $this->customerSession->loginById($authenticationData->getCustomerId());
44-
if (!$loggedIn) {
43+
$result = $this->customerSession->loginById($authenticationData->getCustomerId());
44+
if (false === $result) {
4545
throw new LocalizedException(__('Login was not successful.'));
4646
}
4747

app/code/Magento/LoginAsCustomer/Model/Config.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ class Config implements ConfigInterface
1818
/**
1919
* Extension config path
2020
*/
21-
private const XML_PATH_ENABLED = 'login_as_customer/general/enabled';
21+
private const XML_PATH_ENABLED
22+
= 'login_as_customer/general/enabled';
2223
private const XML_PATH_STORE_VIEW_MANUAL_CHOICE_ENABLED
2324
= 'login_as_customer/general/store_view_manual_choice_enabled';
24-
private const XML_PATH_SECRETS_EXPIRATION_TIME = 'login_as_customer/general/secrets_expiration_time';
25+
private const XML_PATH_AUTHENTICATION_EXPIRATION_TIME
26+
= 'login_as_customer/general/authentication_data_expiration_time';
2527

2628
/**
2729
* @var ScopeConfigInterface
@@ -56,8 +58,8 @@ public function isStoreManualChoiceEnabled(): bool
5658
/**
5759
* @inheritdoc
5860
*/
59-
public function getSecretExpirationTime(): int
61+
public function getAuthenticationDataExpirationTime(): int
6062
{
61-
return (int)$this->scopeConfig->getValue(self::XML_PATH_SECRETS_EXPIRATION_TIME);
63+
return (int)$this->scopeConfig->getValue(self::XML_PATH_AUTHENTICATION_EXPIRATION_TIME);
6264
}
6365
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteOutdatedSecrets.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteExpiredAuthenticationData.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
1212
use Magento\LoginAsCustomer\Api\ConfigInterface;
13-
use Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface;
13+
use Magento\LoginAsCustomer\Api\DeleteExpiredAuthenticationDataInterface;
1414

1515
/**
1616
* @inheritdoc
1717
*/
18-
class DeleteOutdatedSecrets implements DeleteOutdatedSecretsInterface
18+
class DeleteExpiredAuthenticationData implements DeleteExpiredAuthenticationDataInterface
1919
{
2020
/**
2121
* @var ResourceConnection
@@ -55,7 +55,10 @@ public function execute(): void
5555
$connection = $this->resourceConnection->getConnection();
5656
$tableName = $this->resourceConnection->getTableName('login_as_customer');
5757

58-
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $this->config->getSecretExpirationTime());
58+
$timePoint = date(
59+
'Y-m-d H:i:s',
60+
$this->dateTime->gmtTimestamp() - $this->config->getAuthenticationDataExpirationTime()
61+
);
5962

6063
$connection->delete(
6164
$tableName,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GetAuthenticationDataBySecret implements GetAuthenticationDataBySecretInte
3636
private $config;
3737

3838
/**
39-
* @var AuthenticationDataInterfaceFactor
39+
* @var AuthenticationDataInterfaceFactory
4040
*/
4141
private $authenticationDataFactory;
4242

@@ -66,7 +66,10 @@ public function execute(string $secretKey): AuthenticationDataInterface
6666
$connection = $this->resourceConnection->getConnection();
6767
$tableName = $this->resourceConnection->getTableName('login_as_customer');
6868

69-
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $this->config->getSecretExpirationTime());
69+
$timePoint = date(
70+
'Y-m-d H:i:s',
71+
$this->dateTime->gmtTimestamp() - $this->config->getAuthenticationDataExpirationTime()
72+
);
7073

7174
$select = $connection->select()
7275
->from(['main_table' => $tableName])

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
99
<group id="default">
10-
<job instance="Magento\LoginAsCustomer\Cron\DeleteOldSecrets" method="execute" name="login_as_customer_delete_old_secrets">
10+
<job instance="Magento\LoginAsCustomer\Cron\DeleteExpiredAuthenticationData"
11+
method="execute"
12+
name="login_as_customer_delete_expired_authentication_data">
1113
<schedule>15 * * * *</schedule>
1214
</job>
1315
</group>

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@
88
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
1010
<table name="login_as_customer" resource="default" engine="innodb" comment="Magento Login As Customer Table">
11-
<column xsi:type="int" name="login_id" nullable="false" identity="true"
12-
comment="Admin Login ID"/>
13-
<column xsi:type="int" name="customer_id" nullable="true" identity="false"
14-
comment="Customer ID"/>
15-
<column xsi:type="int" name="admin_id" nullable="true" identity="false"
16-
comment="Admin ID"/>
17-
<column xsi:type="varchar" name="secret" length="64" nullable="true" comment="Login Secret"/>
11+
<column xsi:type="varchar" name="secret" nullable="false" length="64" comment="Login Secret"/>
12+
<column xsi:type="int" name="customer_id" nullable="false" comment="Customer ID"/>
13+
<column xsi:type="int" name="admin_id" nullable="false" comment="Admin ID"/>
1814
<column xsi:type="timestamp" name="created_at" comment="Creation Time"/>
1915
<constraint xsi:type="primary" referenceId="PRIMARY">
20-
<column name="login_id"/>
16+
<column name="secret"/>
2117
</constraint>
22-
<index referenceId="LOGIN_AS_CUSTOMER_LOG_CUSTOMER_ID" indexType="btree">
23-
<column name="customer_id"/>
18+
<index referenceId="LOGIN_AS_CUSTOMER_SECRET_CREATED_AT" indexType="btree">
19+
<column name="secret"/>
20+
<column name="created_at"/>
2421
</index>
25-
<index referenceId="LOGIN_AS_CUSTOMER_LOG_ADMIN_ID" indexType="btree">
26-
<column name="admin_id"/>
22+
<index referenceId="LOGIN_AS_CUSTOMER_CREATED_AT" indexType="btree">
23+
<column name="created_at"/>
2724
</index>
2825
</table>
2926
</schema>

app/code/Magento/LoginAsCustomer/etc/db_schema_whitelist.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{
22
"login_as_customer": {
33
"column": {
4-
"login_id": true,
4+
"secret": true,
55
"customer_id": true,
66
"admin_id": true,
7-
"secret": true,
87
"created_at": true
98
},
109
"index": {
11-
"LOGIN_AS_CUSTOMER_LOG_CUSTOMER_ID_ADMIN_ID": true,
12-
"LOGIN_AS_CUSTOMER_LOG_CUSTOMER_ID": true,
13-
"LOGIN_AS_CUSTOMER_LOG_ADMIN_ID": true
10+
"LOGIN_AS_CUSTOMER_SECRET_CREATED_AT": true,
11+
"LOGIN_AS_CUSTOMER_CREATED_AT": true
1412
},
1513
"constraint": {
1614
"PRIMARY": true

0 commit comments

Comments
 (0)