View source
<?php
namespace Drupal\lockr\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Lockr\Lockr;
use Drupal\lockr\CertWriter;
use Drupal\lockr\SettingsFactory;
class LockrRegisterForm implements ContainerInjectionInterface, FormInterface {
use StringTranslationTrait;
protected $lockr;
protected $settingsFactory;
protected $configFactory;
protected $messenger;
public function __construct(Lockr $lockr, SettingsFactory $settings_factory, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, TranslationInterface $translation) {
$this->lockr = $lockr;
$this->settingsFactory = $settings_factory;
$this->configFactory = $config_factory;
$this->messenger = $messenger;
$this
->setStringTranslation($translation);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('lockr.lockr'), $container
->get('lockr.settings_factory'), $container
->get('config.factory'), $container
->get('messenger'), $container
->get('string_translation'));
}
public function getFormId() {
return 'lockr_register_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'lockr/register';
$form['#attached']['drupalSettings']['lockr'] = [
'site_name' => $this
->getSiteName(),
'accounts_host' => 'https://accounts.lockr.io',
];
$form['client_token'] = [
'#type' => 'hidden',
'#required' => TRUE,
];
$form['register'] = [
'#type' => 'button',
'#value' => $this
->t('Register Site'),
'#attributes' => [
'class' => [
'register-site',
],
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#attributes' => [
'class' => [
'register-submit',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$client_token = $form_state
->getValue('client_token');
$partner = $this->settingsFactory
->getPartner();
try {
if (is_null($partner)) {
$dn = [
'countryName' => 'US',
'stateOrProvinceName' => 'Washington',
'localityName' => 'Tacoma',
'organizationName' => 'Lockr',
];
$result = $this->lockr
->createCertClient($client_token, $dn);
CertWriter::writeCerts('dev', $result);
$config = $this->configFactory
->getEditable('lockr.settings');
$config
->set('custom', TRUE);
$config
->set('cert_path', 'private://lockr/dev/pair.pem');
$config
->save();
}
elseif ($partner['name'] === 'pantheon') {
$this->lockr
->createPantheonClient($client_token);
}
} catch (\Exception $e) {
throw $e;
return;
}
$this->messenger
->addMessage($this
->t("That's it! You're signed up with Lockr; your keys are now safe."));
$form_state
->setRedirect('entity.key.collection');
}
protected function getSiteName() {
return $this->configFactory
->get('system.site')
->get('name');
}
}