You are here

public function LockrRenewForm::submitForm in Lockr 4.x

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/LockrRenewForm.php, line 170

Class

LockrRenewForm
Form handler for Lockr renew cert.

Namespace

Drupal\lockr\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // 1. Create a new private key and CSR.
  $texts = $this
    ->createCSR();
  if (is_null($texts)) {
    $this->messenger
      ->addError($this
      ->t('Failed to create a CSR. This could be because of an invalid
         OpenSSL installation.'));
    return;
  }

  // 2. Grab the current environment.
  //    This has the side effect of verifying our current cert is valid.
  try {
    $env = $this
      ->getEnv();
  } catch (LockrApiException $e) {
    $this
      ->handleException($e);
    $this->messenger
      ->addError($this
      ->t('An error occurred verifying the current Lockr client.
         Please try again or contact Lockr support.'));
    return;
  }

  // 3. Request a new cert from Lockr.
  try {
    $cert_text = $this
      ->renewCert($texts['csr_text']);
  } catch (LockrApiException $e) {
    $this
      ->handleException($e);
    $this->messenger
      ->addError($this
      ->t('An error occurred renewing the current Lockr certificate.
         Please try again or contact Lockr support.'));
    return;
  }

  // 4. Write the new cert and private key to a new private directory.
  $dir_name = $env . '_' . (new DateTime())
    ->format('YmdHis');
  $dir = $this->certManager
    ->certDir($dir_name);
  $key_text = $texts['key_text'];
  if (!$this->certManager
    ->writeCerts($dir, $cert_text, $key_text)) {
    $this->messenger
      ->addError($this
      ->t('Failed to write certificates.'));
    return;
  }

  // 5. Verify the new cert.
  try {
    $this
      ->getRenewedEnv($dir);
  } catch (LockrApiException $e) {
    $this
      ->handleException($e);
    $this->messenger
      ->addError($this
      ->t('An error occurred verifying the new Lockr certificate.
         It has been saved at @certpath.
         The original certificate is still being used.
         Please try again or contact Lockr support.', [
      '@certpath' => $full_dir,
    ]));
    return;
  }

  // 6. If we cannot write to the current cert location, bail out.
  if (!$this->certManager
    ->certWritable()) {
    $this->messenger
      ->addError($this
      ->t('The destination cert path is not writable.
         New certs have been saved at @certpath.
         The original certificate is still being used.
         Please try again or contact Lockr support.', [
      '@certpath' => $full_dir,
    ]));
    return;
  }

  // 7. Make a backup of the current certificate.
  if (!$this->certManager
    ->backupCert()) {
    $this->messenger
      ->addError($this
      ->t('An error occurred while attempting to backup the current cert.
         In an abundance of caution, it has not been overwritten.'));
    return;
  }

  // 8. Copy new cert into the current location.
  $cert_path = $this->certManager
    ->certPath();
  $current_dir = dirname($cert_path);
  if (!$this->certManager
    ->copyPEM($dir, $current_dir)) {
    $this->messenger
      ->addError($this
      ->t('An error occurred while attempting to place the new cert.
         Please try again or contact Lockr support.'));
  }
  else {
    $this->messenger
      ->addMessage($this
      ->t('Your certificate has been successfully renewed. A backup of
         the previous certificate has been created for recovery purposes.
         Contact Lockr support if you have any questions.'));
  }
}