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 11ccc27a9..7a4d4349a 100644 --- a/vendor/magento/module-company/Controller/Account/CreatePost.php +++ b/vendor/magento/module-company/Controller/Account/CreatePost.php @@ -3,12 +3,17 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Company\Controller\Account; use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; /** - * Class CreatePost + * Create company account action. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface { @@ -57,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 @@ -67,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, @@ -77,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; @@ -88,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); } /** @@ -103,13 +117,19 @@ class CreatePost extends \Magento\Framework\App\Action\Action implements HttpPos } try { - $customer = $this->customerDataFactory->create(); - $customerData = $request->getParams(); - - if (isset($customerData['company']['status'])) { - unset($customerData['company']['status']); + 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', []); + $this->objectHelper->populateWithArray( $customer, $customerData, @@ -138,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; @@ -155,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; + } + } } diff --git a/vendor/magento/module-company/Plugin/Customer/Api/AccountManagement.php b/vendor/magento/module-company/Plugin/Customer/Api/AccountManagement.php index 09a08deff..16798e1e7 100644 --- a/vendor/magento/module-company/Plugin/Customer/Api/AccountManagement.php +++ b/vendor/magento/module-company/Plugin/Customer/Api/AccountManagement.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Company\Plugin\Customer\Api; use Magento\Company\Api\Data\CompanyInterface; @@ -11,7 +13,7 @@ use Magento\Company\Api\CompanyManagementInterface; use Magento\Framework\Exception\NoSuchEntityException; /** - * Class AccountManagement + * Plugin for AccountManagement. Processing company data. */ class AccountManagement { @@ -35,6 +37,16 @@ class AccountManagement */ private $customerCompany; + /** + * @var CompanyManagementInterface + */ + private $companyManagement; + + /** + * @var CustomerRepositoryInterface + */ + private $customerRepository; + /** * AccountManagement constructor * @@ -115,14 +127,13 @@ class AccountManagement \Magento\Customer\Api\AccountManagementInterface $subject, \Magento\Customer\Api\Data\CustomerInterface $result ) { - $companyData = $this->request->getPost('company'); - + $companyData = $this->request->getPost('company', []); if (isset($companyData['status'])) { unset($companyData['status']); } if (is_array($companyData) && !empty($companyData)) { - $jobTitle = $this->request->getPost('job_title'); + $jobTitle = $companyData['job_title'] ?? null; $companyDataObject = $this->customerCompany->createCompany($result, $companyData, $jobTitle); $companyUrl = $this->urlBuilder->getUrl('company/index/edit', ['id' => $companyDataObject->getId()]); $this->companyEmailSender->sendAdminNotificationEmail( @@ -131,6 +142,7 @@ class AccountManagement $companyUrl ); } + return $result; } } diff --git a/vendor/magento/module-company/view/frontend/layout/company_account_create.xml b/vendor/magento/module-company/view/frontend/layout/company_account_create.xml index 2be577edf..51ec00699 100644 --- a/vendor/magento/module-company/view/frontend/layout/company_account_create.xml +++ b/vendor/magento/module-company/view/frontend/layout/company_account_create.xml @@ -6,6 +6,7 @@ */ --> + + + + customer_account_create + + + Magento\Customer\Model\Customer + + diff --git a/vendor/magento/module-company/view/frontend/templates/company/account/create.phtml b/vendor/magento/module-company/view/frontend/templates/company/account/create.phtml index a5abd19aa..290264dbf 100644 --- a/vendor/magento/module-company/view/frontend/templates/company/account/create.phtml +++ b/vendor/magento/module-company/view/frontend/templates/company/account/create.phtml @@ -5,7 +5,9 @@ */ // phpcs:disable Magento2.Templates.ThisInTemplate -?> +use Magento\Customer\Block\Widget\Gender; +use Magento\Customer\Block\Widget\Name; +use Magento\Directory\Helper\Data; ?> getCountriesList(); ?> - $country) : ?> + $country): ?>