You are here

function salesforce_update_8005 in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 salesforce.install \salesforce_update_8005()
  2. 5.0.x salesforce.install \salesforce_update_8005()

Convert legacy oauth credentials to new auth plugin config.

File

./salesforce.install, line 274
Salesforce install file.

Code

function salesforce_update_8005() {
  $change_list = \Drupal::entityDefinitionUpdateManager()
    ->getChangeSummary();
  if (!empty($change_list['salesforce_auth'])) {
    if (!empty($change_list['salesforce_auth'])) {
      $entityType = \Drupal::entityTypeManager()
        ->getDefinition('salesforce_auth');
      \Drupal::entityDefinitionUpdateManager()
        ->installEntityType($entityType);
    }
  }
  $message = '';

  // If auth plugin providers have not been created already, convert existing.
  if (SalesforceAuthConfig::load('oauth_default')) {

    // If an auth config with our name already exists, we are done here.
    $message = 'Existing "oauth_default" provider config detected. Refused to set legacy credentials.';
  }
  else {

    /** @var \Drupal\salesforce\Entity\SalesforceAuthConfig $oauth */
    $oauth = NULL;
    $config = \Drupal::configFactory()
      ->getEditable('salesforce.settings');

    // Config to new plugin config system.
    $values = [
      'id' => 'oauth_default',
      'label' => 'OAuth Default',
      'provider' => 'oauth',
    ];
    $oauth = SalesforceAuthConfig::create($values);
    $settings = [
      'consumer_key' => $config
        ->get('consumer_key'),
      'consumer_secret' => $config
        ->get('consumer_secret'),
      'login_url' => $config
        ->get('login_url'),
    ];
    $oauth
      ->set('provider_settings', $settings)
      ->save();
    $config
      ->set('salesforce_auth_provider', 'oauth_default')
      ->save();
    $message = 'Default OAuth provider created from legacy credentials.';
  }
  return $message;
}