Skip to content

Commit 295ff43

Browse files
committed
Merge remote-tracking branch 'origin/144+188' into login-as-customer-prs
# Conflicts: # app/code/Magento/LoginAsCustomer/etc/di.xml
2 parents cb4d87a + d6562e3 commit 295ff43

30 files changed

+877
-111
lines changed
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+
}

app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php

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

1010
use Magento\Backend\Model\Auth;
11-
use Magento\Backend\Model\Auth\Session as AuthSession;
1211
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
1312
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
13+
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface;
1414

1515
/**
1616
* Delete all Login as Customer sessions for logging out admin.
@@ -19,11 +19,6 @@
1919
*/
2020
class AdminLogoutPlugin
2121
{
22-
/**
23-
* @var AuthSession
24-
*/
25-
private $authSession;
26-
2722
/**
2823
* @var ConfigInterface
2924
*/
@@ -35,18 +30,23 @@ class AdminLogoutPlugin
3530
private $deleteAuthenticationDataForUser;
3631

3732
/**
38-
* @param AuthSession $authSession
33+
* @var GetLoggedAsCustomerCustomerIdInterface
34+
*/
35+
private $getLoggedAsCustomerCustomerId;
36+
37+
/**
3938
* @param ConfigInterface $config
4039
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
40+
* @param GetLoggedAsCustomerCustomerIdInterface $getLoggedAsCustomerCustomerId
4141
*/
4242
public function __construct(
43-
AuthSession $authSession,
4443
ConfigInterface $config,
45-
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
44+
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser,
45+
GetLoggedAsCustomerCustomerIdInterface $getLoggedAsCustomerCustomerId
4646
) {
47-
$this->authSession = $authSession;
4847
$this->config = $config;
4948
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
49+
$this->getLoggedAsCustomerCustomerId = $getLoggedAsCustomerCustomerId;
5050
}
5151

5252
/**
@@ -57,7 +57,7 @@ public function __construct(
5757
public function beforeLogout(Auth $subject): void
5858
{
5959
$user = $subject->getUser();
60-
$isLoggedAsCustomer = $this->authSession->getIsLoggedAsCustomer();
60+
$isLoggedAsCustomer = (bool)$this->getLoggedAsCustomerCustomerId->execute();
6161
if ($this->config->isEnabled() && $user && $isLoggedAsCustomer) {
6262
$userId = (int)$user->getId();
6363
$this->deleteAuthenticationDataForUser->execute($userId);

0 commit comments

Comments
 (0)