Commit 8a7f8da8 authored by john_a's avatar john_a Committed by John.Avery
Browse files

Issue #3193522 by pcambra, john_a: Support scope and account id for BU

parent d04e7ac9
client_id: ""
client_secret: ""
scope: ""
account_id: 0
validate_json: true
do_not_send: false
base_url: "https://www.exacttargetapis.com"
......
......@@ -10,6 +10,12 @@ marketing_cloud.settings:
client_secret:
type: string
label: 'Client Secret'
scope:
type: text
label: 'Scope'
account_id:
type: integer
label: 'Account ID'
validate_json:
type: boolean
label: 'Validate JSON'
......
......@@ -77,3 +77,18 @@ function marketing_cloud_update_8004() {
$settings->clear('token')->save();
$settings->clear('requesting_token')->save();
}
/**
* Add new scope and account_id config vars.
*
* @see https://www.drupal.org/project/marketing_cloud/issues/3193522
*/
function marketing_cloud_update_8005() {
$settings = Drupal::configFactory()->getEditable('marketing_cloud.settings');
if ($settings->get('scope') === NULL) {
$settings->set('scope', '')->save();
}
if ($settings->get('account_id') === NULL) {
$settings->set('account_id', 0)->save();
}
}
......@@ -45,6 +45,21 @@ class MarketingCloudSettings extends ConfigFormBase {
'#default_value' => $config->get('client_secret'),
];
$form['scope'] = [
'#type' => 'textarea',
'#title' => $this->t('Scope'),
'#description' => $this->t('Space-separated list of data-access permissions for your application. Review <a href="@url">REST API Permission IDs and Scopes</a> for a full list of permissions.', ['@url' => 'https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/rest-permissions-and-scopes.htm']),
'#default_value' => $config->get('scope'),
];
$form['account_id'] = [
'#type' => 'number',
'#min' => 0,
'#title' => $this->t('Account ID'),
'#description' => $this->t('Account identifier, or MID, of the target business unit. Use to switch between business units. If you don’t specify account_id, the returned access token is in the context of the business unit that created the integration.'),
'#default_value' => $config->get('account_id'),
];
$form['validate_json'] = [
'#type' => 'checkbox',
'#title' => $this->t('Validate JSON'),
......@@ -121,6 +136,8 @@ class MarketingCloudSettings extends ConfigFormBase {
$config = $this->config('marketing_cloud.settings');
$config->set('client_id', $form_state->getValue('client_id'));
$config->set('client_secret', $form_state->getValue('client_secret'));
$config->set('scope', $form_state->getValue('scope'));
$config->set('account_id', $form_state->getValue('account_id'));
$config->set('validate_json', $form_state->getValue('validate_json'));
$config->set('do_not_send', $form_state->getValue('do_not_send'));
$config->set('base_url', $form_state->getValue('base_url'));
......
......@@ -95,12 +95,14 @@ class MarketingCloudSession {
$token_requisites = FALSE;
}
$scope = $this->config->get('scope');
$accountId = $this->config->get('account_id');
// Make token request/s.
if ($token_requisites) {
$loginAttempts = 0;
$loginAttemptsMax = $this->config->get('login_attempts_max');
while (!$token && $loginAttempts++ < $loginAttemptsMax) {
$token = $this->requestToken($clientId, $clientSecret);
$token = $this->requestToken($clientId, $clientSecret, $scope, $accountId);
}
}
......@@ -117,11 +119,15 @@ class MarketingCloudSession {
* The marketing Cloud client ID.
* @param string $clientSecret
* The Marketing Cloud secret.
* @param string $scope
* The Marketing Cloud scope.
* @param integer $accountId
* The Marketing Cloud account id.
*
* @return bool|string
* The result of the token request, or FALSE on failure.
*/
private function requestToken($clientId, $clientSecret) {
private function requestToken($clientId, $clientSecret, $scope = '', $accountId = 0) {
try {
\Drupal::logger(__METHOD__)->info('%message', ['%message' => 'Fetching a new token.']);
$url = $this->config
......@@ -151,6 +157,13 @@ class MarketingCloudSession {
break;
}
if ($scope) {
$formParams['scope'] = $scope;
}
if ($accountId) {
$formParams['account_id'] = $accountId;
}
$response = \Drupal::httpClient()->post($url, [
'verify' => FALSE,
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment