Skip to content

Commit dae258b

Browse files
authored
Magento2 login as customer/issues/104
2 parents bf65ffc + 7a33d67 commit dae258b

File tree

91 files changed

+1176
-633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1176
-633
lines changed

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

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

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

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

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

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

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

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

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\LoginAsCustomer\Cron;
9+
10+
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
11+
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
12+
13+
/**
14+
* elete expired authentication data cron task
15+
*/
16+
class DeleteExpiredAuthenticationData
17+
{
18+
/**
19+
* @var DeleteExpiredAuthenticationDataInterface
20+
*/
21+
private $deleteOldSecretsProcessor;
22+
23+
/**
24+
* @var ConfigInterface
25+
*/
26+
private $config;
27+
28+
/**
29+
* @param DeleteExpiredAuthenticationDataInterface $deleteOldSecretsProcessor
30+
* @param ConfigInterface $config
31+
*/
32+
public function __construct(
33+
DeleteExpiredAuthenticationDataInterface $deleteOldSecretsProcessor,
34+
ConfigInterface $config
35+
) {
36+
$this->deleteOldSecretsProcessor = $deleteOldSecretsProcessor;
37+
$this->config = $config;
38+
}
39+
40+
/**
41+
* elete expired authentication data
42+
*/
43+
public function execute(): void
44+
{
45+
if ($this->config->isEnabled()) {
46+
$this->deleteOldSecretsProcessor->execute();
47+
}
48+
}
49+
}

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

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

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

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

1010
use Magento\Customer\Model\Session;
11-
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerInterface;
13+
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
1214

1315
/**
14-
* @api
16+
* @inheritdoc
1517
*/
1618
class AuthenticateCustomer implements AuthenticateCustomerInterface
1719
{
@@ -21,7 +23,6 @@ class AuthenticateCustomer implements AuthenticateCustomerInterface
2123
private $customerSession;
2224

2325
/**
24-
* AuthenticateCustomer constructor.
2526
* @param Session $customerSession
2627
*/
2728
public function __construct(
@@ -31,25 +32,20 @@ public function __construct(
3132
}
3233

3334
/**
34-
* Authenticate a customer by customer ID
35-
*
36-
* @return bool
37-
* @param int $customerId
38-
* @param int $adminId
35+
* @inheritdoc
3936
*/
40-
public function execute(int $customerId, int $adminId):bool
37+
public function execute(AuthenticationDataInterface $authenticationData): void
4138
{
4239
if ($this->customerSession->getId()) {
43-
/* Logout if logged in */
4440
$this->customerSession->logout();
4541
}
4642

47-
$loggedIn = $this->customerSession->loginById($customerId);
48-
if ($loggedIn) {
49-
$this->customerSession->regenerateId();
50-
$this->customerSession->setLoggedAsCustomerAdmindId($adminId);
43+
$result = $this->customerSession->loginById($authenticationData->getCustomerId());
44+
if (false === $result) {
45+
throw new LocalizedException(__('Login was not successful.'));
5146
}
5247

53-
return $loggedIn;
48+
$this->customerSession->regenerateId();
49+
$this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId());
5450
}
5551
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\LoginAsCustomer\Model;
9+
10+
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
11+
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataExtensionInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class AuthenticationData implements AuthenticationDataInterface
17+
{
18+
/**
19+
* @var int
20+
*/
21+
private $customerId;
22+
23+
/**
24+
* @var int
25+
*/
26+
private $adminId;
27+
28+
/**
29+
* @var AuthenticationDataExtensionInterface|null
30+
*/
31+
private $extensionAttributes;
32+
33+
/**
34+
* @param int $customerId
35+
* @param int $adminId
36+
* @param AuthenticationDataExtensionInterface|null $extensionAttributes
37+
*/
38+
public function __construct(
39+
int $customerId,
40+
int $adminId,
41+
AuthenticationDataExtensionInterface $extensionAttributes = null
42+
) {
43+
$this->customerId = $customerId;
44+
$this->adminId = $adminId;
45+
$this->extensionAttributes = $extensionAttributes;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
public function getCustomerId(): int
52+
{
53+
return $this->customerId;
54+
}
55+
56+
/**
57+
* @return int
58+
*/
59+
public function getAdminId(): int
60+
{
61+
return $this->adminId;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function getExtensionAttributes(): ?AuthenticationDataExtensionInterface
68+
{
69+
return $this->extensionAttributes;
70+
}
71+
}

0 commit comments

Comments
 (0)