RevokeAuthorizationForm.php in Salesforce Suite 8.3
File
src/Form/RevokeAuthorizationForm.php
View source
<?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 Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class RevokeAuthorizationForm extends ConfigFormBase {
protected $client;
protected $eventDispatcher;
protected $state;
public function __construct(ConfigFactoryInterface $config_factory, RestClientInterface $salesforce_client, EventDispatcherInterface $event_dispatcher) {
parent::__construct($config_factory);
$this->client = $salesforce_client;
$this->eventDispatcher = $event_dispatcher;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('salesforce.client'), $container
->get('event_dispatcher'));
}
public function getFormId() {
return 'salesforce_oauth';
}
protected function getEditableConfigNames() {
return [
'salesforce.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (!$this->client
->isAuthorized()) {
drupal_set_message($this
->t('Drupal is not authenticated to Salesforce.'), 'warning');
return;
}
$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');
$form['#theme'] = 'system_config_form';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->client
->setAccessToken('');
$this->client
->setRefreshToken('');
$this->client
->setInstanceUrl('');
$this->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."));
}
}