/** * Method to check whether an user should be synchronized or not * * @param JUser $user * @return bool */ public function allowSynchronization($user = null, $action = null) { // Check if we have a valid object if ($user instanceof JUser) { // Don't synchronize backend-users if (MageBridgeUserHelper::isBackendUser($user)) { return false; } return true; } return false; }
/** * Handle the event that is generated after a customer is saved in Magento (JSON-RPC) * * @param array $arguments * * @return int */ public function mageCustomerSaveAfter($arguments = array()) { // Check if this call is valid if (!isset($arguments['customer'])) { return false; } if (!isset($arguments['customer']['email'])) { return false; } // Fetch the right variables $customer = $arguments['customer']; if (isset($customer['addresses'][0][0])) { $address = $customer['addresses'][0][0]; unset($customer['addresses']); } else { $address = array(); } // Try to load the user through the Joomla! ID stored in Magento if (isset($customer['joomla_id'])) { $user = JFactory::getUser(); $user->load($customer['joomla_id']); } // Try to load the user through its email-address if (!isset($user) || !isset($user->id) || !$user->id > 0) { $user = $this->getUser()->loadByEmail($customer['email']); } // Encrypt the user-password before continuing if (isset($customer['password'])) { $customer['password'] = MageBridge::decrypt($customer['password']); } // Start building the data-input for creating the Joomla! user if (!empty($customer['name']) && !empty($customer['email'])) { $data = array(); // Include the received email if (!empty($customer['email'])) { $data['email'] = $customer['email']; } // Include the real name $data['name'] = $this->getRealname($user, $customer); // Include the username $data['username'] = $this->getUsername($user, $customer); // Set the firstname and lastname $data['magebridgefirstlast'] = array('firstname' => $customer['firstname'], 'lastname' => $customer['lastname']); // Include the password if (!empty($customer['password'])) { $data['password'] = $customer['password']; $data['password2'] = $customer['password']; } // Activate this account, if it's also activated in Magento if (isset($customer['is_active'])) { if ($customer['is_active'] == 1) { $data['activation'] = ''; $data['block'] = 0; } else { $data['block'] = 1; } } // Add the usergroup ID to this user (based upon groups configured in #__magebridge_usergroups) $usergroups = is_array($user->groups) ? $user->groups : array(); $customer['usergroup'] = MageBridgeUserHelper::getJoomlaGroupIds($customer, $usergroups); //MageBridgeModelDebug::getInstance()->trace("Customer group", $customer['usergroup']); if (!empty($customer['usergroup'])) { $user->groups = $customer['usergroup']; } // If this user did not exist yet, create it if ($this->params->get('autocreate', 1) == 1 && ($user == false || $user->id == 0)) { $user = $this->getUser()->create($data, true); if (is_object($user) && $user->id > 0) { // Save the user through the Profile Connectors $profile = new MageBridgeConnectorProfile(); $profile->onSave($user, $customer, $address); // Return the new user-ID return $user->id; } else { return 3; } } // Bind the data to the current user if ($user->bind($data) == false) { return false; } // Save the user if ($user->save() == false) { return false; } // Save the user through the Profile Connectors $profile = new MageBridgeConnectorProfile(); $profile->onSave($user, $customer, $address); // Run the newsletter plugins if (isset($customer['is_subscribed'])) { JPluginHelper::importPlugin('magebridgenewsletter'); JFactory::getApplication()->triggerEvent('onNewsletterSubscribe', array($user, (bool) $customer['is_subscribed'])); } // Return the user ID for convenience return $user->id; } return false; }
/** * Method to get all CSV output * * @param null * @return null */ private function getOutput($users, $website_id, $group_id) { $output = '"group_id","website","firstname","lastname","email"' . "\n"; foreach ($users as $user) { $user = MageBridgeUserHelper::convert($user); $values = array(); $values[] = $group_id; $values[] = $website_id; $values[] = $user->firstname; $values[] = $user->lastname; $values[] = $user->email; foreach ($values as $index => $value) { $value = '"' . str_replace('"', '\\"', trim($value)) . '"'; $values[$index] = $value; } $output .= implode(',', $values) . "\n"; } return $output; }
public function synchronize($user) { MageBridgeModelDebug::getInstance()->notice("MageBridgeModelUser::synchronize() on user " . $user['email']); // Use the email if no username is set if (empty($user['username'])) { $user['username'] = $user['email']; } // Set the right ID $user['joomla_id'] = isset($user['id']) ? $user['id'] : 0; // Find some logic to divide the "name" into a "firstname" and "lastname" $user = MageBridgeUserHelper::convert($user); // Only set the password, when the password does not appear to be the encrypted version if (empty($user['password_clear'])) { if (isset($user['password']) && !preg_match('/([a-z0-9]{32}):([a-zA-Z0-9]+)/', $user['password'])) { $user['password_clear'] = $user['password']; } } // Try to detect the password in this POST if (empty($user['password_clear'])) { $fields = array('password_clear', 'password', 'passwd'); $jform = JRequest::getVar('jform', array(), 'post'); foreach ($fields as $field) { $password = JRequest::getString($field, '', 'post'); if (empty($password) && is_array($jform) && !empty($jform[$field])) { $password = $jform[$field]; } if (!empty($password)) { $user['password_clear'] = $password; break; } } } // Delete unusable fields unset($user['id']); unset($user['password']); unset($user['params']); unset($user['userType']); unset($user['sendEmail']); unset($user['option']); unset($user['task']); // Delete unusable empty fields foreach ($user as $name => $value) { if (empty($value)) { unset($user[$name]); } } // Encrypt the user-password for transfer through the MageBridge API if (isset($user['password_clear'])) { if (empty($user['password_clear'])) { unset($user['password_clear']); } else { $user['password_clear'] = MageBridgeEncryptionHelper::encrypt($user['password_clear']); } } // Add the Website ID to this user $user['website_id'] = MagebridgeModelConfig::load('website'); // Add the default customer-group ID to this user (in case we need to create a new user) $user['default_customer_group'] = MagebridgeModelConfig::load('customer_group'); // Add the customer-group ID to this user (based upon groups configured in #__magebridge_usergroups) $user['customer_group'] = MageBridgeUserHelper::getMagentoGroupId($user); // Make sure events are disabled on the Magento side $user['disable_events'] = 1; // Add the profile-connector data to this user $user = MageBridgeConnectorProfile::modifyUserFields($user); // Initalize the needed objects $bridge = MageBridgeModelBridge::getInstance(); $register = MageBridgeModelRegister::getInstance(); // Build the bridge and fetch the result $id = $register->add('api', 'magebridge_user.save', $user); $bridge->build(); $data = $register->getDataById($id); return $data; }