7
7
8
8
namespace Magento \LoginAsCustomerUi \Controller \Adminhtml \Login ;
9
9
10
+ use Magento \Framework \App \RequestInterface ;
11
+ use Magento \Framework \Controller \Result \Redirect ;
12
+ use Magento \Framework \Controller \ResultFactory ;
10
13
use Magento \Framework \Controller \ResultInterface ;
11
14
use Magento \Framework \App \Action \HttpGetActionInterface ;
12
15
use Magento \Framework \App \Action \HttpPostActionInterface ;
13
- use Magento \Backend \App \Action ;
14
16
use Magento \Customer \Api \CustomerRepositoryInterface ;
17
+ use Magento \Framework \Exception \LocalizedException ;
15
18
use Magento \Framework \Exception \NoSuchEntityException ;
19
+ use Magento \Framework \Message \ManagerInterface ;
16
20
use Magento \LoginAsCustomer \Api \ConfigInterface ;
17
- use Magento \LoginAsCustomer \Api \CreateSecretInterface ;
21
+ use Magento \LoginAsCustomer \Api \Data \AuthenticationDataInterface ;
22
+ use Magento \LoginAsCustomer \Api \Data \AuthenticationDataInterfaceFactor ;
23
+ use Magento \LoginAsCustomer \Api \SaveAuthenticationDataInterface ;
18
24
19
25
/**
20
26
* Login as customer action
21
27
* Generate secret key and forward to the storefront action
22
28
*
23
29
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
24
30
*/
25
- class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
31
+ class Login implements HttpGetActionInterface, HttpPostActionInterface
26
32
{
27
33
/**
28
34
* Authorization level of a basic admin session
@@ -31,6 +37,21 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
31
37
*/
32
38
const ADMIN_RESOURCE = 'Magento_LoginAsCustomerUi::login_button ' ;
33
39
40
+ /**
41
+ * @var ResultFactory
42
+ */
43
+ private $ resultFactory ;
44
+
45
+ /**
46
+ * @var RequestInterface
47
+ */
48
+ private $ request ;
49
+
50
+ /**
51
+ * @var ManagerInterface
52
+ */
53
+ private $ messageManager ;
54
+
34
55
/**
35
56
* @var \Magento\Backend\Model\Auth\Session
36
57
*/
@@ -57,58 +78,71 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
57
78
private $ config ;
58
79
59
80
/**
60
- * @var CreateSecretInterface
81
+ * @var AuthenticationDataInterfaceFactory
61
82
*/
62
- private $ createSecretProcessor ;
83
+ private $ authenticationDataFactory ;
63
84
64
85
/**
65
- * Login constructor.
66
- * @param \Magento\Backend\App\Action\Context $context
86
+ * @var SaveAuthenticationDataInterface
87
+ */
88
+ private $ saveAuthenticationData ;
89
+
90
+ /**
91
+ * @param ResultFactory $resultFactory
92
+ * @param RequestInterface $request
93
+ * @param ManagerInterface $messageManager
67
94
* @param \Magento\Backend\Model\Auth\Session $authSession
68
95
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
69
96
* @param \Magento\Framework\Url $url
70
97
* @param CustomerRepositoryInterface $customerRepository
71
- * @param ConfigInterface $config,
72
- * @param CreateSecretInterface $createSecretProcessor
98
+ * @param ConfigInterface $config
99
+ * @param AuthenticationDataInterfaceFactory $authenticationDataFactory
100
+ * @param SaveAuthenticationDataInterface $saveAuthenticationData
73
101
*/
74
102
public function __construct (
75
- \Magento \Backend \App \Action \Context $ context ,
103
+ ResultFactory $ resultFactory ,
104
+ RequestInterface $ request ,
105
+ ManagerInterface $ messageManager ,
76
106
\Magento \Backend \Model \Auth \Session $ authSession ,
77
107
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
78
108
\Magento \Framework \Url $ url ,
79
109
CustomerRepositoryInterface $ customerRepository ,
80
110
ConfigInterface $ config ,
81
- CreateSecretInterface $ createSecretProcessor
111
+ AuthenticationDataInterfaceFactory $ authenticationDataFactory ,
112
+ SaveAuthenticationDataInterface $ saveAuthenticationData
82
113
) {
83
- parent ::__construct ($ context );
114
+ $ this ->resultFactory = $ resultFactory ;
115
+ $ this ->request = $ request ;
116
+ $ this ->messageManager = $ messageManager ;
84
117
$ this ->authSession = $ authSession ;
85
118
$ this ->storeManager = $ storeManager ;
86
119
$ this ->url = $ url ;
87
120
$ this ->customerRepository = $ customerRepository ;
88
121
$ this ->config = $ config ;
89
- $ this ->createSecretProcessor = $ createSecretProcessor ;
122
+ $ this ->authenticationDataFactory = $ authenticationDataFactory ;
123
+ $ this ->saveAuthenticationData = $ saveAuthenticationData ;
90
124
}
91
125
92
126
/**
93
127
* Login as customer
94
128
*
95
129
* @return ResultInterface
130
+ * @throws NoSuchEntityException
131
+ * @throws LocalizedException
96
132
*/
97
133
public function execute (): ResultInterface
98
134
{
99
- /** @var \Magento\Backend\Model\View\Result\ Redirect $resultRedirect */
100
- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
135
+ /** @var Redirect $resultRedirect */
136
+ $ resultRedirect = $ this ->resultFactory ->create (ResultFactory:: TYPE_REDIRECT );
101
137
102
138
if (!$ this ->config ->isEnabled ()) {
103
139
$ this ->messageManager ->addErrorMessage (__ ('Login As Customer is disabled. ' ));
104
140
return $ resultRedirect ->setPath ('customer/index/index ' );
105
141
}
106
142
107
- $ request = $ this ->getRequest ();
108
-
109
- $ customerId = (int ) $ request ->getParam ('customer_id ' );
143
+ $ customerId = (int )$ this ->request ->getParam ('customer_id ' );
110
144
if (!$ customerId ) {
111
- $ customerId = (int ) $ request ->getParam ('entity_id ' );
145
+ $ customerId = (int )$ this -> request ->getParam ('entity_id ' );
112
146
}
113
147
114
148
try {
@@ -118,24 +152,46 @@ public function execute(): ResultInterface
118
152
return $ resultRedirect ->setPath ('customer/index/index ' );
119
153
}
120
154
121
- $ customerStoreId = $ request ->getParam ('store_id ' );
122
- if (! isset ( $ customerStoreId ) && $ this ->config ->isStoreManualChoiceEnabled ()) {
155
+ $ storeId = $ this -> request ->getParam ('store_id ' );
156
+ if (empty ( $ storeId ) && $ this ->config ->isStoreManualChoiceEnabled ()) {
123
157
$ this ->messageManager ->addNoticeMessage (__ ('Please select a Store View to login in. ' ));
124
- return $ resultRedirect ->setPath ('loginascustomer/login/manual ' , ['entity_id ' => $ customerId ]);
158
+ return $ resultRedirect ->setPath ('loginascustomer/login/manual ' , ['customer_id ' => $ customerId ]);
125
159
}
126
160
161
+ $ adminUser = $ this ->authSession ->getUser ();
162
+
163
+ /** @var AuthenticationDataInterface $authenticationData */
164
+ $ authenticationData = $ this ->authenticationDataFactory ->create (
165
+ [
166
+ 'customerId ' => $ customerId ,
167
+ 'adminId ' => (int )$ adminUser ->getId (),
168
+ 'extensionAttributes ' => null ,
169
+ ]
170
+ );
171
+ $ secret = $ this ->saveAuthenticationData ->execute ($ authenticationData );
172
+
173
+ $ redirectUrl = $ this ->getLoginProceedRedirectUrl ($ secret , $ storeId );
174
+ $ resultRedirect ->setUrl ($ redirectUrl );
175
+ return $ resultRedirect ;
176
+ }
127
177
128
- $ user = $ this ->authSession ->getUser ();
129
- $ secret = $ this ->createSecretProcessor ->execute ($ customerId , (int )$ user ->getId ());
130
-
131
- $ store = $ this ->storeManager ->getStore ();
132
- if (null === $ store ) {
178
+ /**
179
+ * @param string $secret
180
+ * @param int|null $storeId
181
+ * @return string
182
+ * @throws NoSuchEntityException
183
+ */
184
+ private function getLoginProceedRedirectUrl (string $ secret , ?int $ storeId ): string
185
+ {
186
+ if (null === $ storeId ) {
133
187
$ store = $ this ->storeManager ->getDefaultStoreView ();
188
+ } else {
189
+ $ store = $ this ->storeManager ->getStore ($ storeId );
134
190
}
135
191
136
- $ redirectUrl = $ this ->url ->setScope ($ store )
192
+ $ redirectUrl = $ this ->url
193
+ ->setScope ($ store )
137
194
->getUrl ('loginascustomer/login/index ' , ['secret ' => $ secret , '_nosid ' => true ]);
138
-
139
- return $ resultRedirect ->setUrl ($ redirectUrl );
195
+ return $ redirectUrl ;
140
196
}
141
197
}
0 commit comments