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();
     $customer = $events->getCustomerDataObject();
     $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()_-=?";
     $birthDate = date("m-d-Y", strtotime($customer->getDob()));
     $newUserData = array('emailid' => $customer->getEmail(), 'firstname' => $customer->getFirstname(), 'lastname' => $customer->getLastname(), 'password' => substr(str_shuffle($chars), 0, 8), 'gender' => $this->getGenderValue($customer->getGender()), 'birthdate' => $birthDate);
     $userAPI = new \LoginRadiusSDK\CustomerRegistration\UserAPI($activationHelper->siteApiKey(), $activationHelper->siteApiSecret(), array('authentication' => true, 'output_format' => 'json'));
     $homeDomain = $this->_objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getStore()->getBaseUrl();
     if (!isset($_POST['customer']['entity_id'])) {
         try {
             $userCreatedata = $userAPI->create($newUserData);
             try {
                 $rsetPasswordUrl = 'https://api.loginradius.com/raas/client/password/forgot?apikey=' . $activationHelper->siteApiKey() . '&emailid=' . $customer->getEmail() . '&resetpasswordurl=' . $homeDomain . 'customer/account/login/';
                 $result = \LoginRadiusSDK\LoginRadius::apiClient($rsetPasswordUrl, FALSE, array('output_format' => 'json'));
             } catch (\LoginRadiusSDK\LoginRadiusException $e) {
                 $errorDescription = isset($e->getErrorResponse()->description) ? $e->getErrorResponse()->description : '';
                 $this->_messageManager->addError($errorDescription);
             }
             try {
                 $this->socialLinkingData($customer->getId(), $userCreatedata);
             } catch (\Exception $e) {
             }
         } catch (\LoginRadiusSDK\LoginRadiusException $e) {
         }
         return;
     }
 }
 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;
             //                }
             //                }
         }
     }
 }