You are here

abstract class ClientAuthorizationPluginBase in Entity Share 8.3

Base class for Client authorization plugins.

Hierarchy

Expanded class hierarchy of ClientAuthorizationPluginBase

4 files declare their use of ClientAuthorizationPluginBase
Anonymous.php in modules/entity_share_client/src/Plugin/ClientAuthorization/Anonymous.php
BasicAuth.php in modules/entity_share_client/src/Plugin/ClientAuthorization/BasicAuth.php
Header.php in modules/entity_share_client/src/Plugin/ClientAuthorization/Header.php
Oauth.php in modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php

File

modules/entity_share_client/src/ClientAuthorization/ClientAuthorizationPluginBase.php, line 20

Namespace

Drupal\entity_share_client\ClientAuthorization
View source
abstract class ClientAuthorizationPluginBase extends PluginBase implements ClientAuthorizationInterface, ContainerFactoryPluginInterface {

  /**
   * Injected key service.
   *
   * @var \Drupal\entity_share_client\Service\KeyProvider
   */
  protected $keyService;

  /**
   * The key value store to use.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $keyValueStore;

  /**
   * Injected UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuid;

  /**
   * Injected HTTP client factory.
   *
   * @var \Drupal\Core\Http\ClientFactory
   */
  protected $httpClientFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, KeyProvider $keyProvider, KeyValueFactoryInterface $key_value_factory, UuidInterface $uuid, ClientFactory $clientFactory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->keyService = $keyProvider;
    $this->keyValueStore = $key_value_factory
      ->get(ClientAuthorizationInterface::LOCAL_STORAGE_KEY_VALUE_COLLECTION);
    $this->uuid = $uuid;
    $this->httpClientFactory = $clientFactory;
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_share_client.key_provider'), $container
      ->get('keyvalue'), $container
      ->get('uuid'), $container
      ->get('http_client_factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'uuid' => $this->uuid
        ->generate(),
      'data' => [],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCredentialProvider() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['data']['credential_provider'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageKey() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['data']['storage_key'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form + [
      'credential_provider' => [
        '#type' => 'hidden',
        '#value' => 'entity_share',
      ],
      'entity_share' => [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Stored locally'),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (empty($values['credential_provider'])) {
      $form_state
        ->setError($form['credential_provider'], 'A credential provider is required.');
    }
    else {
      $provider = $values['credential_provider'];
      foreach ($values[$provider] as $key => $value) {
        if (empty($value)) {
          $form_state
            ->setError($form[$provider][$key], 'All credential values are required.');
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $key = NULL;
    $values = $form_state
      ->getValues();
    $configuration = $this
      ->getConfiguration();
    $provider = $values['credential_provider'];
    $credentials = $values[$provider];
    switch ($provider) {
      case 'entity_share':
        $this->keyValueStore
          ->set($configuration['uuid'], $credentials);
        $key = $configuration['uuid'];
        break;
      case 'key':
        $this->keyValueStore
          ->delete($configuration['uuid']);
        $key = $credentials['id'];
        break;
    }
    $configuration['data'] = [
      'credential_provider' => $provider,
      'storage_key' => $key,
    ];
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['label'];
  }

  /**
   * Helper method to build the credential provider elements of the form.
   *
   * @param array $form
   *   The configuration form.
   */
  protected function expandedProviderOptions(array &$form) {
    $provider = $this
      ->getCredentialProvider();

    // Provide selectors for the api key credential provider.
    $form['credential_provider'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Credential provider'),
      '#default_value' => empty($provider) ? 'entity_share' : $provider,
      '#options' => [
        'entity_share' => $this
          ->t('Local storage'),
        'key' => $this
          ->t('Key module'),
      ],
      '#attributes' => [
        'data-states-selector' => 'provider',
      ],
      '#weight' => -99,
    ];
    $form['entity_share']['#states'] = [
      'required' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'entity_share',
        ],
      ],
      'visible' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'entity_share',
        ],
      ],
      'enabled' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'entity_share',
        ],
      ],
    ];
    $key_id = $provider == 'key' ? $this
      ->getStorageKey() : '';
    $form['key'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Managed by the Key module'),
      '#states' => [
        'required' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
        'visible' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
        'enabled' => [
          ':input[data-states-selector="provider"]' => [
            'value' => 'key',
          ],
        ],
      ],
    ];
    $form['key']['id'] = [
      '#type' => 'key_select',
      '#title' => $this
        ->t('Select a stored Key'),
      '#default_value' => $key_id,
      '#empty_option' => $this
        ->t('- Please select -'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientAuthorizationInterface::checkIfAvailable public function Returns true if the plugin method is supported. 4
ClientAuthorizationInterface::getClient public function Prepares a guzzle client for http operations with the supported auth. 4
ClientAuthorizationInterface::getJsonApiClient public function Prepares a guzzle client for JSON operations with the supported auth. 4
ClientAuthorizationInterface::LOCAL_STORAGE_KEY_VALUE_COLLECTION constant The collection ID of for authorization config local storage.
ClientAuthorizationPluginBase::$httpClientFactory protected property Injected HTTP client factory.
ClientAuthorizationPluginBase::$keyService protected property Injected key service.
ClientAuthorizationPluginBase::$keyValueStore protected property The key value store to use.
ClientAuthorizationPluginBase::$uuid protected property Injected UUID service.
ClientAuthorizationPluginBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 4
ClientAuthorizationPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ClientAuthorizationPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
ClientAuthorizationPluginBase::expandedProviderOptions protected function Helper method to build the credential provider elements of the form.
ClientAuthorizationPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ClientAuthorizationPluginBase::getCredentialProvider public function Returns the plugin data if it is set, otherwise returns NULL. Overrides ClientAuthorizationInterface::getCredentialProvider
ClientAuthorizationPluginBase::getLabel public function Gets the plugin label. Overrides ClientAuthorizationInterface::getLabel
ClientAuthorizationPluginBase::getStorageKey public function Returns the plugin data if it is set, otherwise returns NULL. Overrides ClientAuthorizationInterface::getStorageKey
ClientAuthorizationPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ClientAuthorizationPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
ClientAuthorizationPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
ClientAuthorizationPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.