@@ -18,23 +18,32 @@ class Login extends \Magento\Backend\App\Action
18
18
* @var \Magefan\LoginAsCustomer\Model\Login
19
19
*/
20
20
protected $ loginModel ;
21
+
21
22
/**
22
23
* @var \Magento\Backend\Model\Auth\Session
23
24
*/
24
25
protected $ authSession = null ;
26
+
25
27
/**
26
28
* @var \Magento\Store\Model\StoreManagerInterface
27
29
*/
28
30
protected $ storeManager = null ;
31
+
29
32
/**
30
33
* @var \Magento\Framework\Url
31
34
*/
32
35
protected $ url = null ;
36
+
33
37
/**
34
38
* @var \Magefan\LoginAsCustomer\Model\Config
35
39
*/
36
40
protected $ config = null ;
37
41
42
+ /**
43
+ * @var \Magento\Customer\Api\CustomerRepositoryInterface
44
+ */
45
+ protected $ customerRepository = null ;
46
+
38
47
/**
39
48
* Login constructor.
40
49
* @param \Magento\Backend\App\Action\Context $context
@@ -43,21 +52,24 @@ class Login extends \Magento\Backend\App\Action
43
52
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
44
53
* @param \Magento\Framework\Url|null $url
45
54
* @param \Magefan\LoginAsCustomer\Model\Config|null $config
55
+ * @param \Magento\Customer\Api\CustomerRepositoryInterface|null $customerRepository
46
56
*/
47
57
public function __construct (
48
58
\Magento \Backend \App \Action \Context $ context ,
49
59
\Magefan \LoginAsCustomer \Model \Login $ loginModel = null ,
50
60
\Magento \Backend \Model \Auth \Session $ authSession = null ,
51
61
\Magento \Store \Model \StoreManagerInterface $ storeManager = null ,
52
62
\Magento \Framework \Url $ url = null ,
53
- \Magefan \LoginAsCustomer \Model \Config $ config = null
63
+ \Magefan \LoginAsCustomer \Model \Config $ config = null ,
64
+ \Magento \Customer \Api \CustomerRepositoryInterface $ customerRepository = null
54
65
) {
55
66
parent ::__construct ($ context );
56
67
$ this ->loginModel = $ loginModel ?: $ this ->_objectManager ->get (\Magefan \LoginAsCustomer \Model \Login::class);
57
68
$ this ->authSession = $ authSession ?: $ this ->_objectManager ->get (\Magento \Backend \Model \Auth \Session::class);
58
69
$ this ->storeManager = $ storeManager ?: $ this ->_objectManager ->get (\Magento \Store \Model \StoreManagerInterface::class);
59
70
$ this ->url = $ url ?: $ this ->_objectManager ->get (\Magento \Framework \Url::class);
60
71
$ this ->config = $ config ?: $ this ->_objectManager ->get (\Magefan \LoginAsCustomer \Model \Config::class);
72
+ $ this ->customerRepository = $ customerRepository ?: $ this ->_objectManager ->get (\Magento \Customer \Api \CustomerRepositoryInterface::class);
61
73
}
62
74
/**
63
75
* Login as customer action
@@ -103,6 +115,29 @@ public function execute()
103
115
return $ resultRedirect ->setPath ('customer/index/index ' );
104
116
}
105
117
118
+ /* Check if customer's company is active */
119
+ $ tmpCustomer = $ this ->customerRepository ->getById ($ customer ->getId ());
120
+ if ($ tmpCustomer ->getExtensionAttributes () !== null ) {
121
+ $ companyAttributes = null ;
122
+ if (method_exists ($ tmpCustomer ->getExtensionAttributes (), 'getCompanyAttributes ' )) {
123
+ $ companyAttributes = $ tmpCustomer ->getExtensionAttributes ()->getCompanyAttributes ();
124
+ }
125
+
126
+ if ($ companyAttributes !== null ) {
127
+ $ companyId = $ companyAttributes ->getCompanyId ();
128
+ if ($ companyId ) {
129
+ try {
130
+ $ company = $ this ->getCompanyRepository ()->get ($ companyId );
131
+ if ($ company ->getStatus () != 1 ) {
132
+ $ this ->messageManager ->addErrorMessage (__ ('You cannot login as customer. Customer \'s company is not active. ' ));
133
+ return $ resultRedirect ->setPath ('customer/index/index ' );
134
+ }
135
+ } catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {}
136
+ }
137
+ }
138
+ }
139
+ /* End check */
140
+
106
141
$ user = $ this ->authSession ->getUser ();
107
142
$ login ->generate ($ user ->getId ());
108
143
@@ -141,4 +176,13 @@ protected function _isAllowed()
141
176
{
142
177
return $ this ->_authorization ->isAllowed ('Magefan_LoginAsCustomer::login_button ' );
143
178
}
179
+
180
+ /**
181
+ * Retrieve Company Repository
182
+ * @return \Magento\Company\Api\CompanyRepositoryInterface
183
+ */
184
+ protected function getCompanyRepository ()
185
+ {
186
+ return $ this ->_objectManager ->get (\Magento \Company \Api \CompanyRepositoryInterface::class);
187
+ }
144
188
}
0 commit comments