You are here

function salesforce_api_connect in Salesforce Suite 7

Same name and namespace in other branches
  1. 5.2 salesforce_api/salesforce_api.module \salesforce_api_connect()
  2. 6.2 salesforce_api/salesforce_api.module \salesforce_api_connect()
  3. 7.2 salesforce_api/salesforce_api.module \salesforce_api_connect()

Creates an object used for communicating with the Salesforce server and performs a login to verify the API credentials.

Parameters

$username: Username for Salesforce. An email address, most likely. If none passed, sitewide creds will be used

$password: Password to Salesforce account.

$token: Security token from Salesforce.

$reconnect: By default, subsequent calls to this function will return the same, already connected Salesforce object as preceding calls. Setting this variable to TRUE will cause a new connection to be established instead.

Return value

The DrupalSalesforce object used to communicate with the Salesforce server if successful or FALSE if a connection could not be established.

14 calls to salesforce_api_connect()
hook_sf_find_match in ./hooks.php
Retrieve matching object ids before creating a new object. This hook is designed to eliminate creation of duplicate objects if so desired. For example, an implementation of sf_user_sf_find_match might query Salesforce for Ids matching the…
salesforce_api_cache_build in salesforce_api/salesforce_api.module
Recreate the salesforce object cache
salesforce_api_demo in salesforce_api/salesforce_api.admin.inc
Demonstrates some of the API functionality through the Salesforce class and fieldmap functionality.
salesforce_api_describeGlobal in salesforce_api/salesforce_api.module
Wrapper for SOAP SforceBaseClient::describeGlobal
salesforce_api_describeSObjects in salesforce_api/salesforce_api.module
Wrapper for SOAP SforceBaseClient::describeSObjects Given an array of sf object type, return an associative, normalized array of SF object definitions, indexed on machine-readable names of SObjects

... See full list

File

salesforce_api/salesforce_api.module, line 186
Defines an API that enables modules to interact with the Salesforce server.

Code

function salesforce_api_connect($username = FALSE, $password = FALSE, $token = FALSE, $reconnect = FALSE) {
  static $sf = FALSE;
  module_load_include('inc', 'salesforce_api', 'salesforce.class');

  // Return the previously connected object.
  if ($sf && !$reconnect) {
    return $sf;
  }

  // Boolean, whether we are connecting with the default website user or not.
  $default_site_user = $username == variable_get('salesforce_api_username', FALSE);

  // Load up the sitewide API credentials if no others were provided:
  $username = $username ? $username : variable_get('salesforce_api_username', '');
  $password = $password ? $password : variable_get('salesforce_api_password', '');
  $token = $token ? $token : variable_get('salesforce_api_token', '');

  // Fail early if we didn't receive an API username, password, or token.
  if (empty($username) || empty($password) || empty($token)) {
    DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Connection to Salesforce
      failed because API credentials have not been set.', array(), WATCHDOG_ERROR);
    return FALSE;
  }

  // Create a new Salesforce object with the API credentials.
  $sf = new DrupalSalesforce($username, $password, $token);
  if (!is_object($sf)) {
    DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Connection to Salesforce
      failed. Failed to create DrupalSalesforce wrapper.', WATCHDOG_ERROR);
    return FALSE;
  }

  // Attempt a login.
  if ($sf
    ->login()) {

    // Mimick expired password state to debug.
    // $sf->login->passwordExpired = TRUE;
    if ($sf->login->passwordExpired) {
      if ($default_site_user) {
        salesforce_api_reset_expired_password($sf);
      }
      elseif (user_access('administer salesforce')) {
        drupal_set_message(t('Your Salesforce account password expired.  Please
         <a href="https://login.salesforce.com/">login to Salesforce.com</a> and
         change your password.'), 'error');
      }
      else {
        DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Connection to Salesforce
          due to expired password for @user.', array(
          '@user' => $username,
        ), WATCHDOG_ERROR);
      }
    }
  }
  else {
    DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Connection to Salesforce
      failed with response @resp.', array(
      '@resp' => $sf->client
        ->getLastResponse(),
    ), WATCHDOG_ERROR);

    // Or return FALSE to indicate the failure.
    $sf = FALSE;
  }
  return $sf;
}