public function customer_save_before($observer) { require_once Mage::getModuleDir('', 'Loginradius_Sociallogin') . DS . 'Helper' . DS . 'SDKClient.php'; global $apiClient_class; $apiClient_class = 'Loginradius_Sociallogin_Helper_SDKClient'; $activationBlockObj = Mage::getBlockSingleton('activation/activation'); $userApi = new LoginRadiusSDK\CustomerRegistration\UserAPI($activationBlockObj->apiKey(), $activationBlockObj->apiSecret(), array('output_format' => 'json')); if (Mage::app()->getFrontController()->getRequest()->getRouteName() == "checkout" && !Mage::getSingleton('customer/session')->isLoggedIn()) { $customer = $observer->getCustomer(); $address = $observer->getCustomerAddress(); $modified = $this->loginradius_get_customer_saved_data($customer, $address); if (isset($modified['EmailId']) && !empty($modified['EmailId'])) { $response = $userApi->create($modified); if (isset($response->description)) { Mage::throwException($response->description); } else { $_POST['lr_raas_resonse'] = $response; } return; } } $socialId = Mage::getSingleton("customer/session")->getloginRadiusId(); $postData = Mage::app()->getRequest()->getPost(); if (isset($postData['email'])) { $customer = Mage::getSingleton("customer/session")->getCustomer(); if (isset($postData['email']) && $postData['email'] == $customer->email) { //customer basic info object $modified = array(); //customer basic info $modified['firstname'] = isset($postData['firstname']) ? $postData['firstname'] : $customer->firstname; //firstname $modified['lastname'] = isset($postData['lastname']) ? $postData['lastname'] : $customer->lastname; //lastname $modified['email'] = isset($postData['email']) ? $postData['email'] : $customer->email; //email $modified['birthdate'] = isset($postData['dob']) ? date('m-d-Y', strtotime($postData['dob'])) : date('m-d-Y', strtotime($customer->dob)); //dob $modified['taxvat'] = isset($postData['taxvat']) ? $postData['taxvat'] : $customer->taxvat; //taxvat $modified['gender'] = isset($postData['gender']) ? $postData['gender'] : $customer->gender; //gender if ($modified['gender'] == '1') { $modified['gender'] = 'M'; } elseif ($modified['gender'] == '0') { $modified['gender'] = 'F'; } else { $modified['gender'] = 'M'; } $address = $observer->getCustomerAddress(); if (!isset($address) || empty($address)) { $address = new stdClass(); } $modified['Company'] = $this->checking_post_and_isset('company', $address); $modified['street'] = $this->checking_post_and_isset('street', $address); $modified['city'] = $this->checking_post_and_isset('city', $address); $modified['PostCode'] = $this->checking_post_and_isset('postcode', $address); $modified['phonenumber'] = $this->checking_post_and_isset('telephone', $address); //change and set password if (isset($postData['social_password']) && $postData['social_password'] == 1) { $accountAPI = new LoginRadiusSDK\CustomerRegistration\AccountAPI($activationBlockObj->apiKey(), $activationBlockObj->apiSecret(), array('output_format' => 'json')); $userId = Mage::getSingleton("customer/session")->getId(); $loginRadiusConn = Mage::getSingleton('core/resource')->getConnection('core_read'); $loginRadiusQuery = "select uid from " . Mage::getSingleton('core/resource')->getTableName('lr_sociallogin') . " where entity_id = '" . $userId . "' LIMIT 1"; $loginRadiusQueryHandle = $loginRadiusConn->query($loginRadiusQuery); $loginRadiusResult = $loginRadiusQueryHandle->fetch(); if (isset($loginRadiusResult["uid"]) && !empty($loginRadiusResult["uid"])) { //set password $raasSettings = Mage::getBlockSingleton('customerregistration/customerregistration'); if (isset($postData['emailid']) && isset($postData['confirmpassword']) && isset($postData['password'])) { if (empty($postData['emailid'])) { Mage::getSingleton('core/session')->addError('Please select Email Address'); /* not the best redirect but don`t know how to */ $this->_redirectUrl('customer/account/edit'); } if ($raasSettings->minPasswordLength() != 0 && $raasSettings->minPasswordLength() > strlen($postData['password'])) { Mage::getSingleton('core/session')->addError('The Password field must be at least ' . $raasSettings->minPasswordLength() . ' characters in length.'); } elseif ($raasSettings->maxPasswordLength() != 0 && strlen($postData['password']) > $raasSettings->maxPasswordLength()) { Mage::getSingleton('core/session')->addError('The Password field must not exceed ' . $raasSettings->maxPasswordLength() . ' characters in length.'); } elseif ($postData['password'] === $postData['confirmpassword']) { //check both password $data = array('accountid' => trim($loginRadiusResult["uid"]), 'emailid' => trim($postData['emailid']), 'password' => trim($postData['password'])); try { $response = $accountAPI->createUserRegistrationProfile($data); Mage::getSingleton('core/session')->addSuccess('Password updated successfully.'); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::getSingleton('core/session')->addError($e->getErrorResponse()->description); } } else { //password not match Mage::getSingleton('core/session')->addError('Password don\'t match'); } $this->_redirectUrl('customer/account/edit'); } elseif (isset($postData['newpassword']) && isset($postData['confirmnewpassword'])) { if ($raasSettings->minPasswordLength() != 0 && $raasSettings->minPasswordLength() > strlen($postData['newpassword'])) { Mage::getSingleton('core/session')->addError('The Password field must be at least ' . $raasSettings->minPasswordLength() . ' characters in length.'); } elseif ($raasSettings->maxPasswordLength() != 0 && strlen($postData['newpassword']) > $raasSettings->maxPasswordLength()) { Mage::getSingleton('core/session')->addError('The Password field must not exceed ' . $raasSettings->maxPasswordLength() . ' characters in length.'); } elseif ($postData['newpassword'] !== $postData['confirmnewpassword']) { //password not match Mage::getSingleton('core/session')->addError('Password and Confirm Password don\'t match'); } else { try { $response = $accountAPI->changeAccountPassword($loginRadiusResult["uid"], $postData['oldpassword'], $postData['newpassword']); Mage::getSingleton('core/session')->addSuccess('Password updated successfully.'); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::getSingleton('core/session')->addError($e->getErrorResponse()->description); } } $this->_redirectUrl('customer/account/edit'); } } else { Mage::getSingleton('core/session')->addError('An error occurred'); $this->_redirectUrl('customer/account/edit'); } } //update user at raas try { $response = $userApi->edit($socialId, $modified); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::getSingleton('core/session')->addError($e->getErrorResponse()->description); $this->_redirectUrl('customer/account/edit'); } } } }
public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomerDataObject(); $activationHelper = $this->_objectManager->get('LoginRadius\\Activation\\Model\\Helper\\Data'); $customerRegistrationHelper = $this->_objectManager->get("LoginRadius" . "\\" . $activationHelper->getAuthDirectory() . "\\Model\\Helper\\Data"); if ($customerRegistrationHelper->enableRaas() != '1') { return; } $customerSession = $this->_objectManager->get('Magento\\Customer\\Model\\Session'); $editUserData = array('firstname' => $customer->getFirstname(), 'lastname' => $customer->getLastname(), 'emailid' => $customer->getEmail(), 'gender' => $customer->getGender(), 'birthdate' => $customer->getDob()); $userAPI = new \LoginRadiusSDK\CustomerRegistration\UserAPI($activationHelper->siteApiKey(), $activationHelper->siteApiSecret(), array('authentication' => true, 'output_format' => 'json')); try { $userEditdata = $userAPI->edit($customerSession->getLoginRadiusId(), $editUserData); /* Edit profile in local db */ } catch (\LoginRadiusSDK\LoginRadiusException $e) { if ($customerRegistrationHelper->debug() == '1') { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } if (isset($_REQUEST['changepassword']) && $_REQUEST['changepassword'] == '1') { $accountAPI = new \LoginRadiusSDK\CustomerRegistration\AccountAPI($activationHelper->siteApiKey(), $activationHelper->siteApiSecret(), array('authentication' => true, 'output_format' => 'json')); $postData = $_REQUEST; if (isset($postData['emailid']) && !empty($postData['emailid'])) { if ($customerRegistrationHelper->minPassword() != 0 && $customerRegistrationHelper->minPassword() > strlen($postData['password'])) { $this->_messageManager->addError('The Password field must be at least ' . $customerRegistrationHelper->minPassword() . ' characters in length.'); } elseif ($customerRegistrationHelper->maxPassword() != 0 && strlen($postData['password']) > $customerRegistrationHelper->maxPassword()) { $this->_messageManager->addError('The Password field must not exceed ' . $customerRegistrationHelper->maxPassword() . ' characters in length.'); } elseif ($postData['password'] === $postData['confirmpassword']) { //check both password //setpassword $data = array('accountid' => $customerSession->getLoginRadiusUid(), 'password' => trim($postData['password']), 'emailid' => trim($postData['emailid'])); try { $setUserPassword = $accountAPI->createUserRegistrationProfile($data); } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } else { //password not match $this->_messageManager->addError('Password don\'t match'); } } else { if (isset($postData['oldpassword']) && isset($postData['newpassword']) && isset($postData['confirmnewpassword'])) { if ($customerRegistrationHelper->minPassword() != 0 && $customerRegistrationHelper->minPassword() > strlen($postData['newpassword'])) { $this->_messageManager->addError('The Password field must be at least ' . $customerRegistrationHelper->minPassword() . ' characters in length.'); } elseif ($customerRegistrationHelper->maxPassword() != 0 && strlen($postData['newpassword']) > $customerRegistrationHelper->maxPassword()) { $this->_messageManager->addError('The Password field must not exceed ' . $customerRegistrationHelper->maxPassword() . ' characters in length.'); } elseif ($postData['newpassword'] !== $postData['confirmnewpassword']) { //password not match $this->_messageManager->addError('Password and Confirm Password don\'t match'); } else { try { $changeUserPassword = $accountAPI->changeAccountPassword($customerSession->getLoginRadiusUid(), $postData['oldpassword'], $postData['newpassword']); } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } } } } }