11
11
use Magento \Framework \App \Action \HttpGetActionInterface ;
12
12
use Magento \Framework \App \Action \HttpPostActionInterface ;
13
13
use Magento \Backend \App \Action ;
14
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
15
+ use Magento \Framework \Exception \NoSuchEntityException ;
16
+ use Magento \LoginAsCustomer \Api \CreateSecretInterface ;
14
17
15
18
/**
16
19
* Login as customer action
@@ -27,11 +30,6 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
27
30
*/
28
31
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button ' ;
29
32
30
- /**
31
- * @var \Magento\LoginAsCustomer\Model\Login
32
- */
33
- private $ loginModel ;
34
-
35
33
/**
36
34
* @var \Magento\Backend\Model\Auth\Session
37
35
*/
@@ -47,34 +45,47 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
47
45
*/
48
46
private $ url ;
49
47
48
+ /**
49
+ * @var CustomerRepositoryInterface
50
+ */
51
+ private $ customerRepository ;
52
+
50
53
/**
51
54
* @var \Magento\LoginAsCustomer\Model\Config
52
55
*/
53
56
private $ config ;
54
57
58
+ /**
59
+ * @var CreateSecretInterface
60
+ */
61
+ private $ createSecretProcessor ;
62
+
55
63
/**
56
64
* Login constructor.
57
65
* @param \Magento\Backend\App\Action\Context $context
58
- * @param \Magento\LoginAsCustomer\Model\Login $loginModel
59
66
* @param \Magento\Backend\Model\Auth\Session $authSession
60
67
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
61
68
* @param \Magento\Framework\Url $url
62
- * @param \Magento\LoginAsCustomer\Model\Config $config
69
+ * @param CustomerRepositoryInterface $customerRepository
70
+ * @param \Magento\LoginAsCustomer\Model\Config $config,
71
+ * @param CreateSecretInterface $createSecretProcessor
63
72
*/
64
73
public function __construct (
65
74
\Magento \Backend \App \Action \Context $ context ,
66
- \Magento \LoginAsCustomer \Model \Login $ loginModel ,
67
75
\Magento \Backend \Model \Auth \Session $ authSession ,
68
76
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
69
77
\Magento \Framework \Url $ url ,
70
- \Magento \LoginAsCustomer \Model \Config $ config
78
+ CustomerRepositoryInterface $ customerRepository ,
79
+ \Magento \LoginAsCustomer \Model \Config $ config ,
80
+ CreateSecretInterface $ createSecretProcessor
71
81
) {
72
82
parent ::__construct ($ context );
73
- $ this ->loginModel = $ loginModel ;
74
83
$ this ->authSession = $ authSession ;
75
84
$ this ->storeManager = $ storeManager ;
76
85
$ this ->url = $ url ;
86
+ $ this ->customerRepository = $ customerRepository ;
77
87
$ this ->config = $ config ;
88
+ $ this ->createSecretProcessor = $ createSecretProcessor ;
78
89
}
79
90
80
91
/**
@@ -84,48 +95,45 @@ public function __construct(
84
95
*/
85
96
public function execute (): ResultInterface
86
97
{
98
+ /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
99
+ $ resultRedirect = $ this ->resultRedirectFactory ->create ();
100
+
101
+ if (!$ this ->config ->isEnabled ()) {
102
+ $ this ->messageManager ->addErrorMessage (__ ('Login As Customer is disabled. ' ));
103
+ return $ resultRedirect ->setPath ('customer/index/index ' );
104
+ }
105
+
87
106
$ request = $ this ->getRequest ();
107
+
88
108
$ customerId = (int ) $ request ->getParam ('customer_id ' );
89
109
if (!$ customerId ) {
90
110
$ customerId = (int ) $ request ->getParam ('entity_id ' );
91
111
}
92
112
93
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
94
- $ resultRedirect = $ this ->resultRedirectFactory ->create ();
95
-
96
- if (!$ this ->config ->isEnabled ()) {
97
- $ this ->messageManager ->addErrorMessage (__ ('Login As Customer is disabled. ' ));
113
+ try {
114
+ $ customer = $ this ->customerRepository ->getById ($ customerId );
115
+ } catch (NoSuchEntityException $ e ) {
116
+ $ this ->messageManager ->addErrorMessage (__ ('Customer with this ID are no longer exist. ' ));
98
117
return $ resultRedirect ->setPath ('customer/index/index ' );
99
118
}
100
119
101
120
$ customerStoreId = $ request ->getParam ('store_id ' );
102
-
103
121
if (!isset ($ customerStoreId ) && $ this ->config ->isManualChoiceEnabled ()) {
104
122
$ this ->messageManager ->addNoticeMessage (__ ('Please select a Store View to login in. ' ));
105
123
return $ resultRedirect ->setPath ('loginascustomer/login/manual ' , ['entity_id ' => $ customerId ]);
106
124
}
107
125
108
- $ login = $ this ->loginModel ->setCustomerId ($ customerId );
109
-
110
- $ login ->deleteNotUsed ();
111
-
112
- $ customer = $ login ->getCustomer ();
113
-
114
- if (!$ customer ->getId ()) {
115
- $ this ->messageManager ->addErrorMessage (__ ('Customer with this ID are no longer exist. ' ));
116
- return $ resultRedirect ->setPath ('customer/index/index ' );
117
- }
118
126
119
127
$ user = $ this ->authSession ->getUser ();
120
- $ login ->generate ($ user ->getId ());
121
- $ store = $ this ->storeManager ->getStore ();
128
+ $ secret = $ this ->createSecretProcessor ->execute ($ customerId , (int )$ user ->getId ());
122
129
130
+ $ store = $ this ->storeManager ->getStore ();
123
131
if (null === $ store ) {
124
132
$ store = $ this ->storeManager ->getDefaultStoreView ();
125
133
}
126
134
127
135
$ redirectUrl = $ this ->url ->setScope ($ store )
128
- ->getUrl ('loginascustomer/login/index ' , ['secret ' => $ login -> getSecret () , '_nosid ' => true ]);
136
+ ->getUrl ('loginascustomer/login/index ' , ['secret ' => $ secret , '_nosid ' => true ]);
129
137
130
138
return $ resultRedirect ->setUrl ($ redirectUrl );
131
139
}
0 commit comments