Skip to content

Commit 625e57e

Browse files
magento/magento2-login-as-customer#144: Session interfaces introduction.
1 parent 74a48fd commit 625e57e

File tree

16 files changed

+335
-37
lines changed

16 files changed

+335
-37
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
1313
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
14+
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface;
1415

1516
/**
1617
* @inheritdoc
@@ -29,16 +30,24 @@ class AuthenticateCustomerBySecret implements AuthenticateCustomerBySecretInterf
2930
*/
3031
private $customerSession;
3132

33+
/**
34+
* @var SetLoggedAsCustomerAdminIdInterface
35+
*/
36+
private $setLoggedAsCustomerAdminId;
37+
3238
/**
3339
* @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret
3440
* @param Session $customerSession
41+
* @param SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId
3542
*/
3643
public function __construct(
3744
GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret,
38-
Session $customerSession
45+
Session $customerSession,
46+
SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId
3947
) {
4048
$this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret;
4149
$this->customerSession = $customerSession;
50+
$this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId;
4251
}
4352

4453
/**
@@ -58,6 +67,6 @@ public function execute(string $secret): void
5867
}
5968

6069
$this->customerSession->regenerateId();
61-
$this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId());
70+
$this->setLoggedAsCustomerAdminId->execute($authenticationData->getAdminId());
6271
}
6372
}
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: 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);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataForUser"/>
1414
<preference for="Magento\LoginAsCustomerApi\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config"/>
1515
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
16+
<preference for="Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface" type="Magento\LoginAsCustomer\Model\GetLoggedAsCustomerAdminId"/>
17+
<preference for="Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface" type="Magento\LoginAsCustomer\Model\GetLoggedAsCustomerCustomerId"/>
18+
<preference for="Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface" type="Magento\LoginAsCustomer\Model\SetLoggedAsCustomerAdminId"/>
19+
<preference for="Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface" type="Magento\LoginAsCustomer\Model\SetLoggedAsCustomerCustomerId"/>
1620
<type name="Magento\Backend\Model\Auth">
1721
<plugin name="login_as_customer_admin_logout" type="Magento\LoginAsCustomer\Plugin\AdminLogoutPlugin"/>
1822
</type>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
2424
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
2525
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
26+
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface;
2627
use Magento\Store\Model\StoreManagerInterface;
2728

2829
/**
@@ -80,6 +81,11 @@ class Login extends Action implements HttpGetActionInterface
8081
*/
8182
private $url;
8283

84+
/**
85+
* @var SetLoggedAsCustomerCustomerIdInterface
86+
*/
87+
private $setLoggedAsCustomerCustomerId;
88+
8389
/**
8490
* @param Context $context
8591
* @param Session $authSession
@@ -90,6 +96,9 @@ class Login extends Action implements HttpGetActionInterface
9096
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
9197
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
9298
* @param Url $url
99+
* @param SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId
100+
*
101+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
93102
*/
94103
public function __construct(
95104
Context $context,
@@ -100,7 +109,8 @@ public function __construct(
100109
AuthenticationDataInterfaceFactory $authenticationDataFactory,
101110
SaveAuthenticationDataInterface $saveAuthenticationData,
102111
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser,
103-
Url $url
112+
Url $url,
113+
SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId
104114
) {
105115
parent::__construct($context);
106116

@@ -112,6 +122,7 @@ public function __construct(
112122
$this->saveAuthenticationData = $saveAuthenticationData;
113123
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
114124
$this->url = $url;
125+
$this->setLoggedAsCustomerCustomerId = $setLoggedAsCustomerCustomerId;
115126
}
116127

117128
/**
@@ -167,7 +178,7 @@ public function execute(): ResultInterface
167178

168179
$this->deleteAuthenticationDataForUser->execute($userId);
169180
$secret = $this->saveAuthenticationData->execute($authenticationData);
170-
$this->authSession->setIsLoggedAsCustomer(true);
181+
$this->setLoggedAsCustomerCustomerId->execute($customerId);
171182

172183
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
173184
$resultRedirect->setUrl($redirectUrl);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
* Get id of Admin logged as Customer.
12+
*
13+
* @api
14+
*/
15+
interface GetLoggedAsCustomerAdminIdInterface
16+
{
17+
/**
18+
* Get id of Admin logged as Customer.
19+
*
20+
* @return int
21+
*/
22+
public function execute(): int;
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
* Get id of Customer Admin is logged as.
12+
*
13+
* @api
14+
*/
15+
interface GetLoggedAsCustomerCustomerIdInterface
16+
{
17+
/**
18+
* Get id of Customer Admin is logged as.
19+
*
20+
* @return int
21+
*/
22+
public function execute(): int;
23+
}

0 commit comments

Comments
 (0)