diff --git a/vendor/magento/module-company/Controller/Account/Create.php b/vendor/magento/module-company/Controller/Account/Create.php index 5fead536b..cf41229aa 100644 --- a/vendor/magento/module-company/Controller/Account/Create.php +++ b/vendor/magento/module-company/Controller/Account/Create.php @@ -5,14 +5,18 @@ */ namespace Magento\Company\Controller\Account; +use InvalidArgumentException; +use Magento\Company\Api\CompanyManagementInterface; use Magento\Customer\Model\Session; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; +use Magento\Framework\View\Result\Page; /** - * Class Create + * Controller Class Create to render Company Form page */ class Create extends Action implements HttpGetActionInterface { @@ -21,33 +25,47 @@ class Create extends Action implements HttpGetActionInterface */ private $customerSession; + /** + * @var CompanyManagementInterface|null + */ + private $companyManagement; + /** * @param Context $context * @param Session $customerSession + * @param CompanyManagementInterface|null $companyManagement */ public function __construct( Context $context, - Session $customerSession + Session $customerSession, + CompanyManagementInterface $companyManagement = null ) { - $this->customerSession = $customerSession; parent::__construct($context); + $this->customerSession = $customerSession; + $this->companyManagement = $companyManagement ?: + ObjectManager::getInstance()->get(CompanyManagementInterface::class); } /** * @inheritdoc * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function execute() { if ($this->customerSession->isLoggedIn()) { - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ - $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); - $resultRedirect->setPath('customer/account/'); - return $resultRedirect; + $company = $this->companyManagement->getByCustomerId($this->customerSession->getCustomerId()); + if ($company) { + /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ + $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD); + $resultForward->setModule('company'); + $resultForward->setController('accessdenied'); + $resultForward->forward('index'); + return $resultForward; + } } - /** @var \Magento\Framework\View\Result\Page $resultPage */ + /** @var Page $resultPage */ $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE); $resultPage->getConfig()->getTitle()->set(__('New Company')); return $resultPage; diff --git a/vendor/magento/module-company/Controller/Account/CreatePost.php b/vendor/magento/module-company/Controller/Account/CreatePost.php index dbce306c6..7a4d4349a 100644 --- a/vendor/magento/module-company/Controller/Account/CreatePost.php +++ b/vendor/magento/module-company/Controller/Account/CreatePost.php @@ -11,6 +11,9 @@ use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterf /** * Create company account action. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface { @@ -59,6 +62,11 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos */ private $companyCreateSession; + /** + * @var \Magento\Company\Model\CompanyUser|null + */ + private $companyUser; + /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Authorization\Model\UserContextInterface $userContext @@ -69,6 +77,7 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos * @param \Magento\Customer\Api\AccountManagementInterface $customerAccountManagement * @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory * @param \Magento\Company\Model\Create\Session $companyCreateSession + * @param \Magento\Company\Model\CompanyUser|null $companyUser */ public function __construct( \Magento\Framework\App\Action\Context $context, @@ -79,7 +88,8 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos \Magento\Company\Model\Action\Validator\Captcha $captchaValidator, \Magento\Customer\Api\AccountManagementInterface $customerAccountManagement, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory, - \Magento\Company\Model\Create\Session $companyCreateSession + \Magento\Company\Model\Create\Session $companyCreateSession, + \Magento\Company\Model\CompanyUser $companyUser = null ) { parent::__construct($context); $this->userContext = $userContext; @@ -90,6 +100,8 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos $this->customerAccountManagement = $customerAccountManagement; $this->customerDataFactory = $customerDataFactory; $this->companyCreateSession = $companyCreateSession; + $this->companyUser = $companyUser ?: + \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Company\Model\CompanyUser::class); } /** @@ -105,6 +117,16 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos } try { + if ($this->checkIfLoggedCustomerIsACompanyMember()) { + /** @var \Magento\Framework\Controller\Result\Forward $resultForward */ + $resultForward = $this->resultFactory + ->create(\Magento\Framework\Controller\ResultFactory::TYPE_FORWARD); + $resultForward->setModule('company'); + $resultForward->setController('accessdenied'); + $resultForward->forward('index'); + return $resultForward; + } + $customer = $this->customerDataFactory->create(); $customerData = $request->getPost('customer', []); @@ -136,7 +158,7 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos * * @return bool */ - private function validateRequest() + private function validateRequest(): bool { if (!$this->getRequest()->isPost()) { return false; @@ -153,4 +175,19 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos return true; } + + /** + * Method checks if logged customer is a company customer + * + * @return bool + * @throws \Magento\Framework\Exception\LocalizedException + */ + private function checkIfLoggedCustomerIsACompanyMember(): bool + { + try { + return (bool)$this->companyUser->getCurrentCompanyId(); + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { + return false; + } + } }