Skip to content

Commit 8283653

Browse files
committed
B2B - restrict login if customer's company is not active
1 parent e63d309 commit 8283653

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

Controller/Adminhtml/Login/Login.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,32 @@ class Login extends \Magento\Backend\App\Action
1818
* @var \Magefan\LoginAsCustomer\Model\Login
1919
*/
2020
protected $loginModel;
21+
2122
/**
2223
* @var \Magento\Backend\Model\Auth\Session
2324
*/
2425
protected $authSession = null;
26+
2527
/**
2628
* @var \Magento\Store\Model\StoreManagerInterface
2729
*/
2830
protected $storeManager = null;
31+
2932
/**
3033
* @var \Magento\Framework\Url
3134
*/
3235
protected $url = null;
36+
3337
/**
3438
* @var \Magefan\LoginAsCustomer\Model\Config
3539
*/
3640
protected $config = null;
3741

42+
/**
43+
* @var \Magento\Customer\Api\CustomerRepositoryInterface
44+
*/
45+
protected $customerRepository = null;
46+
3847
/**
3948
* Login constructor.
4049
* @param \Magento\Backend\App\Action\Context $context
@@ -43,21 +52,24 @@ class Login extends \Magento\Backend\App\Action
4352
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
4453
* @param \Magento\Framework\Url|null $url
4554
* @param \Magefan\LoginAsCustomer\Model\Config|null $config
55+
* @param \Magento\Customer\Api\CustomerRepositoryInterface|null $customerRepository
4656
*/
4757
public function __construct(
4858
\Magento\Backend\App\Action\Context $context,
4959
\Magefan\LoginAsCustomer\Model\Login $loginModel = null,
5060
\Magento\Backend\Model\Auth\Session $authSession = null,
5161
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
5262
\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
5465
) {
5566
parent::__construct($context);
5667
$this->loginModel = $loginModel ?: $this->_objectManager->get(\Magefan\LoginAsCustomer\Model\Login::class);
5768
$this->authSession = $authSession ?: $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class);
5869
$this->storeManager = $storeManager ?: $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
5970
$this->url = $url ?: $this->_objectManager->get(\Magento\Framework\Url::class);
6071
$this->config = $config ?: $this->_objectManager->get(\Magefan\LoginAsCustomer\Model\Config::class);
72+
$this->customerRepository = $customerRepository ?: $this->_objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
6173
}
6274
/**
6375
* Login as customer action
@@ -103,6 +115,29 @@ public function execute()
103115
return $resultRedirect->setPath('customer/index/index');
104116
}
105117

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+
106141
$user = $this->authSession->getUser();
107142
$login->generate($user->getId());
108143

@@ -141,4 +176,13 @@ protected function _isAllowed()
141176
{
142177
return $this->_authorization->isAllowed('Magefan_LoginAsCustomer::login_button');
143178
}
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+
}
144188
}

0 commit comments

Comments
 (0)