RestClient.php in Salesforce Suite 8.3
File
modules/salesforce_encrypt/src/Rest/RestClient.php
View source
<?php
namespace Drupal\salesforce_encrypt\Rest;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\encrypt\EncryptServiceInterface;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\encrypt\EncryptionProfileManagerInterface;
use Drupal\salesforce\EntityNotFoundException;
use Drupal\salesforce\Rest\RestClient as SalesforceRestClient;
use GuzzleHttp\ClientInterface;
use Drupal\Component\Datetime\TimeInterface;
class RestClient extends SalesforceRestClient implements EncryptedRestClientInterface {
use StringTranslationTrait;
protected $encryption;
protected $encryptionProfileManager;
protected $encryptionProfileId;
protected $encryptionProfile;
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json, TimeInterface $time, EncryptServiceInterface $encryption, EncryptionProfileManagerInterface $encryptionProfileManager, LockBackendInterface $lock) {
parent::__construct($http_client, $config_factory, $state, $cache, $json, $time);
$this->encryption = $encryption;
$this->encryptionProfileId = $this->state
->get('salesforce_encrypt.profile');
$this->encryptionProfileManager = $encryptionProfileManager;
$this->encryptionProfile = NULL;
$this->lock = $lock;
}
public function enableEncryption(EncryptionProfileInterface $profile) {
if ($ret = $this
->setEncryption($profile)) {
$this->state
->resetCache();
}
return $ret;
}
public function disableEncryption() {
if ($ret = $this
->setEncryption()) {
$this->state
->resetCache();
}
return $ret;
}
public function hookEncryptionProfileDelete(EncryptionProfileInterface $profile) {
if ($this->encryptionProfileId == $profile
->id()) {
$this
->disableEncryption();
}
}
protected function setEncryption(EncryptionProfileInterface $profile = NULL) {
if (!$this->lock
->acquire('salesforce_encrypt')) {
throw new \RuntimeException('Unable to acquire lock.');
}
$access_token = $this
->getAccessToken();
$refresh_token = $this
->getRefreshToken();
$identity = $this
->getIdentity();
$consumerKey = $this
->getConsumerKey();
$consumerSecret = $this
->getConsumerSecret();
$this->encryptionProfileId = $profile == NULL ? NULL : $profile
->id();
$this->encryptionProfile = $profile;
$this->state
->set('salesforce_encrypt.profile', $this->encryptionProfileId);
$this
->setAccessToken($access_token);
$this
->setRefreshToken($refresh_token);
$this
->setIdentity($identity);
$this
->setConsumerKey($consumerKey);
$this
->setConsumerSecret($consumerSecret);
$this->lock
->release('salesforce_encrypt');
}
public function getEncryptionProfile() {
if ($this->encryptionProfile) {
return $this->encryptionProfile;
}
elseif (empty($this->encryptionProfileId)) {
return NULL;
}
else {
$this->encryptionProfile = $this->encryptionProfileManager
->getEncryptionProfile($this->encryptionProfileId);
if (empty($this->encryptionProfile)) {
throw new EntityNotFoundException([
'id' => $this->encryptionProfileId,
], 'encryption_profile');
}
return $this->encryptionProfile;
}
}
protected function _getEncryptionProfile() {
return $this
->doGetEncryptionProfile();
}
protected function doGetEncryptionProfile() {
try {
$profile = $this
->getEncryptionProfile();
} catch (EntityNotFoundException $e) {
drupal_set_message($this
->t('Error while loading encryption profile. You will need to <a href=":encrypt">assign a new encryption profile</a>, then <a href=":oauth">re-authenticate to Salesforce</a>.', [
':encrypt' => Url::fromRoute('salesforce_encrypt.settings')
->toString(),
':oauth' => Url::fromRoute('salesforce.authorize')
->toString(),
]), 'error');
}
return $profile;
}
public function encrypt($value) {
if (empty($this
->doGetEncryptionProfile())) {
return $value;
}
else {
return $this->encryption
->encrypt($value, $this
->doGetEncryptionProfile());
}
}
public function decrypt($value) {
if (empty($this
->doGetEncryptionProfile()) || empty($value) || mb_strlen($value) === 0) {
return $value;
}
else {
return $this->encryption
->decrypt($value, $this
->doGetEncryptionProfile());
}
}
public function getAccessToken() {
return $this
->decrypt(parent::getAccessToken());
}
public function setAccessToken($token) {
return parent::setAccessToken($this
->encrypt($token));
}
public function getRefreshToken() {
return $this
->decrypt(parent::getRefreshToken());
}
public function setRefreshToken($token) {
return parent::setRefreshToken($this
->encrypt($token));
}
public function setIdentity($data) {
if (is_array($data)) {
$data = serialize($data);
}
return parent::setIdentity($this
->encrypt($data));
}
public function getIdentity() {
$data = $this
->decrypt(parent::getIdentity());
if (!empty($data) && !is_array($data)) {
$data = unserialize($data);
}
return $data;
}
public function getConsumerKey() {
return $this
->decrypt(parent::getConsumerKey());
}
public function setConsumerKey($value) {
return parent::setConsumerKey($this
->encrypt($value));
}
public function getConsumerSecret() {
return $this
->decrypt(parent::getConsumerSecret());
}
public function setConsumerSecret($value) {
return parent::setConsumerSecret($this
->encrypt($value));
}
}
Classes
Name |
Description |
RestClient Deprecated |
Objects, properties, and methods to communicate with the Salesforce REST API. |