You are here

class LockrLoginForm in Lockr 8.2

Hierarchy

Expanded class hierarchy of LockrLoginForm

1 string reference to 'LockrLoginForm'
lockr.services.yml in ./lockr.services.yml
lockr.services.yml
1 service uses LockrLoginForm
lockr.login_form in ./lockr.services.yml
Drupal\lockr\Form\LockrLoginForm

File

src/Form/LockrLoginForm.php, line 23
Contains Drupal\lockr\Form\LockrLoginForm.

Namespace

Drupal\lockr\Form
View source
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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LockrLoginForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LockrLoginForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LockrLoginForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LockrLoginForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LockrLoginForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
LockrLoginForm::__construct public function Constructs a new LockrLoginForm.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.