public function admin_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')); $postData = Mage::app()->getRequest()->getPost(); $formattedDate = ''; if (isset($postData['account']['dob']) && !empty($postData['account']['dob'])) { $dateString = strtotime($postData['account']['dob']); $formattedDate = date('m-d-Y', $dateString); } /** * Creating user on RAAS */ if (!isset($postData['customer_id'])) { if ($postData['account']['website_id'] != 0) { $params = array('EmailId' => $postData['account']['email'], 'firstname' => $postData['account']['firstname'], 'lastname' => $postData['account']['lastname'], 'birthdate' => $formattedDate, 'password' => $postData['account']['password']); if (isset($postData['account']['gender'])) { if ($postData['account']['gender'] == '1') { $params['gender'] = 'M'; } elseif ($postData['account']['gender'] == '0') { $params['gender'] = 'F'; } } if ($postData['account']['password'] == 'auto') { $charsPass = "******"; $autoGenratedPassParams = array('EmailId' => $postData['account']['email'], 'firstname' => $postData['account']['firstname'], 'lastname' => $postData['account']['lastname'], 'birthdate' => $formattedDate, 'password' => substr(str_shuffle($charsPass), 0, 8)); try { $response = $userAPI->create($params); $_POST['lr_raas_resonse'] = $response; $forgotPassDomain = Mage::getBaseUrl() . 'customerregistration/index/verification/'; try { $rsetPasswordUrl = 'https://api.loginradius.com/raas/client/password/forgot?apikey=' . $activationBlockObj->apiKey() . '&emailid=' . $postData['account']['email'] . '&resetpasswordurl=' . $forgotPassDomain; } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } $result = LoginRadiusSDK\LoginRadius::apiClient($rsetPasswordUrl, FALSE, array('output_format' => 'json')); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } else { try { $response = $userAPI->create($params); $_POST['lr_raas_resonse'] = $response; } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } } } else { // Updating user profile $params = array('EmailId' => $postData['account']['email'], 'firstname' => $postData['account']['firstname'], 'lastname' => $postData['account']['lastname'], 'birthdate' => $formattedDate); if (isset($postData['account']['gender'])) { if ($postData['account']['gender'] == '1') { $params['gender'] = 'M'; } elseif ($postData['account']['gender'] == '0') { $params['gender'] = 'F'; } } $connection = Mage::getSingleton('core/resource'); $readConnection = $connection->getConnection('core_read'); $tableName = $connection->getTableName('lr_sociallogin'); $query = "select sociallogin_id, uid from {$tableName} where entity_id= '" . $postData['customer_id'] . "'"; $result = $readConnection->query($query)->fetch(); if (isset($result['sociallogin_id']) && !empty($result['sociallogin_id'])) { //Code for password changing if (isset($postData['account']['new_password']) && !empty($postData['account']['new_password'])) { $accountAPI = new LoginRadiusSDK\CustomerRegistration\AccountAPI($activationBlockObj->apiKey(), $activationBlockObj->apiSecret(), array('output_format' => 'json')); $getRaasProfile = $accountAPI->getAccounts($result['uid']); $checkProviderStatus = ''; foreach ($getRaasProfile as $key => $value) { if ($value->Provider == 'RAAS') { $checkProviderStatus = 'true'; } } if ($checkProviderStatus == 'true') { if ($postData['account']['new_password'] == 'auto') { $forgotPassDomain = Mage::getBaseUrl() . 'customerregistration/index/verification/'; try { $rsetPasswordUrl = 'https://api.loginradius.com/raas/client/password/forgot?apikey=' . $activationBlockObj->apiKey() . '&emailid=' . $postData['account']['email'] . '&resetpasswordurl=' . $forgotPassDomain; } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } $result = LoginRadiusSDK\LoginRadius::apiClient($rsetPasswordUrl, FALSE, array('output_format' => 'json')); } else { try { $accountAPI->setPassword(trim($result['uid']), trim($postData['account']['new_password'])); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } } else { if ($postData['account']['new_password'] == 'auto') { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_-=?"; try { $data = array('accountid' => trim($result['uid']), 'emailid' => trim($postData['account']['email']), 'password' => substr(str_shuffle($chars), 0, 8)); $res = $accountAPI->createUserRegistrationProfile($data); try { $forgotPassDomain = Mage::getBaseUrl() . 'customerregistration/index/verification/'; try { $rsetPasswordUrl = 'https://api.loginradius.com/raas/client/password/forgot?apikey=' . $activationBlockObj->apiKey() . '&emailid=' . $postData['account']['email'] . '&resetpasswordurl=' . $forgotPassDomain; } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } $result = LoginRadiusSDK\LoginRadius::apiClient($rsetPasswordUrl, FALSE, array('output_format' => 'json')); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } else { try { $data = array('accountid' => trim($result['uid']), 'emailid' => trim($postData['account']['email']), 'password' => trim($postData['account']['new_password'])); $res = $accountAPI->createUserRegistrationProfile($data); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); } } } } $connectionDb = Mage::getSingleton('core/resource'); $readConnectionDb = $connection->getConnection('core_read'); $tableNameCustomer = $connection->getTableName('customer_entity'); $queryCustomer = "select email from {$tableNameCustomer} where entity_id= '" . $postData['customer_id'] . "'"; $resultCustomer = $readConnectionDb->query($queryCustomer)->fetch(); if ($postData['account']['email'] != $resultCustomer["email"]) { $accountAPI = new LoginRadiusSDK\CustomerRegistration\AccountAPI($activationBlockObj->apiKey(), $activationBlockObj->apiSecret(), array('output_format' => 'json')); /* Adding New Email by Add/Remove Email Api */ $addEmailData = array('EmailId' => $postData['account']['email'], 'EmailType' => 'Primary'); try { $addEmail = $accountAPI->userAdditionalEmail($result['uid'], 'add', $addEmailData); } catch (LoginRadiusSDK\LoginRadiusException $e) { if (isset($e->getErrorResponse()->description)) { /* Getting Raas Profile By User Profile by UID Api */ $getRaasProfile = $accountAPI->getAccounts($result['uid']); $providerStatus = ''; foreach ($getRaasProfile as $key => $value) { if ($value->Provider == 'RAAS') { $providerStatus = 'true'; } } if ($providerStatus == 'true') { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; Mage::throwException($errorDescription); } else { Mage::throwException('Please Set Password then change the email id.'); } } return; } if (isset($addEmail->isPosted) && $addEmail->isPosted == 'true') { /* Removing Old Email by Add/Remove Email Api */ $removeEmailData = array('EmailId' => $resultCustomer["email"], 'EmailType' => 'Primary'); try { $removeEmail = $accountAPI->userAdditionalEmail($result['uid'], 'remove', $removeEmailData); } catch (LoginRadiusSDK\LoginRadiusException $e) { Mage::throwException($e->getErrorResponse()->description); return; } } } // if(empty($postData['account']['new_password']) || $postData['account']['new_password'] != 'auto'){ // // // try { // $customer = Mage::getSingleton("customer/session")->getCustomer(); // // // $userAPI->edit($result['sociallogin_id'], $params); // // } catch (LoginRadiusSDK\LoginRadiusException $e) { // // Mage::throwException($e->getErrorResponse()->description); // return; // } // } } } }
public function execute(\Magento\Framework\Event\Observer $observer) { $activationHelper = $this->_objectManager->get('LoginRadius\\Activation\\Model\\Helper\\Data'); $customerRegistrationHelper = $this->_objectManager->get("LoginRadius" . "\\" . $activationHelper->getAuthDirectory() . "\\Model\\Helper\\Data"); if ($customerRegistrationHelper->enableRaas() != '1') { return; } $events = $observer->getEvent(); $customerData = $events->getRequest()->getPostValue(); $customer = $observer->getEvent()->getCustomer(); $this->_date = $this->_objectManager->create('Magento\\Framework\\Stdlib\\DateTime'); $customer->setDob($this->_date->gmDate('m-d-Y', $this->_date->strToTime($customerData['customer']['dob']))); if (isset($_POST['customer']['entity_id'])) { $editUserData = array('firstname' => $customerData['customer']['firstname'], 'lastname' => $customerData['customer']['lastname'], 'birthdate' => $customer->getDob()); $resource = $this->_objectManager->get('Magento\\Framework\\App\\ResourceConnection'); $ruleTable = $resource->getTableName('lr_sociallogin'); $connection = $resource->getConnection(); $select = $connection->select()->from(['r' => $ruleTable])->where('entity_id=?', $customerData['customer']['entity_id']); $output = $connection->fetchAll($select); $userAPI = new \LoginRadiusSDK\CustomerRegistration\UserAPI($activationHelper->siteApiKey(), $activationHelper->siteApiSecret(), array('authentication' => true, 'output_format' => 'json')); $accountApi = new \LoginRadiusSDK\CustomerRegistration\AccountAPI($activationHelper->siteApiKey(), $activationHelper->siteApiSecret(), array('authentication' => true, 'output_format' => 'json')); /* Getting Raas Profile By User Profile by UID Api */ $getRaasProfileEmailByUid = $accountApi->getAccounts($output[0]['uid']); $checkProvider = ''; foreach ($getRaasProfileEmailByUid as $key => $value) { if ($getRaasProfileEmailByUid[$key]->Provider == 'RAAS') { $emailRaas = $getRaasProfileEmailByUid[$key]->Email[0]->Value; $checkProvider = 'true'; } } if ($checkProvider == 'true') { if ($customerData['customer']['email'] != $emailRaas) { /* Adding New Email by Add/Remove Email Api */ $addEmailData = array('EmailId' => $customerData['customer']['email'], 'EmailType' => 'Primary'); try { $addEmailRaas = $accountApi->userAdditionalEmail($output[0]['uid'], 'add', $addEmailData); } catch (\LoginRadiusSDK\LoginRadiusException $e) { /* Getting Raas Profile By User Profile by UID Api */ $getRaasProfileByUid = $accountApi->getAccounts($output[0]['uid']); $providerStatus = ''; foreach ($getRaasProfileByUid as $key => $value) { if ($getRaasProfileByUid[$key]->Provider == 'RAAS') { $providerStatus = 'true'; } } if ($providerStatus == 'true') { $getRaasProfile = $userAPI->getProfileByID($output[0]['sociallogin_id']); /* Updating email in local database */ $resource = $this->_objectManager->get('Magento\\Framework\\App\\ResourceConnection'); $updateRuleTable = $resource->getTableName('customer_entity'); $updateGridTable = $resource->getTableName('customer_grid_flat'); $updateConnection = $resource->getConnection(); $data = array("email" => $getRaasProfile->Email[0]->Value); $updateConnection->update($updateRuleTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); $updateConnection->update($updateGridTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } else { $getRaasProfile = $userAPI->getProfileByID($output[0]['sociallogin_id']); /* Updating email in local database */ $resource = $this->_objectManager->get('Magento\\Framework\\App\\ResourceConnection'); $updateRuleTable = $resource->getTableName('customer_entity'); $updateGridTable = $resource->getTableName('customer_grid_flat'); $updateConnection = $resource->getConnection(); $data = array("email" => $getRaasProfile->Email[0]->Value); $updateConnection->update($updateRuleTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); $updateConnection->update($updateGridTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError('Please Reset Set Password then change the email id.'); } } if (isset($addEmailRaas->isPosted) && $addEmailRaas->isPosted == 'true') { // $getRaasProfile = $userAPI->getProfileByID($output[0]['sociallogin_id']); $getRaasProfileUid = $accountApi->getAccounts($output[0]['uid']); foreach ($getRaasProfileUid as $key => $value) { if ($getRaasProfileUid[$key]->Provider == 'RAAS') { $raasEmail = $getRaasProfileUid[$key]->Email[0]->Value; } } /* Removing Old Email by Add/Remove Email Api */ $removeEmailData = array('EmailId' => $raasEmail, 'EmailType' => 'Primary'); try { $removeEmail = $accountApi->userAdditionalEmail($output[0]['uid'], 'remove', $removeEmailData); try { $getUserDataByEmail = $userAPI->getProfileByEmail($customerData['customer']['email']); try { $userEditdata = $userAPI->edit($getUserDataByEmail[0]->ID, $editUserData); } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } catch (\LoginRadiusSDK\LoginRadiusException $e) { } } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } } else { try { $getUserDataByEmail = $userAPI->getProfileByEmail($customerData['customer']['email']); try { $userEditdata = $userAPI->edit($getUserDataByEmail[0]->ID, $editUserData); } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } catch (\LoginRadiusSDK\LoginRadiusException $e) { } } } else { try { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_-=?"; $data = array('accountid' => $output[0]['uid'], 'emailid' => $customerData['customer']['email'], 'password' => substr(str_shuffle($chars), 0, 8)); $accountApi->createUserRegistrationProfile($data); /* Updating Basic profile */ try { $getUserDataByEmail = $userAPI->getProfileByEmail($customerData['customer']['email']); try { $userEditdata = $userAPI->edit($getUserDataByEmail[0]->ID, $editUserData); } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } catch (\LoginRadiusSDK\LoginRadiusException $e) { $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError($errorDescription); } } catch (\LoginRadiusSDK\LoginRadiusException $e) { $getRaasProfile = $userAPI->getProfileByID($output[0]['sociallogin_id']); /* Updating email in local database */ $resource = $this->_objectManager->get('Magento\\Framework\\App\\ResourceConnection'); $updateRuleTable = $resource->getTableName('customer_entity'); $updateGridTable = $resource->getTableName('customer_grid_flat'); $updateConnection = $resource->getConnection(); $data = array("email" => $getRaasProfile->Email[0]->Value); $updateConnection->update($updateRuleTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); $updateConnection->update($updateGridTable, $data, "entity_id ='" . $output[0]['entity_id'] . "'"); //$errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : ''; $this->_messageManager->addError('Not able to change the email of social user.'); } return; } } }