Skip to content

Commit 52e6554

Browse files
committed
Merge remote-tracking branch 'mainline/2.4-develop' into 2.4-develop-pr34
2 parents d55c219 + e12a2fd commit 52e6554

35 files changed

+898
-121
lines changed

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

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

1010
use Magento\Customer\Model\Session;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
1314
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
15+
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface;
1416

1517
/**
1618
* @inheritdoc
@@ -29,16 +31,25 @@ class AuthenticateCustomerBySecret implements AuthenticateCustomerBySecretInterf
2931
*/
3032
private $customerSession;
3133

34+
/**
35+
* @var SetLoggedAsCustomerAdminIdInterface
36+
*/
37+
private $setLoggedAsCustomerAdminId;
38+
3239
/**
3340
* @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret
3441
* @param Session $customerSession
42+
* @param SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId
3543
*/
3644
public function __construct(
3745
GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret,
38-
Session $customerSession
46+
Session $customerSession,
47+
?SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId = null
3948
) {
4049
$this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret;
4150
$this->customerSession = $customerSession;
51+
$this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId
52+
?? ObjectManager::getInstance()->get(SetLoggedAsCustomerAdminIdInterface::class);
4253
}
4354

4455
/**
@@ -58,6 +69,6 @@ public function execute(string $secret): void
5869
}
5970

6071
$this->customerSession->regenerateId();
61-
$this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId());
72+
$this->setLoggedAsCustomerAdminId->execute($authenticationData->getAdminId());
6273
}
6374
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Customer\Model\Session;
11+
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class GetLoggedAsCustomerAdminId implements GetLoggedAsCustomerAdminIdInterface
19+
{
20+
/**
21+
* @var Session
22+
*/
23+
private $session;
24+
25+
/**
26+
* @param Session $session
27+
*/
28+
public function __construct(Session $session)
29+
{
30+
$this->session = $session;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function execute(): int
37+
{
38+
return (int)$this->session->getLoggedAsCustomerAdmindId();
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Backend\Model\Auth\Session;
11+
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class GetLoggedAsCustomerCustomerId implements GetLoggedAsCustomerCustomerIdInterface
19+
{
20+
/**
21+
* @var Session
22+
*/
23+
private $session;
24+
25+
/**
26+
* @param Session $session
27+
*/
28+
public function __construct(Session $session)
29+
{
30+
$this->session = $session;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function execute(): int
37+
{
38+
return (int)$this->session->getLoggedAsCustomerCustomerId();
39+
}
40+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\ConfigInterface;
11+
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
12+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
13+
use Magento\LoginAsCustomerApi\Model\IsLoginAsCustomerEnabledForCustomerResolverInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class IsLoginAsCustomerEnabledForCustomerChain implements IsLoginAsCustomerEnabledForCustomerInterface
19+
{
20+
/**
21+
* @var ConfigInterface
22+
*/
23+
private $config;
24+
25+
/**
26+
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
27+
*/
28+
private $resultFactory;
29+
30+
/**
31+
* @var IsLoginAsCustomerEnabledForCustomerResultInterface[]
32+
*/
33+
private $resolvers;
34+
35+
/**
36+
* @param ConfigInterface $config
37+
* @param IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
38+
* @param array $resolvers
39+
*/
40+
public function __construct(
41+
ConfigInterface $config,
42+
IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory,
43+
array $resolvers = []
44+
) {
45+
$this->config = $config;
46+
$this->resultFactory = $resultFactory;
47+
$this->resolvers = $resolvers;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
54+
{
55+
$messages = [[]];
56+
/** @var IsLoginAsCustomerEnabledForCustomerResultInterface $resolver */
57+
foreach ($this->resolvers as $resolver) {
58+
$resolverResult = $resolver->execute($customerId);
59+
if (!$resolverResult->isEnabled()) {
60+
$messages[] = $resolverResult->getMessages();
61+
}
62+
}
63+
64+
return $this->resultFactory->create(['messages' => array_merge(...$messages)]);
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\IsLoginAsCustomerEnabledForCustomerResultInterface;
11+
12+
/**
13+
* @inheritdoc
14+
*/
15+
class IsLoginAsCustomerEnabledForCustomerResult implements IsLoginAsCustomerEnabledForCustomerResultInterface
16+
{
17+
/**
18+
* @var string[]
19+
*/
20+
private $messages;
21+
22+
/**
23+
* @param array $messages
24+
*/
25+
public function __construct(array $messages = [])
26+
{
27+
$this->messages = $messages;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function isEnabled(): bool
34+
{
35+
return empty($this->messages);
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getMessages(): array
42+
{
43+
return $this->messages;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function setMessages(array $messages): void
50+
{
51+
$this->messages = $messages;
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Resolver;
9+
10+
use Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerResultFactory;
11+
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
12+
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
13+
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class IsLoginAsCustomerEnabledResolver implements IsLoginAsCustomerEnabledForCustomerInterface
19+
{
20+
/**
21+
* @var ConfigInterface
22+
*/
23+
private $config;
24+
25+
/**
26+
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
27+
*/
28+
private $resultFactory;
29+
30+
/**
31+
* @param ConfigInterface $config
32+
* @param IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
33+
*/
34+
public function __construct(
35+
ConfigInterface $config,
36+
IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
37+
) {
38+
$this->config = $config;
39+
$this->resultFactory = $resultFactory;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
46+
{
47+
$messages = [];
48+
if (!$this->config->isEnabled()) {
49+
$messages[] = __('Login as Customer is disabled.');
50+
}
51+
52+
return $this->resultFactory->create(['messages' => $messages]);
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Customer\Model\Session;
11+
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class SetLoggedAsCustomerAdminId implements SetLoggedAsCustomerAdminIdInterface
19+
{
20+
/**
21+
* @var Session
22+
*/
23+
private $session;
24+
25+
/**
26+
* @param Session $session
27+
*/
28+
public function __construct(Session $session)
29+
{
30+
$this->session = $session;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function execute(int $adminId): void
37+
{
38+
$this->session->setLoggedAsCustomerAdmindId($adminId);
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Backend\Model\Auth\Session;
11+
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class SetLoggedAsCustomerCustomerId implements SetLoggedAsCustomerCustomerIdInterface
19+
{
20+
/**
21+
* @var Session
22+
*/
23+
private $session;
24+
25+
/**
26+
* @param Session $session
27+
*/
28+
public function __construct(Session $session)
29+
{
30+
$this->session = $session;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function execute(int $customerId): void
37+
{
38+
$this->session->setLoggedAsCustomerCustomerId($customerId);
39+
}
40+
}

0 commit comments

Comments
 (0)