You are here

class LockrCSRForm in Lockr 8.2

Hierarchy

Expanded class hierarchy of LockrCSRForm

1 file declares its use of LockrCSRForm
LockrAdminController.php in src/Controller/LockrAdminController.php

File

src/Form/LockrCSRForm.php, line 18

Namespace

Drupal\lockr\Form
View source
class LockrCSRForm implements ContainerInjectionInterface, FormInterface {

  /** @var StateInterface */
  protected $state;

  /** @var ClientFactory */
  protected $clientFactory;

  /** @var StreamWrapperManagerInterface */
  protected $streamWrapperManager;
  public function __construct(StateInterface $state, ClientFactory $client_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
    $this->state = $state;
    $this->clientFactory = $client_factory;
    $this->streamWrapperManager = $stream_wrapper_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('state'), $container
      ->get('lockr.client_factory'), $container
      ->get('stream_wrapper_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'lockr_csr_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $dn = $this->state
      ->get('lockr.cert_dn', []);
    $form['description'] = [
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
    $form['country'] = [
      '#type' => 'textfield',
      '#title' => 'Country',
      '#default' => isset($dn['countryName']) ? $dn['countryName'] : NULL,
      '#maxlength' => 2,
      '#attributes' => [
        'placeholder' => [
          'US',
        ],
      ],
      '#required' => TRUE,
    ];
    $form['state'] = [
      '#type' => 'textfield',
      '#title' => 'State or Province',
      '#default' => isset($dn['stateOrProvinceName']) ? $dn['stateOrProvinceName'] : NULL,
      '#attributes' => [
        'placeholder' => [
          'Washington',
        ],
      ],
      '#required' => TRUE,
    ];
    $form['city'] = [
      '#type' => 'textfield',
      '#title' => 'Locality',
      '#default' => isset($dn['localityName']) ? $dn['localityName'] : NULL,
      '#attributes' => [
        'placeholder' => [
          'Seattle',
        ],
      ],
      '#required' => TRUE,
    ];
    $form['org'] = [
      '#type' => 'textfield',
      '#title' => 'Organization',
      '#default' => isset($dn['organizationName']) ? $dn['organizationName'] : NULL,
      '#attributes' => [
        'placeholder' => [
          'ACME, Inc.',
        ],
      ],
      '#required' => TRUE,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Create Certificate',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $private_path = PrivateStream::basePath();
    if (!is_dir($private_path)) {
      $form_state
        ->setErrorByName('', 'File private path is not a directory, ' . 'please update the private file system path in settings.php. ' . 'Preferrably to a location outside the webroot.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $dn = [
      'countryName' => $form_state
        ->getValue('country'),
      'stateOrProvinceName' => $form_state
        ->getValue('state'),
      'localityName' => $form_state
        ->getValue('city'),
      'organizationName' => $form_state
        ->getValue('org'),
    ];
    $this->state
      ->set('lockr.cert_dn', $dn);
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $result = $site_client
        ->createCert($dn);
    } catch (LockrClientException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('Please check form inputs.', 'error');
      return;
    } catch (LockrServerException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('Lockr encountered an unexpected server error, please try again.', 'error');
    }
    $this->streamWrapperManager
      ->registerWrapper('private', PrivateStream::class, PrivateStream::getType());
    $dir = 'private://lockr/dev';
    mkdir($dir, 0700, TRUE);
    $key_file = "{$dir}/key.pem";
    $key_fd = fopen($key_file, 'w');
    fwrite($key_fd, $result['key_text']);
    fclose($key_fd);
    chmod($key_file, 0600);
    $cert_file = "{$dir}/crt.pem";
    $cert_fd = fopen($cert_file, 'w');
    fwrite($cert_fd, $result['cert_text']);
    fclose($cert_fd);
    chmod($cert_file, 0600);
    $pair_file = "{$dir}/pair.pem";
    $pair_fd = fopen($pair_file, 'w');
    fwrite($pair_fd, $result['key_text']);
    fwrite($pair_fd, $result['cert_text']);
    fclose($pair_fd);
    chmod($pair_file, 0600);
    $private_stream = new PrivateStream();
    $private_stream
      ->setUri("{$dir}/pair.pem");
    $this->state
      ->set('lockr.cert', $private_stream
      ->realpath());
    $this->state
      ->set('lockr.custom', TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrCSRForm::$clientFactory protected property @var ClientFactory
LockrCSRForm::$state protected property @var StateInterface
LockrCSRForm::$streamWrapperManager protected property @var StreamWrapperManagerInterface
LockrCSRForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LockrCSRForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LockrCSRForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LockrCSRForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LockrCSRForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
LockrCSRForm::__construct public function