123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Account;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Customer\Model\Account\Redirect as AccountRedirect;
- use Magento\Customer\Api\Data\AddressInterface;
- use Magento\Framework\Api\DataObjectHelper;
- use Magento\Framework\App\Action\Context;
- use Magento\Customer\Model\Session;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\CsrfAwareActionInterface;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\App\Request\InvalidRequestException;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\Controller\Result\Redirect;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Phrase;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Customer\Api\AccountManagementInterface;
- use Magento\Customer\Helper\Address;
- use Magento\Framework\UrlFactory;
- use Magento\Customer\Model\Metadata\FormFactory;
- use Magento\Newsletter\Model\SubscriberFactory;
- use Magento\Customer\Api\Data\RegionInterfaceFactory;
- use Magento\Customer\Api\Data\AddressInterfaceFactory;
- use Magento\Customer\Api\Data\CustomerInterfaceFactory;
- use Magento\Customer\Model\Url as CustomerUrl;
- use Magento\Customer\Model\Registration;
- use Magento\Framework\Escaper;
- use Magento\Customer\Model\CustomerExtractor;
- use Magento\Framework\Exception\StateException;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Data\Form\FormKey\Validator;
- use Magento\Customer\Controller\AbstractAccount;
- /**
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CreatePost extends AbstractAccount implements CsrfAwareActionInterface, HttpPostActionInterface
- {
- /**
- * @var \Magento\Customer\Api\AccountManagementInterface
- */
- protected $accountManagement;
- /**
- * @var \Magento\Customer\Helper\Address
- */
- protected $addressHelper;
- /**
- * @var \Magento\Customer\Model\Metadata\FormFactory
- */
- protected $formFactory;
- /**
- * @var \Magento\Newsletter\Model\SubscriberFactory
- */
- protected $subscriberFactory;
- /**
- * @var \Magento\Customer\Api\Data\RegionInterfaceFactory
- */
- protected $regionDataFactory;
- /**
- * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
- */
- protected $addressDataFactory;
- /**
- * @var \Magento\Customer\Model\Registration
- */
- protected $registration;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
- */
- protected $customerDataFactory;
- /**
- * @var \Magento\Customer\Model\Url
- */
- protected $customerUrl;
- /**
- * @var \Magento\Framework\Escaper
- */
- protected $escaper;
- /**
- * @var \Magento\Customer\Model\CustomerExtractor
- */
- protected $customerExtractor;
- /**
- * @var \Magento\Framework\UrlInterface
- */
- protected $urlModel;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper
- */
- protected $dataObjectHelper;
- /**
- * @var Session
- */
- protected $session;
- /**
- * @var AccountRedirect
- */
- private $accountRedirect;
- /**
- * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
- */
- private $cookieMetadataFactory;
- /**
- * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
- */
- private $cookieMetadataManager;
- /**
- * @var Validator
- */
- private $formKeyValidator;
- /**
- * @param Context $context
- * @param Session $customerSession
- * @param ScopeConfigInterface $scopeConfig
- * @param StoreManagerInterface $storeManager
- * @param AccountManagementInterface $accountManagement
- * @param Address $addressHelper
- * @param UrlFactory $urlFactory
- * @param FormFactory $formFactory
- * @param SubscriberFactory $subscriberFactory
- * @param RegionInterfaceFactory $regionDataFactory
- * @param AddressInterfaceFactory $addressDataFactory
- * @param CustomerInterfaceFactory $customerDataFactory
- * @param CustomerUrl $customerUrl
- * @param Registration $registration
- * @param Escaper $escaper
- * @param CustomerExtractor $customerExtractor
- * @param DataObjectHelper $dataObjectHelper
- * @param AccountRedirect $accountRedirect
- * @param Validator $formKeyValidator
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- Context $context,
- Session $customerSession,
- ScopeConfigInterface $scopeConfig,
- StoreManagerInterface $storeManager,
- AccountManagementInterface $accountManagement,
- Address $addressHelper,
- UrlFactory $urlFactory,
- FormFactory $formFactory,
- SubscriberFactory $subscriberFactory,
- RegionInterfaceFactory $regionDataFactory,
- AddressInterfaceFactory $addressDataFactory,
- CustomerInterfaceFactory $customerDataFactory,
- CustomerUrl $customerUrl,
- Registration $registration,
- Escaper $escaper,
- CustomerExtractor $customerExtractor,
- DataObjectHelper $dataObjectHelper,
- AccountRedirect $accountRedirect,
- Validator $formKeyValidator = null
- ) {
- $this->session = $customerSession;
- $this->scopeConfig = $scopeConfig;
- $this->storeManager = $storeManager;
- $this->accountManagement = $accountManagement;
- $this->addressHelper = $addressHelper;
- $this->formFactory = $formFactory;
- $this->subscriberFactory = $subscriberFactory;
- $this->regionDataFactory = $regionDataFactory;
- $this->addressDataFactory = $addressDataFactory;
- $this->customerDataFactory = $customerDataFactory;
- $this->customerUrl = $customerUrl;
- $this->registration = $registration;
- $this->escaper = $escaper;
- $this->customerExtractor = $customerExtractor;
- $this->urlModel = $urlFactory->create();
- $this->dataObjectHelper = $dataObjectHelper;
- $this->accountRedirect = $accountRedirect;
- $this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
- parent::__construct($context);
- }
- /**
- * Retrieve cookie manager
- *
- * @deprecated 100.1.0
- * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
- */
- private function getCookieManager()
- {
- if (!$this->cookieMetadataManager) {
- $this->cookieMetadataManager = ObjectManager::getInstance()->get(
- \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
- );
- }
- return $this->cookieMetadataManager;
- }
- /**
- * Retrieve cookie metadata factory
- *
- * @deprecated 100.1.0
- * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
- */
- private function getCookieMetadataFactory()
- {
- if (!$this->cookieMetadataFactory) {
- $this->cookieMetadataFactory = ObjectManager::getInstance()->get(
- \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
- );
- }
- return $this->cookieMetadataFactory;
- }
- /**
- * Add address to customer during create account
- *
- * @return AddressInterface|null
- */
- protected function extractAddress()
- {
- if (!$this->getRequest()->getPost('create_address')) {
- return null;
- }
- $addressForm = $this->formFactory->create('customer_address', 'customer_register_address');
- $allowedAttributes = $addressForm->getAllowedAttributes();
- $addressData = [];
- $regionDataObject = $this->regionDataFactory->create();
- foreach ($allowedAttributes as $attribute) {
- $attributeCode = $attribute->getAttributeCode();
- $value = $this->getRequest()->getParam($attributeCode);
- if ($value === null) {
- continue;
- }
- switch ($attributeCode) {
- case 'region_id':
- $regionDataObject->setRegionId($value);
- break;
- case 'region':
- $regionDataObject->setRegion($value);
- break;
- default:
- $addressData[$attributeCode] = $value;
- }
- }
- $addressDataObject = $this->addressDataFactory->create();
- $this->dataObjectHelper->populateWithArray(
- $addressDataObject,
- $addressData,
- \Magento\Customer\Api\Data\AddressInterface::class
- );
- $addressDataObject->setRegion($regionDataObject);
- $addressDataObject->setIsDefaultBilling(
- $this->getRequest()->getParam('default_billing', false)
- )->setIsDefaultShipping(
- $this->getRequest()->getParam('default_shipping', false)
- );
- return $addressDataObject;
- }
- /**
- * @inheritDoc
- */
- public function createCsrfValidationException(
- RequestInterface $request
- ): ?InvalidRequestException {
- /** @var Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
- $resultRedirect->setUrl($this->_redirect->error($url));
- return new InvalidRequestException(
- $resultRedirect,
- [new Phrase('Invalid Form Key. Please refresh the page.')]
- );
- }
- /**
- * @inheritDoc
- */
- public function validateForCsrf(RequestInterface $request): ?bool
- {
- return null;
- }
- /**
- * Create customer account action
- *
- * @return void
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function execute()
- {
- /** @var Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
- $resultRedirect->setPath('*/*/');
- return $resultRedirect;
- }
- if (!$this->getRequest()->isPost()
- || !$this->formKeyValidator->validate($this->getRequest())
- ) {
- $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
- return $this->resultRedirectFactory->create()
- ->setUrl($this->_redirect->error($url));
- }
- $this->session->regenerateId();
- try {
- $address = $this->extractAddress();
- $addresses = $address === null ? [] : [$address];
- $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
- $customer->setAddresses($addresses);
- $password = $this->getRequest()->getParam('password');
- $confirmation = $this->getRequest()->getParam('password_confirmation');
- $redirectUrl = $this->session->getBeforeAuthUrl();
- $this->checkPasswordConfirmation($password, $confirmation);
- $customer = $this->accountManagement
- ->createAccount($customer, $password, $redirectUrl);
- if ($this->getRequest()->getParam('is_subscribed', false)) {
- $this->subscriberFactory->create()->subscribeCustomerById($customer->getId());
- }
- $this->_eventManager->dispatch(
- 'customer_register_success',
- ['account_controller' => $this, 'customer' => $customer]
- );
- $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
- if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
- $email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail());
- // @codingStandardsIgnoreStart
- $this->messageManager->addSuccess(
- __(
- 'You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.',
- $email
- )
- );
- // @codingStandardsIgnoreEnd
- $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
- $resultRedirect->setUrl($this->_redirect->success($url));
- } else {
- $this->session->setCustomerDataAsLoggedIn($customer);
- $this->messageManager->addSuccess($this->getSuccessMessage());
- $requestedRedirect = $this->accountRedirect->getRedirectCookie();
- if (!$this->scopeConfig->getValue('customer/startup/redirect_dashboard') && $requestedRedirect) {
- $resultRedirect->setUrl($this->_redirect->success($requestedRedirect));
- $this->accountRedirect->clearRedirectCookie();
- return $resultRedirect;
- }
- $resultRedirect = $this->accountRedirect->getRedirect();
- }
- if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
- $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
- $metadata->setPath('/');
- $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
- }
- return $resultRedirect;
- } catch (StateException $e) {
- $url = $this->urlModel->getUrl('customer/account/forgotpassword');
- // @codingStandardsIgnoreStart
- $message = __(
- 'There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.',
- $url
- );
- // @codingStandardsIgnoreEnd
- $this->messageManager->addError($message);
- } catch (InputException $e) {
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- foreach ($e->getErrors() as $error) {
- $this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
- }
- } catch (LocalizedException $e) {
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- } catch (\Exception $e) {
- $this->messageManager->addException($e, __('We can\'t save the customer.'));
- }
- $this->session->setCustomerFormData($this->getRequest()->getPostValue());
- $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
- return $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
- }
- /**
- * Make sure that password and password confirmation matched
- *
- * @param string $password
- * @param string $confirmation
- * @return void
- * @throws InputException
- */
- protected function checkPasswordConfirmation($password, $confirmation)
- {
- if ($password != $confirmation) {
- throw new InputException(__('Please make sure your passwords match.'));
- }
- }
- /**
- * Retrieve success message
- *
- * @return string
- */
- protected function getSuccessMessage()
- {
- if ($this->addressHelper->isVatValidationEnabled()) {
- if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
- // @codingStandardsIgnoreStart
- $message = __(
- 'If you are a registered VAT customer, please <a href="%1">click here</a> to enter your shipping address for proper VAT calculation.',
- $this->urlModel->getUrl('customer/address/edit')
- );
- // @codingStandardsIgnoreEnd
- } else {
- // @codingStandardsIgnoreStart
- $message = __(
- 'If you are a registered VAT customer, please <a href="%1">click here</a> to enter your billing address for proper VAT calculation.',
- $this->urlModel->getUrl('customer/address/edit')
- );
- // @codingStandardsIgnoreEnd
- }
- } else {
- $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName());
- }
- return $message;
- }
- }
|