You are here

LockrLoginForm.php in Lockr 8.2

Namespace

Drupal\lockr\Form

File

src/Form/LockrLoginForm.php
View source
<?php

/**
 * @file
 * Contains Drupal\lockr\Form\LockrLoginForm.
 */
namespace Drupal\lockr\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Egulias\EmailValidator\EmailValidator;
use Lockr\Exception\LockrClientException;
use Lockr\Exception\LockrServerException;
use Lockr\Exception\LockrException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\lockr\ClientFactory;
class LockrLoginForm extends FormBase {

  /**
   * Constructs a new LockrLoginForm.
   */
  public function __construct(ConfigFactoryInterface $config, ClientFactory $client_factory, RequestStack $requests, EmailValidator $emailValidator) {
    $this->config = $config;
    $this->clientFactory = $client_factory;
    $this->requests = $requests;
    $this->emailValidator = $emailValidator;
  }
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('lockr.client_factory'), $container
      ->get('request_stack'), $container
      ->get('email.validator'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $status = $site_client
        ->exists();
    } catch (LockrServerException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('The Lockr service has returned an error. Please try again.', 'error');
      return $form;
    }
    $exists = $status['exists'];
    if ($exists) {
      $form['registered'] = [
        '#title' => $this
          ->t("You're already registered"),
        '#prefix' => '<p>',
        '#markup' => $this
          ->t("This site is already registered with the Lockr Key Management Service. There's nothing left for you do to here, your keys entered in the key settings are already protected. If you registered with the wrong account, you can click <a href=\"https://lockr.io/user/login\" target=\"_blank\">here</a> to go to Lockr and manage your sites."),
        '#suffix' => '</p>',
      ];
      return $form;
    }
    $default_email = $this->requests
      ->getCurrentRequest()
      ->get('email');
    $form['email'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Email'),
      '#required' => TRUE,
      '#default_value' => $default_email,
      '#description' => $this
        ->t('Enter your Lockr email.'),
    ];
    $form['pass'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Password'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Enter the password that accompanies your email.'),
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Log in'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $email = $form_state
      ->getValue('email');
    if (!$this->emailValidator
      ->isValid($email)) {
      $form_state
        ->setErrorByName('email', $this
        ->t('Please enter a valid email address.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $email = $form_state
      ->getValue('email');
    $pass = $form_state
      ->getValue('pass');
    $name = $this->config
      ->get('system.site')
      ->get('name');
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $site_client
        ->register($email, $pass, $name);
    } catch (LockrClientException $e) {
      if ($e->title === 'Partner mismatch') {
        $msg = $this
          ->t("We didn't recognize your certificate, please ensure the provide path is a valid Lockr certificate.");
      }
      elseif ($e->title === 'Site exists') {
        $msg = $this
          ->t('This site is already registered. If you are experiencing issues, please contact support@lockr.io.');
      }
      elseif ($e->title === 'Credentials invalid') {
        $msg = $this
          ->t('The username and password did not match, please try again.');
      }
      else {
        $msg = $this
          ->t('An unknown error occurred, please try again later.');
      }
      drupal_set_message($msg, 'error');
      return;
    } catch (LockrException $e) {
      $msg = $this
        ->t('An unknown error occurred, please try again later.');
      drupal_set_message($msg, 'error');
      return;
    }
    drupal_set_message($this
      ->t("That's it! You're signed up with Lockr; your keys are now safe."));
    $form_state
      ->setRedirect('entity.key.collection');
  }

}

Classes

Namesort descending Description
LockrLoginForm