Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function _toHtml()
 {
     if (!$this->_customerHelper->isRegistrationAllowed() || $this->httpContext->getValue(\Magento\Customer\Helper\Data::CONTEXT_AUTH)) {
         return '';
     }
     return parent::_toHtml();
 }
Exemplo n.º 2
0
 /**
  * Authenticate user
  *
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param RequestInterface $request
  * @return void
  */
 public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
 {
     $loginUrl = $this->customerHelper->getLoginUrl();
     if (!$this->customerSession->authenticate($subject, $loginUrl)) {
         $subject->getActionFlag()->set('', $subject::FLAG_NO_DISPATCH, true);
     }
 }
Exemplo n.º 3
0
 /**
  * Handle customer VAT number if needed on collect_totals_before event of quote address
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function dispatch(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Sales\Model\Quote\Address $quoteAddress */
     $quoteAddress = $observer->getQuoteAddress();
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $quoteAddress->getQuote();
     $customerData = $quote->getCustomerData();
     $storeId = $customerData->getStoreId();
     if ($customerData->getCustomAttribute('disable_auto_group_change') && $customerData->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) {
         return;
     }
     $customerCountryCode = $quoteAddress->getCountryId();
     $customerVatNumber = $quoteAddress->getVatId();
     $groupId = null;
     if (empty($customerVatNumber) || false == $this->customerHelper->isCountryInEU($customerCountryCode)) {
         $groupId = $customerData->getId() ? $this->customerHelper->getDefaultCustomerGroupId($storeId) : \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID;
     } else {
         // Magento always has to emulate group even if customer uses default billing/shipping address
         $groupId = $this->customerHelper->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId);
     }
     if ($groupId) {
         $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
         $quote->setCustomerGroupId($groupId);
         $customerData = $this->customerBuilder->mergeDataObjectWithArray($customerData, array('group_id' => $groupId));
         $quote->setCustomerData($customerData);
     }
 }
Exemplo n.º 4
0
 /**
  * Customer register form page
  *
  * @return void
  */
 public function execute()
 {
     if ($this->_getSession()->isLoggedIn() || !$this->customerHelper->isRegistrationAllowed()) {
         $this->_redirect('*/*');
         return;
     }
     $this->_view->loadLayout();
     $this->_view->getLayout()->initMessages();
     $this->_view->renderLayout();
 }
Exemplo n.º 5
0
 public function testExtractCustomerDataWithFactory()
 {
     /** @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject */
     $mockFormFactory = $this->getMockBuilder('\\Magento\\Customer\\Model\\Metadata\\FormFactory')->disableOriginalConstructor()->setMethods(array('create'))->getMock();
     $objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
     $arguments = array('formFactory' => $mockFormFactory);
     $this->_dataHelper = $objectManagerHelper->getObject('\\Magento\\Customer\\Helper\\Data', $arguments);
     $mockFormFactory->expects($this->once())->method('create')->with(self::ENTITY, self::FORM_CODE, [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE)->will($this->returnValue($this->_mockMetadataForm));
     $this->assertEquals($this->_expected, $this->_dataHelper->extractCustomerData($this->_mockRequest, self::FORM_CODE, self::ENTITY, $this->_additionalAttributes, self::SCOPE));
 }
Exemplo n.º 6
0
 /**
  * Login post action
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     if ($this->_getSession()->isLoggedIn() || !$this->_formKeyValidator->validate($this->getRequest())) {
         $this->_redirect('*/*/');
         return;
     }
     if ($this->getRequest()->isPost()) {
         $login = $this->getRequest()->getPost('login');
         if (!empty($login['username']) && !empty($login['password'])) {
             try {
                 $customer = $this->_customerAccountService->authenticate($login['username'], $login['password']);
                 $this->_getSession()->setCustomerDataAsLoggedIn($customer);
                 $this->_getSession()->regenerateId();
             } catch (EmailNotConfirmedException $e) {
                 $value = $this->_customerHelperData->getEmailConfirmationUrl($login['username']);
                 $message = __('This account is not confirmed.' . ' <a href="%1">Click here</a> to resend confirmation email.', $value);
                 $this->messageManager->addError($message);
                 $this->_getSession()->setUsername($login['username']);
             } catch (AuthenticationException $e) {
                 $message = __('Invalid login or password.');
                 $this->messageManager->addError($message);
                 $this->_getSession()->setUsername($login['username']);
             } catch (\Exception $e) {
                 // PA DSS violation: this exception log can disclose customer password
                 // $this->_objectManager->get('Magento\Framework\Logger')->logException($e);
                 $this->messageManager->addError(__('There was an error validating the login and password.'));
             }
         } else {
             $this->messageManager->addError(__('Login and password are required.'));
         }
     }
     $this->_loginPostRedirect();
 }
Exemplo n.º 7
0
 /**
  * Address after save event handler
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function afterAddressSave($observer)
 {
     /** @var $customerAddress Address */
     $customerAddress = $observer->getCustomerAddress();
     $customer = $customerAddress->getCustomer();
     if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) {
         return;
     }
     try {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true);
         /** @var $customerHelper \Magento\Customer\Helper\Data */
         $customerHelper = $this->_customerData;
         if ($customerAddress->getVatId() == '' || !$this->_customerData->isCountryInEU($customerAddress->getCountry())) {
             $defaultGroupId = $customerHelper->getDefaultCustomerGroupId($customer->getStore());
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
                 $customer->setGroupId($defaultGroupId);
                 $customer->save();
             }
         } else {
             $result = $customerHelper->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId());
             $newGroupId = $customerHelper->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore());
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
                 $customer->setGroupId($newGroupId);
                 $customer->save();
             }
             $customerAddress->setVatValidationResult($result);
         }
     } catch (\Exception $e) {
         $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true);
     }
 }
Exemplo n.º 8
0
 /**
  * Retrieve back url
  *
  * @return string
  */
 public function getBackUrl()
 {
     $url = $this->getData('back_url');
     if (is_null($url)) {
         $url = $this->_customerHelper->getLoginUrl();
     }
     return $url;
 }
Exemplo n.º 9
0
 public function testDelete()
 {
     $dbAdapter = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface');
     $this->resource->expects($this->once())->method('getConnection')->will($this->returnValue($dbAdapter));
     $customer = $this->getMock('Magento\\Customer\\Model\\Customer', ['__wakeup', 'load', 'getId', 'getStoreId', 'setGroupId', 'save'], [], '', false);
     $customerId = 1;
     $customer->expects($this->once())->method('getId')->will($this->returnValue($customerId));
     $customer->expects($this->once())->method('load')->with($customerId)->will($this->returnSelf());
     $defaultCustomerGroup = 1;
     $this->customerHelper->expects($this->once())->method('getDefaultCustomerGroupId')->will($this->returnValue($defaultCustomerGroup));
     $customer->expects($this->once())->method('setGroupId')->with($defaultCustomerGroup);
     $customerCollection = $this->getMock('Magento\\Customer\\Model\\Resource\\Customer\\Collection', [], [], '', false);
     $customerCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf());
     $customerCollection->expects($this->once())->method('load')->will($this->returnValue([$customer]));
     $this->customersFactory->expects($this->once())->method('create')->will($this->returnValue($customerCollection));
     $this->groupResourceModel->delete($this->groupModel);
 }
Exemplo n.º 10
0
 /**
  * Retrieve name suffix drop-down options
  *
  * @return array|bool
  */
 public function getSuffixOptions()
 {
     $suffixOptions = $this->_customerHelper->getNameSuffixOptions();
     if ($this->getObject() && !empty($suffixOptions)) {
         $oldSuffix = $this->escapeHtml(trim($this->getObject()->getSuffix()));
         $suffixOptions[$oldSuffix] = $oldSuffix;
     }
     return $suffixOptions;
 }
Exemplo n.º 11
0
 /**
  * Create customer account action
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     if ($this->_getSession()->isLoggedIn() || !$this->isRegistrationAllowed()) {
         $this->_redirect('*/*/');
         return;
     }
     if (!$this->getRequest()->isPost()) {
         $url = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true));
         $this->getResponse()->setRedirect($this->_redirect->error($url));
         return;
     }
     $this->_session->regenerateId();
     try {
         $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
         $address = $this->_extractAddress();
         $addresses = is_null($address) ? array() : array($address);
         $password = $this->getRequest()->getParam('password');
         $redirectUrl = $this->_getSession()->getBeforeAuthUrl();
         $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses($addresses)->create();
         $customer = $this->_customerAccountService->createCustomer($customerDetails, $password, $redirectUrl);
         if ($this->getRequest()->getParam('is_subscribed', false)) {
             $this->_subscriberFactory->create()->subscribeCustomerById($customer->getId());
         }
         $this->_eventManager->dispatch('customer_register_success', array('account_controller' => $this, 'customer' => $customer));
         $confirmationStatus = $this->_customerAccountService->getConfirmationStatus($customer->getId());
         if ($confirmationStatus === CustomerAccountServiceInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
             $email = $this->_customerHelperData->getEmailConfirmationUrl($customer->getEmail());
             // @codingStandardsIgnoreStart
             $this->messageManager->addSuccess(__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%1">click here</a>.', $email));
             // @codingStandardsIgnoreEnd
             $url = $this->_createUrl()->getUrl('*/*/index', array('_secure' => true));
             $this->getResponse()->setRedirect($this->_redirect->success($url));
         } else {
             $this->_getSession()->setCustomerDataAsLoggedIn($customer);
             $url = $this->_welcomeCustomer($customer);
             $this->getResponse()->setRedirect($this->_redirect->success($url));
         }
         return;
     } catch (StateException $e) {
         $url = $this->_createUrl()->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 (\Exception $e) {
         $this->messageManager->addException($e, __('Cannot save the customer.'));
     }
     $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
     $defaultUrl = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true));
     $this->getResponse()->setRedirect($this->_redirect->error($defaultUrl));
 }
Exemplo n.º 12
0
 /**
  * Prepare Form and add elements to form
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _prepareForm()
 {
     $fieldset = $this->_form->addFieldset('main', array('no_container' => true));
     $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address');
     $attributes = $addressForm->getAttributes();
     $this->_addAttributesToForm($attributes, $fieldset);
     $prefixElement = $this->_form->getElement('prefix');
     if ($prefixElement) {
         $prefixOptions = $this->_customerHelper->getNamePrefixOptions($this->getStore());
         if (!empty($prefixOptions)) {
             $fieldset->removeField($prefixElement->getId());
             $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^');
             $prefixField->setValues($prefixOptions);
             if ($this->getAddressId()) {
                 $prefixField->addElementValues($this->getAddress()->getPrefix());
             }
         }
     }
     $suffixElement = $this->_form->getElement('suffix');
     if ($suffixElement) {
         $suffixOptions = $this->_customerHelper->getNameSuffixOptions($this->getStore());
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $this->_form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
             if ($this->getAddressId()) {
                 $suffixField->addElementValues($this->getAddress()->getSuffix());
             }
         }
     }
     $regionElement = $this->_form->getElement('region_id');
     if ($regionElement) {
         $regionElement->setNoDisplay(true);
     }
     $this->_form->setValues($this->getFormValues());
     if ($this->_form->getElement('country_id')->getValue()) {
         $countryId = $this->_form->getElement('country_id')->getValue();
         $this->_form->getElement('country_id')->setValue(null);
         foreach ($this->_form->getElement('country_id')->getValues() as $country) {
             if ($country['value'] == $countryId) {
                 $this->_form->getElement('country_id')->setValue($countryId);
             }
         }
     }
     if (is_null($this->_form->getElement('country_id')->getValue())) {
         $this->_form->getElement('country_id')->setValue($this->_coreData->getDefaultCountry($this->getStore()));
     }
     // Set custom renderer for VAT field if needed
     $vatIdElement = $this->_form->getElement('vat_id');
     if ($vatIdElement && $this->getDisplayVatValidationButton() !== false) {
         $vatIdElement->setRenderer($this->getLayout()->createBlock('Magento\\Customer\\Block\\Adminhtml\\Sales\\Order\\Address\\Form\\Renderer\\Vat')->setJsVariablePrefix($this->getJsVariablePrefix()));
     }
     return $this;
 }
Exemplo n.º 13
0
 /**
  * Involve new customer to system
  *
  * @return $this
  */
 protected function _involveNewCustomer()
 {
     $customer = $this->getQuote()->getCustomerData();
     $confirmationStatus = $this->_customerAccountService->getConfirmationStatus($customer->getId());
     if ($confirmationStatus === CustomerAccountServiceInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
         $url = $this->_customerData->getEmailConfirmationUrl($customer->getEmail());
         $this->messageManager->addSuccess(__('Account confirmation is required. Please, check your e-mail for confirmation link. To resend confirmation email please <a href="%1">click here</a>.', $url));
     } else {
         $this->getCustomerSession()->loginById($customer->getId());
     }
     return $this;
 }
Exemplo n.º 14
0
 /**
  * Method set default group id to the customers collection
  *
  * @param \Magento\Framework\Model\AbstractModel $group
  * @return $this
  */
 protected function _afterDelete(\Magento\Framework\Model\AbstractModel $group)
 {
     $customerCollection = $this->_createCustomersCollection()->addAttributeToFilter('group_id', $group->getId())->load();
     foreach ($customerCollection as $customer) {
         /** @var $customer \Magento\Customer\Model\Customer */
         $customer->load($customer->getId());
         $defaultGroupId = $this->_customerData->getDefaultCustomerGroupId($customer->getStoreId());
         $customer->setGroupId($defaultGroupId);
         $customer->save();
     }
     return parent::_afterDelete($group);
 }
Exemplo n.º 15
0
 /**
  * Validate VAT number
  *
  * @param \Magento\Sales\Model\Quote\Address $quoteAddress
  * @param \Magento\Store\Model\Store|int $store
  * @return \Magento\Framework\Object
  */
 public function validate(\Magento\Sales\Model\Quote\Address $quoteAddress, $store)
 {
     $customerCountryCode = $quoteAddress->getCountryId();
     $customerVatNumber = $quoteAddress->getVatId();
     $merchantCountryCode = $this->customerData->getMerchantCountryCode();
     $merchantVatNumber = $this->customerData->getMerchantVatNumber();
     $validationResult = null;
     if ($this->customerAddress->hasValidateOnEachTransaction($store) || $customerCountryCode != $quoteAddress->getValidatedCountryCode() || $customerVatNumber != $quoteAddress->getValidatedVatNumber()) {
         // Send request to gateway
         $validationResult = $this->customerData->checkVatNumber($customerCountryCode, $customerVatNumber, $merchantVatNumber !== '' ? $merchantCountryCode : '', $merchantVatNumber);
         // Store validation results in corresponding quote address
         $quoteAddress->setVatIsValid((int) $validationResult->getIsValid());
         $quoteAddress->setVatRequestId($validationResult->getRequestIdentifier());
         $quoteAddress->setVatRequestDate($validationResult->getRequestDate());
         $quoteAddress->setVatRequestSuccess($validationResult->getRequestSuccess());
         $quoteAddress->setValidatedVatNumber($customerVatNumber);
         $quoteAddress->setValidatedCountryCode($customerCountryCode);
         $quoteAddress->save();
     } else {
         // Restore validation results from corresponding quote address
         $validationResult = new \Magento\Framework\Object(array('is_valid' => (int) $quoteAddress->getVatIsValid(), 'request_identifier' => (string) $quoteAddress->getVatRequestId(), 'request_date' => (string) $quoteAddress->getVatRequestDate(), 'request_success' => (bool) $quoteAddress->getVatRequestSuccess()));
     }
     return $validationResult;
 }
Exemplo n.º 16
0
 /**
  * Authenticate controller action by login customer
  *
  * @param   \Magento\Framework\App\Action\Action $action
  * @param   bool|null $loginUrl
  * @return  bool
  */
 public function authenticate(\Magento\Framework\App\Action\Action $action, $loginUrl = null)
 {
     if ($this->isLoggedIn()) {
         return true;
     }
     $this->setBeforeAuthUrl($this->_createUrl()->getUrl('*/*/*', array('_current' => true)));
     if (isset($loginUrl)) {
         $action->getResponse()->setRedirect($loginUrl);
     } else {
         $arguments = $this->_customerData->getLoginUrlParams();
         if ($this->_session->getCookieShouldBeReceived() && $this->_createUrl()->getUseSession()) {
             $arguments += array('_query' => array($this->sidResolver->getSessionIdQueryParam($this->_session) => $this->_session->getSessionId()));
         }
         $action->getResponse()->setRedirect($this->_createUrl()->getUrl(\Magento\Customer\Helper\Data::ROUTE_ACCOUNT_LOGIN, $arguments));
     }
     return false;
 }
Exemplo n.º 17
0
 /**
  * @param $customerData
  * @param $isSingleStoreMode
  * @param $canModifyCustomer
  */
 private function _setupStoreMode($customerData, $isSingleStoreMode, $canModifyCustomer)
 {
     $backendSessionMock = $this->getMock('\\Magento\\Backend\\Model\\Session', ['getCustomerData'], [], '', false);
     $backendSessionMock->expects($this->any())->method('getCustomerData')->will($this->returnValue([]));
     $layoutMock = $this->getMock('\\Magento\\Framework\\View\\Layout\\Element\\Layout', ['createBlock'], [], '', false);
     $layoutMock->expects($this->at(0))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group')->will($this->returnValue($this->objectManagerHelper->getObject('\\Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group')));
     $layoutMock->expects($this->at(1))->method('createBlock')->with('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element')));
     if (empty($customerData['id'])) {
         $layoutMock->expects($this->at(2))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail')));
     }
     $urlBuilderMock = $this->getMock('\\Magento\\Backend\\Model\\Url', ['getBaseUrl'], [], '', false);
     $urlBuilderMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue('someUrl'));
     $storeManagerMock = $this->getMock('Magento\\Store\\Model\\StoreManager', [], [], '', false);
     $storeManagerMock->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue($isSingleStoreMode));
     $customerObject = $this->getMockBuilder('\\Magento\\Customer\\Service\\V1\\Data\\Customer')->setMethods(['__toArray', 'getId'])->disableOriginalConstructor()->getMock();
     $customerObject->expects($this->any())->method('__toArray')->will($this->returnValue($customerData));
     if (!empty($customerData['id'])) {
         $customerObject->expects($this->any())->method('getId')->will($this->returnValue($customerData['id']));
     }
     $fieldset = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Fieldset')->setMethods(['getForm', 'addField', 'removeField'])->disableOriginalConstructor()->getMock();
     $accountForm = $this->getMockBuilder('Magento\\Framework\\Data\\Form')->setMethods(['create', 'addFieldset', 'getElement', 'setValues'])->disableOriginalConstructor()->getMock();
     $accountForm->expects($this->any())->method('addFieldset')->with('base_fieldset')->will($this->returnValue($fieldset));
     $accountForm->expects($this->any())->method('setValues')->will($this->returnValue($accountForm));
     $fieldset->expects($this->any())->method('getForm')->will($this->returnValue($accountForm));
     $formElement = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Select')->setMethods(['setRenderer', 'addClass', 'setDisabled'])->disableOriginalConstructor()->getMock();
     $formElement->expects($this->any())->method('setRenderer')->will($this->returnValue(null));
     $formElement->expects($this->any())->method('addClass')->will($this->returnValue(null));
     $formElement->expects($this->any())->method('setDisabled')->will($this->returnValue(null));
     $accountForm->expects($this->any())->method('getElement')->withAnyParameters()->will($this->returnValue($formElement));
     $fieldset->expects($this->any())->method('addField')->will($this->returnValue($formElement));
     $customerForm = $this->getMock('\\Magento\\Customer\\Model\\Metadata\\Form', ['getAttributes'], [], '', false);
     $customerForm->expects($this->any())->method('getAttributes')->will($this->returnValue([]));
     $this->contextMock->expects($this->any())->method('getBackendSession')->will($this->returnValue($backendSessionMock));
     $this->contextMock->expects($this->any())->method('getLayout')->will($this->returnValue($layoutMock));
     $this->contextMock->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($urlBuilderMock));
     $this->contextMock->expects($this->any())->method('getStoreManager')->will($this->returnValue($storeManagerMock));
     $this->customerBuilderMock->expects($this->any())->method('populateWithArray')->will($this->returnValue($this->customerBuilderMock));
     $this->customerBuilderMock->expects($this->any())->method('create')->will($this->returnValue($customerObject));
     $this->customerHelperMock->expects($this->any())->method('getNamePrefixOptions')->will($this->returnValue(['Pref1', 'Pref2']));
     $this->customerHelperMock->expects($this->any())->method('getNameSuffixOptions')->will($this->returnValue(['Suf1', 'Suf2']));
     $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($accountForm));
     $this->customerFormFactoryMock->expects($this->any())->method('create')->with('customer', 'adminhtml_customer')->will($this->returnValue($customerForm));
     $this->customerAccountServiceInterfaceMock->expects($this->any())->method('canModify')->withAnyParameters()->will($this->returnValue($canModifyCustomer));
     $this->customerAccountServiceInterfaceMock->expects($this->any())->method('getConfirmationStatus')->withAnyParameters()->will($this->returnValue(CustomerAccountServiceInterface::ACCOUNT_CONFIRMED));
 }
Exemplo n.º 18
0
 /**
  * Check Captcha On User Login Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function checkUserLogin($observer)
 {
     $formId = 'user_login';
     $captchaModel = $this->_helper->getCaptcha($formId);
     $controller = $observer->getControllerAction();
     $loginParams = $controller->getRequest()->getPost('login');
     $login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
     if ($captchaModel->isRequired($login)) {
         $word = $this->_getCaptchaString($controller->getRequest(), $formId);
         if (!$captchaModel->isCorrect($word)) {
             $this->messageManager->addError(__('Incorrect CAPTCHA'));
             $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
             $this->_session->setUsername($login);
             $beforeUrl = $this->_session->getBeforeAuthUrl();
             $url = $beforeUrl ? $beforeUrl : $this->_customerData->getLoginUrl();
             $controller->getResponse()->setRedirect($url);
         }
     }
     $captchaModel->logAttempt($login);
     return $this;
 }
Exemplo n.º 19
0
 /**
  * Check if type of Prefix and Suffix elements should be changed from text to select and change it if need.
  *
  * @param string $elementName
  * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
  * @return null
  */
 protected function _checkElementType($elementName, $fieldset)
 {
     $possibleElements = ['prefix', 'suffix'];
     if (!in_array($elementName, $possibleElements)) {
         return;
     }
     $element = $fieldset->getForm()->getElement($elementName);
     if ($element) {
         if ($elementName == 'prefix') {
             $options = $this->_customerHelper->getNamePrefixOptions($this->_getCustomerDataObject()->getStoreId());
             $prevSibling = $fieldset->getForm()->getElement('group_id')->getId();
         }
         if ($elementName == 'suffix') {
             $options = $this->_customerHelper->getNameSuffixOptions($this->_getCustomerDataObject()->getStoreId());
             $prevSibling = $fieldset->getForm()->getElement('lastname')->getId();
         }
         if (!empty($options)) {
             $fieldset->removeField($element->getId());
             $elementField = $fieldset->addField($element->getId(), 'select', $element->getData(), $prevSibling);
             $elementField->setValues($options);
         }
     }
 }
 /**
  * Define and return target URL after logging in
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _loginPostRedirect()
 {
     $lastCustomerId = $this->_getSession()->getLastCustomerId();
     if (isset($lastCustomerId) && $this->_getSession()->isLoggedIn() && $lastCustomerId != $this->_getSession()->getId()) {
         $this->_getSession()->unsBeforeAuthUrl()->setLastCustomerId($this->_getSession()->getId());
     }
     if (!$this->_getSession()->getBeforeAuthUrl() || $this->_getSession()->getBeforeAuthUrl() == $this->_storeManager->getStore()->getBaseUrl()) {
         // Set default URL to redirect customer to
         $this->_getSession()->setBeforeAuthUrl($this->_customerHelperData->getAccountUrl());
         // Redirect customer to the last page visited after logging in
         if ($this->_getSession()->isLoggedIn()) {
             if (!$this->_scopeConfig->isSetFlag(\Magento\Customer\Helper\Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
                 $referer = $this->getRequest()->getParam(\Magento\Customer\Helper\Data::REFERER_QUERY_PARAM_NAME);
                 if ($referer) {
                     $referer = $this->coreHelperData->urlDecode($referer);
                     if ($this->_url->isOwnOriginUrl()) {
                         $this->_getSession()->setBeforeAuthUrl($referer);
                     }
                 }
             } elseif ($this->_getSession()->getAfterAuthUrl()) {
                 $this->_getSession()->setBeforeAuthUrl($this->_getSession()->getAfterAuthUrl(true));
             }
         } else {
             $this->_getSession()->setBeforeAuthUrl($this->_customerHelperData->getLoginUrl());
         }
     } elseif ($this->_getSession()->getBeforeAuthUrl() == $this->_customerHelperData->getLogoutUrl()) {
         $this->_getSession()->setBeforeAuthUrl($this->_customerHelperData->getDashboardUrl());
     } else {
         if (!$this->_getSession()->getAfterAuthUrl()) {
             $this->_getSession()->setAfterAuthUrl($this->_getSession()->getBeforeAuthUrl());
         }
         if ($this->_getSession()->isLoggedIn()) {
             $this->_getSession()->setBeforeAuthUrl($this->_getSession()->getAfterAuthUrl(true));
         }
     }
     return $this->_getSession()->getBeforeAuthUrl(true);
 }
Exemplo n.º 21
0
 /**
  * @return string
  */
 public function getHref()
 {
     return $this->_customerHelper->getAccountUrl();
 }
Exemplo n.º 22
0
 /**
  * Initialize form object
  *
  * @return $this
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function initForm()
 {
     $customerData = $this->_backendSession->getCustomerData();
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $fieldset = $form->addFieldset('address_fieldset', array('legend' => __("Edit Customer's Address")));
     $account = $customerData['account'];
     $this->_addressBuilder->populateWithArray(array());
     if (!empty($account) && isset($account['store_id'])) {
         $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry($this->_storeManager->getStore($account['store_id'])));
     } else {
         $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry());
     }
     $address = $this->_addressBuilder->create();
     $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address));
     $attributes = $addressForm->getAttributes();
     if (isset($attributes['street'])) {
         if ($attributes['street']->getMultilineCount() <= 0) {
             $attributes['street'] = $this->_attributeMetadataBuilder->populate($attributes['street'])->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT)->create();
         }
     }
     foreach ($attributes as $key => $attribute) {
         $attributes[$key] = $this->_attributeMetadataBuilder->populate($attribute)->setFrontendLabel(__($attribute->getFrontendLabel()))->setVisible(false)->create();
     }
     $this->_setFieldset($attributes, $fieldset);
     $regionElement = $form->getElement('region');
     if ($regionElement) {
         $regionElement->setRenderer($this->_regionFactory->create());
     }
     $regionElement = $form->getElement('region_id');
     if ($regionElement) {
         $regionElement->setNoDisplay(true);
     }
     $country = $form->getElement('country_id');
     if ($country) {
         $country->addClass('countries');
     }
     if ($this->isReadonly()) {
         foreach ($this->_metadataService->getAllAddressAttributeMetadata() as $attribute) {
             $element = $form->getElement($attribute->getAttributeCode());
             if ($element) {
                 $element->setReadonly(true, true);
             }
         }
     }
     $customerStoreId = null;
     if (!empty($account) && isset($account['id']) && isset($account['website_id'])) {
         $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId();
     }
     $prefixElement = $form->getElement('prefix');
     if ($prefixElement) {
         $prefixOptions = $this->_customerHelper->getNamePrefixOptions($customerStoreId);
         if (!empty($prefixOptions)) {
             $fieldset->removeField($prefixElement->getId());
             $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^');
             $prefixField->setValues($prefixOptions);
         }
     }
     $suffixElement = $form->getElement('suffix');
     if ($suffixElement) {
         $suffixOptions = $this->_customerHelper->getNameSuffixOptions($customerStoreId);
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
         }
     }
     $this->assign('customer', $this->_customerBuilder->populateWithArray($account)->create());
     $addressCollection = array();
     foreach ($customerData['address'] as $key => $addressData) {
         $addressCollection[$key] = $this->_addressBuilder->populateWithArray($addressData)->create();
     }
     $this->assign('addressCollection', $addressCollection);
     $form->setValues(AddressConverter::toFlatArray($address));
     $this->setForm($form);
     return $this;
 }
Exemplo n.º 23
0
 /**
  * Redirect to login page
  *
  * @return void
  */
 public function redirectLogin()
 {
     $this->_actionFlag->set('', 'no-dispatch', true);
     $this->_customerSession->setBeforeAuthUrl($this->_redirect->getRefererUrl());
     $this->getResponse()->setRedirect($this->_urlHelper->addRequestParam($this->_customerHelper->getLoginUrl(), array('context' => 'checkout')));
 }
Exemplo n.º 24
0
 /**
  * Retrieve password forgotten url
  *
  * @return string
  */
 public function getForgotPasswordUrl()
 {
     return $this->_customerHelper->getForgotPasswordUrl();
 }
Exemplo n.º 25
0
 /**
  * Check if current reset password link token is expired
  *
  * @return boolean
  */
 public function isResetPasswordLinkTokenExpired()
 {
     $linkToken = $this->getRpToken();
     $linkTokenCreatedAt = $this->getRpTokenCreatedAt();
     if (empty($linkToken) || empty($linkTokenCreatedAt)) {
         return true;
     }
     $expirationPeriod = $this->_customerData->getResetPasswordLinkExpirationPeriod();
     $currentTimestamp = $this->dateTime->toTimestamp($this->dateTime->now());
     $tokenTimestamp = $this->dateTime->toTimestamp($linkTokenCreatedAt);
     if ($tokenTimestamp > $currentTimestamp) {
         return true;
     }
     $dayDifference = floor(($currentTimestamp - $tokenTimestamp) / (24 * 60 * 60));
     if ($dayDifference >= $expirationPeriod) {
         return true;
     }
     return false;
 }
Exemplo n.º 26
0
 /**
  * @return string
  */
 public function getHref()
 {
     return $this->isLoggedIn() ? $this->_customerHelper->getLogoutUrl() : $this->_customerHelper->getLoginUrl();
 }
Exemplo n.º 27
0
 public function testGetHrefLoggedOut()
 {
     $this->httpContext->expects($this->once())->method('getValue')->will($this->returnValue(false));
     $this->_helper->expects($this->once())->method('getLoginUrl')->will($this->returnValue('login url'));
     $this->assertEquals('login url', $this->_block->getHref());
 }