/** * Change customer password action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { $this->_redirect('*/*/edit'); return; } if ($this->getRequest()->isPost()) { $customerId = $this->_getSession()->getCustomerId(); $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request); $this->_customerBuilder->populate($customer); $this->_customerBuilder->setId($customerId); $customer = $this->_customerBuilder->create(); if ($this->getRequest()->getParam('change_password')) { $currPass = $this->getRequest()->getPost('current_password'); $newPass = $this->getRequest()->getPost('password'); $confPass = $this->getRequest()->getPost('confirmation'); if (strlen($newPass)) { if ($newPass == $confPass) { try { $this->_customerAccountService->changePassword($customerId, $currPass, $newPass); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('A problem was encountered trying to change password.')); } } else { $this->messageManager->addError(__('Confirm your new password')); } } else { $this->messageManager->addError(__('New password field cannot be empty.')); } } try { $this->_customerDetailsBuilder->setCustomer($customer); $this->_customerAccountService->updateCustomer($this->_customerDetailsBuilder->create()); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (InputException $e) { $this->messageManager->addException($e, __('Invalid input')); } catch (\Exception $e) { $this->messageManager->addException($e, __('Cannot save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>'); } if ($this->messageManager->getMessages()->getCount() > 0) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); $this->_redirect('*/*/edit'); return; } $this->messageManager->addSuccess(__('The account information has been saved.')); $this->_redirect('customer/account'); return; } $this->_redirect('*/*/edit'); }
/** * Retrieve Customer instance * * @return \Magento\Customer\Service\V1\Data\Customer */ public function getCustomer() { if (is_null($this->_customer)) { $this->_customer = $this->_customerBuilder->create(); $params = $this->_coreData->urlDecode($this->_getRequest()->getParam('data')); $data = explode(',', $params); $cId = abs(intval($data[0])); if ($cId && $cId == $this->_customerSession->getCustomerId()) { $this->_customer = $this->_customerSession->getCustomerDataObject(); } } return $this->_customer; }
/** * @param string $formCode * @param RequestInterface $request * @return \Magento\Customer\Service\V1\Data\Customer */ public function extract($formCode, RequestInterface $request) { $customerForm = $this->formFactory->create('customer', $formCode); $allowedAttributes = $customerForm->getAllowedAttributes(); $isGroupIdEmpty = true; $customerData = array(); foreach ($allowedAttributes as $attribute) { // confirmation in request param is the repeated password, not a confirmation code. if ($attribute === 'confirmation') { continue; } $attributeCode = $attribute->getAttributeCode(); if ($attributeCode == 'group_id') { $isGroupIdEmpty = false; } $customerData[$attributeCode] = $request->getParam($attributeCode); } $this->customerBuilder->populateWithArray($customerData); $store = $this->storeManager->getStore(); if ($isGroupIdEmpty) { $this->customerBuilder->setGroupId($this->groupService->getDefaultGroup($store->getId())->getId()); } $this->customerBuilder->setWebsiteId($store->getWebsiteId()); $this->customerBuilder->setStoreId($store->getId()); return $this->customerBuilder->create(); }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @expectedException \Magento\Framework\Exception\StateException */ public function testUpdateCustomerDetailsByEmailWithException() { $customerId = 1; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => 'fname', 'id' => 1234567]); $this->_customerBuilder->populateWithArray($customerData); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create()); }
/** * Builds the entity. * * @return \Magento\Customer\Service\V1\Data\CustomerDetails */ public function create() { if (!isset($this->_data[CustomerDetails::KEY_CUSTOMER])) { $this->_data[CustomerDetails::KEY_CUSTOMER] = $this->_customerBuilder->create(); } if (!isset($this->_data[CustomerDetails::KEY_ADDRESSES])) { $this->_data[CustomerDetails::KEY_ADDRESSES] = null; } return parent::create(); }
/** * Sample customer data * * @return CustomerData */ private function getSampleCustomerEntity() { $email = '*****@*****.**'; $storeId = 1; $firstname = 'Tester'; $lastname = 'McTest'; $groupId = 1; $this->_customerBuilder->setStoreId($storeId)->setEmail($email)->setFirstname($firstname)->setLastname($lastname)->setGroupId($groupId); return $this->_customerBuilder->create(); }
/** * Get customer data from session or service. * * @param int|null $customerId possible customer ID from DB * @return Customer * @throws NoSuchEntityException */ protected function getCustomer($customerId) { $customerData = $this->_backendSession->getCustomerData(); if (!empty($customerData['account'])) { return $this->_customerBuilder->populateWithArray($customerData['account'])->create(); } elseif ($customerId) { return $this->_customerAccountService->getCustomer($customerId); } else { return $this->_customerBuilder->create(); } }
public function connectByFacebookId($facebookId, \stdClass $token, $customerId) { $customerDetailsObject = $this->_customerAccountService->getCustomerDetails($customerId); /* @var $customerDetailsObject \Magento\Customer\Service\V1\Data\CustomerDetails */ $customerDataObject = $customerDetailsObject->getCustomer(); /* @var $customerDetailsObject \Magento\Customer\Service\V1\Data\Customer */ // Merge old and new data $customerDetailsArray = array_merge($customerDataObject->__toArray(), array('custom_attributes' => array(array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_fid', \Magento\Framework\Service\Data\AttributeValue::VALUE => $facebookId), array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_ftoken', \Magento\Framework\Service\Data\AttributeValue::VALUE => serialize($token))))); // Pass result to customerBuilder $this->_customerBuilder->populateWithArray($customerDetailsArray); // Pass result to customerDetailsBuilder $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create()); // Update customer $this->_customerAccountService->updateCustomer($customerId, $this->_customerDetailsBuilder->create()); // Set customer as logged in $this->_customerSession->setCustomerDataAsLoggedIn($customerDataObject); }
/** * @expectedException \Magento\Customer\Exception * @expectedExceptionMessage exception message */ public function testCreateCustomerWithPasswordHashException() { $storeId = 5; $customerData = array('id' => self::ID, 'email' => self::EMAIL, 'firstname' => self::FIRSTNAME, 'lastname' => self::LASTNAME, 'store_id' => $storeId, 'website_id' => self::WEBSITE_ID); $this->_customerBuilder->populateWithArray($customerData); $customerEntity = $this->_customerBuilder->create(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerEntity)->create(); $this->_storeManagerMock->expects($this->once())->method('getStores')->will($this->returnValue(array())); $this->_converter = $this->getMock('Magento\\Customer\\Model\\Converter', array('getCustomerModel', 'createCustomerModel'), array(), '', false); $this->_converter->expects($this->once())->method('getCustomerModel')->will($this->returnValue($this->_customerModelMock)); $this->_converter->expects($this->once())->method('createCustomerModel')->will($this->throwException(new \Magento\Customer\Exception('exception message', 0))); $customerService = $this->_createService(); $customerService->createCustomerWithPasswordHash($customerDetails, '', ''); }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomerData(); $customerData = \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(CustomerMetadata::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, array()); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return array('error' => -1, 'message' => implode(', ', $customerErrors)); } if (!$isCustomerNew) { return true; } $this->_customerBuilder->populateWithArray($customerData); $customer = $this->_customerBuilder->create(); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->_customerAccountService->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $this->_customerBuilder->populate($customer); $this->_customerBuilder->setGroupId(CustomerGroupServiceInterface::NOT_LOGGED_IN_ID); $customer = $this->_customerBuilder->create(); } //validate customer $attributes = $customerForm->getAllowedAttributes(); $result = $this->_customerAccountService->validateCustomerData($customer, $attributes); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer), $quote); return true; }