Skip to content
Snippets Groups Projects
Commit 28f1c121 authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Add 'revoke auth' form, fix up some doc blocks

parent 7cfbd2d1
No related branches found
No related tags found
No related merge requests found
...@@ -6,4 +6,4 @@ core: 8.x ...@@ -6,4 +6,4 @@ core: 8.x
configure: entity.salesforce_mapping.list configure: entity.salesforce_mapping.list
dependencies: dependencies:
- salesforce - salesforce
- dynamic_entity_reference - dynamic_entity_reference (2.x)
...@@ -15,9 +15,18 @@ salesforce.global_settings: ...@@ -15,9 +15,18 @@ salesforce.global_settings:
parent: salesforce.admin_config_salesforce parent: salesforce.admin_config_salesforce
title: Salesforce Settings title: Salesforce Settings
description: 'Manage global settings for Salesforce Suite.' description: 'Manage global settings for Salesforce Suite.'
weight: -100
salesforce.authorize: salesforce.authorize:
route_name: salesforce.authorize route_name: salesforce.authorize
parent: salesforce.admin_config_salesforce parent: salesforce.admin_config_salesforce
title: Salesforce Authorization title: Salesforce Authorization
description: 'Manage OAuth consumer key and secret and authorize. View existing authorization details.' description: 'Manage OAuth consumer key and secret and authorize. View existing authorization details.'
weight: 99
salesforce.revoke:
route_name: salesforce.revoke
parent: salesforce.admin_config_salesforce
title: Revoke Salesforce Authorization
description: 'Revoke OAuth tokens.'
weight: 100
...@@ -14,6 +14,15 @@ salesforce.authorize: ...@@ -14,6 +14,15 @@ salesforce.authorize:
requirements: requirements:
_permission: 'authorize salesforce' _permission: 'authorize salesforce'
salesforce.revoke:
path: '/admin/config/salesforce/revoke'
defaults:
_form: '\Drupal\salesforce\Form\RevokeAuthorizationForm'
_title: 'Revoke Salesforce Authorization'
_description: 'Revoke OAuth tokens.'
requirements:
_permission: 'authorize salesforce'
salesforce.global_settings: salesforce.global_settings:
path: '/admin/config/salesforce/settings' path: '/admin/config/salesforce/settings'
defaults: defaults:
......
...@@ -47,7 +47,7 @@ class AuthorizeForm extends ConfigFormBase { ...@@ -47,7 +47,7 @@ class AuthorizeForm extends ConfigFormBase {
* *
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects. * The factory for configuration objects.
* @param \Drupal\salesforce\RestClient $salesforce_client * @param \Drupal\salesforce\Rest\RestClientInterface $salesforce_client
* The factory for configuration objects. * The factory for configuration objects.
* @param \Drupal\Core\State\StateInterface $state * @param \Drupal\Core\State\StateInterface $state
* The state keyvalue collection to use. * The state keyvalue collection to use.
...@@ -144,7 +144,8 @@ class AuthorizeForm extends ConfigFormBase { ...@@ -144,7 +144,8 @@ class AuthorizeForm extends ConfigFormBase {
]; ];
} }
} }
catch (RequestException $e) { catch (\Exception $e) {
// Do not allow any exceptions to interfere with displaying this page.
drupal_set_message($e->getMessage(), 'warning'); drupal_set_message($e->getMessage(), 'warning');
$this->eventDispatcher->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e)); $this->eventDispatcher->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
} }
......
<?php
namespace Drupal\salesforce\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\Event\SalesforceNoticeEvent;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class RevokeAuthorizationForm extends ConfigFormBase {
/**
* The Salesforce REST client.
*
* @var \Drupal\salesforce\Rest\RestClientInterface
*/
protected $sf_client;
/**
* The sevent dispatcher service..
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The state keyvalue collection.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\salesforce\Rest\RestClientInterface $salesforce_client
* The factory for configuration objects.
* @param \Drupal\Core\State\StateInterface $state
* The state keyvalue collection to use.
*/
public function __construct(ConfigFactoryInterface $config_factory, RestClientInterface $salesforce_client, EventDispatcherInterface $event_dispatcher) {
parent::__construct($config_factory);
$this->sf_client = $salesforce_client;
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('salesforce.client'),
$container->get('event_dispatcher')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'salesforce_oauth';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'salesforce.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['actions']['#title'] = 'Are you sure you want to revoke authorization?';
$form['actions']['#type'] = 'details';
$form['actions']['#open'] = TRUE;
$form['actions']['#description'] = t('Revoking authorization will destroy Salesforce OAuth and refresh tokens. Drupal will no longer be authorized to communicate with Salesforce.');
$form['actions']['submit']['#value'] = t('Revoke authorization');
// By default, render the form using system-config-form.html.twig.
$form['#theme'] = 'system_config_form';
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->sf_client->setAccessToken('');
$this->sf_client->setRefreshToken('');
$this->sf_client->setInstanceUrl('');
$this->sf_client->setIdentity(FALSE);
drupal_set_message($this->t('Salesforce OAuth tokens have been revoked.'));
$this->eventDispatcher->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, "Salesforce OAuth tokens revoked."));
}
}
\ No newline at end of file
...@@ -307,12 +307,15 @@ class RestClient implements RestClientInterface { ...@@ -307,12 +307,15 @@ class RestClient implements RestClientInterface {
* E.g., rest, partner, enterprise. * E.g., rest, partner, enterprise.
* *
* @return string * @return string
* Complete URL endpoint for API access. * Complete URL endpoint for API access, or FALSE if no identity is set.
*/ */
public function getApiEndPoint($api_type = 'rest') { public function getApiEndPoint($api_type = 'rest') {
$url = &drupal_static(__FUNCTION__ . $api_type); $url = &drupal_static(__FUNCTION__ . $api_type);
if (!isset($url)) { if (!isset($url)) {
$identity = $this->getIdentity(); $identity = $this->getIdentity();
if (empty($identity)) {
return FALSE;
}
if (is_string($identity)) { if (is_string($identity)) {
$url = $identity; $url = $identity;
} }
...@@ -413,7 +416,7 @@ class RestClient implements RestClientInterface { ...@@ -413,7 +416,7 @@ class RestClient implements RestClientInterface {
* @param string $url * @param string $url
* URL to set. * URL to set.
*/ */
protected function setInstanceUrl($url) { public function setInstanceUrl($url) {
$this->state->set('salesforce.instance_url', $url); $this->state->set('salesforce.instance_url', $url);
return $this; return $this;
} }
...@@ -450,7 +453,7 @@ class RestClient implements RestClientInterface { ...@@ -450,7 +453,7 @@ class RestClient implements RestClientInterface {
* @param string $token * @param string $token
* Refresh token from Salesforce. * Refresh token from Salesforce.
*/ */
protected function setRefreshToken($token) { public function setRefreshToken($token) {
$this->state->set('salesforce.refresh_token', $token); $this->state->set('salesforce.refresh_token', $token);
return $this; return $this;
} }
...@@ -539,7 +542,7 @@ class RestClient implements RestClientInterface { ...@@ -539,7 +542,7 @@ class RestClient implements RestClientInterface {
* *
* @return $this * @return $this
*/ */
protected function setIdentity($data) { public function setIdentity($data) {
$this->state->set('salesforce.identity', $data); $this->state->set('salesforce.identity', $data);
return $this; return $this;
} }
...@@ -596,7 +599,8 @@ class RestClient implements RestClientInterface { ...@@ -596,7 +599,8 @@ class RestClient implements RestClientInterface {
* Whether to reset cache. * Whether to reset cache.
* *
* @return array * @return array
* Array of all available Salesforce versions. * Array of all available Salesforce versions, or empty array if version
* info is not available.
*/ */
public function getVersions($reset = FALSE) { public function getVersions($reset = FALSE) {
if (!$reset && ($cache = $this->cache->get('salesforce:versions'))) { if (!$reset && ($cache = $this->cache->get('salesforce:versions'))) {
...@@ -605,13 +609,18 @@ class RestClient implements RestClientInterface { ...@@ -605,13 +609,18 @@ class RestClient implements RestClientInterface {
$versions = []; $versions = [];
$id = $this->getIdentity(); $id = $this->getIdentity();
$url = str_replace('v{version}/', '', $id['urls']['rest']); if (!empty($identity)) {
$response = new RestResponse($this->httpRequest($url)); $url = str_replace('v{version}/', '', $id['urls']['rest']);
foreach ($response->data as $version) { $response = new RestResponse($this->httpRequest($url));
$versions[$version['version']] = $version; foreach ($response->data as $version) {
$versions[$version['version']] = $version;
}
$this->cache->set('salesforce:versions', $versions, $this->getRequestTime() + self::LONGTERM_CACHE_LIFETIME, ['salesforce']);
return $versions;
}
else {
return [];
} }
$this->cache->set('salesforce:versions', $versions, $this->getRequestTime() + self::LONGTERM_CACHE_LIFETIME, ['salesforce']);
return $versions;
} }
/** /**
......
...@@ -143,6 +143,11 @@ interface RestClientInterface { ...@@ -143,6 +143,11 @@ interface RestClientInterface {
public function getInstanceUrl(); public function getInstanceUrl();
/** /**
* Set the SF instance URL.
*/
public function setInstanceUrl($url);
/**
* Get the access token. * Get the access token.
*/ */
public function getAccessToken(); public function getAccessToken();
...@@ -155,6 +160,14 @@ interface RestClientInterface { ...@@ -155,6 +160,14 @@ interface RestClientInterface {
*/ */
public function setAccessToken($token); public function setAccessToken($token);
/**
* Set the refresh token.
*
* @param string $token
* Refresh token from Salesforce.
*/
public function setRefreshToken($token);
/** /**
* Refresh access token based on the refresh token. * Refresh access token based on the refresh token.
* *
...@@ -191,6 +204,11 @@ interface RestClientInterface { ...@@ -191,6 +204,11 @@ interface RestClientInterface {
*/ */
public function getIdentity(); public function getIdentity();
/**
* Set the Salesforce identity, which is stored in a variable.
*/
public function setIdentity($data);
/** /**
* Helper to build the redirect URL for OAUTH workflow. * Helper to build the redirect URL for OAUTH workflow.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment