You are here

LockrKeyProvider.php in Lockr 8.2

File

src/Plugin/KeyProvider/LockrKeyProvider.php
View source
<?php

/**
 * @file
 * Contains Drupal\lockr\Plugin\KeyProvider\LockrKeyProvider.
 */
namespace Drupal\lockr\Plugin\KeyProvider;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\key\KeyInterface;
use Drupal\key\Plugin\KeyPluginFormInterface;
use Drupal\key\Plugin\KeyProviderBase;
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
use Drupal\lockr\ClientFactory;

/**
 * Adds a key provider that allows a key to be stored in Lockr.
 *
 * @KeyProvider(
 *   id = "lockr",
 *   label = "Lockr",
 *   description = @Translation("The Lockr key provider stores the key in Lockr key management service."),
 *   storage_method = "lockr",
 *   key_value = {
 *     "accepted" = TRUE,
 *     "required" = TRUE
 *   }
 * )
 */
class LockrKeyProvider extends KeyProviderBase implements KeyProviderSettableValueInterface, KeyPluginFormInterface {
  protected $client;
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientFactory $client_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->clientFactory = $client_factory;
  }
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('lockr.client_factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'encoded' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $site_client = $this->clientFactory
      ->getSiteClient();
    $status = $site_client
      ->exists();
    if (!$status['exists']) {
      $form['need_register'] = [
        '#prefix' => '<p>',
        '#markup' => $this
          ->t('This site has not yet registered with Lockr, please <a href="@link">click here to register</a>.', [
          '@link' => Url::fromRoute('lockr.admin')
            ->toString(),
        ]),
        '#suffix' => '</p>',
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function getKeyValue(KeyInterface $key) {
    $name = $key
      ->id();
    $encoded = $this
      ->getConfiguration()['encoded'];
    $key_client = $this->clientFactory
      ->getKeyClient();
    return $key_client
      ->encrypted($encoded)
      ->get($name);
  }

  /**
   * {@inheritdoc}
   */
  public function setKeyValue(KeyInterface $key, $key_value) {
    $name = $key
      ->id();
    $label = $key
      ->label();
    $encoded = $this
      ->getConfiguration()['encoded'] ?: NULL;
    $key_client = $this->clientFactory
      ->getKeyClient();
    $encoded = $key_client
      ->encrypted()
      ->set($name, $key_value, $label, $encoded);
    if ($encoded) {
      $this
        ->setConfiguration([
        'encoded' => $encoded,
      ]);
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteKeyValue(KeyInterface $key) {
    $name = $key
      ->id();
    $key_client = $this->clientFactory
      ->getKeyClient();
    return $key_client
      ->delete($name);
  }

}

Classes

Namesort descending Description
LockrKeyProvider Adds a key provider that allows a key to be stored in Lockr.