View source
<?php
namespace Drupal\tfa_test_plugins\Plugin\TfaSetup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\tfa\Plugin\TfaBasePlugin;
use Drupal\tfa\Plugin\TfaSetupInterface;
class TfaTestValidationPluginSetupPlugin extends TfaBasePlugin implements TfaSetupInterface {
use StringTranslationTrait;
public function ready() {
return TRUE;
}
public function getSetupForm(array $form, FormStateInterface $form_state) {
$form['expected_field'] = [
'#type' => 'textfield',
'#title' => $this
->t('Expected field'),
'#required' => TRUE,
];
$form['actions']['#type'] = 'actions';
$form['actions']['login'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Verify and save'),
];
return $form;
}
public function validateSetupForm(array $form, FormStateInterface $form_state) {
$expected_value = $form_state
->getValue('expected_field');
if (empty($expected_value)) {
$form_state
->setError($form['expected_field'], $this
->t('Missing expected value.'));
return FALSE;
}
return TRUE;
}
public function submitSetupForm(array $form, FormStateInterface $form_state) {
$encrypted = $this
->encrypt($form_state
->getValue('expected_field'));
$record = [
'test_data' => [
'expected_field' => base64_encode($encrypted),
],
];
$this
->setUserData($this->pluginDefinition['id'], $record, $this->uid, $this->userData);
return TRUE;
}
public function getExpectedFieldData() {
$data = $this
->getUserData($this->pluginDefinition['id'], 'test_data', $this->uid, $this->userData);
if (!empty($data['expected_field'])) {
return $this
->decrypt(base64_decode($data['expected_field']));
}
return NULL;
}
public function getHelpLinks() {
return [];
}
public function getSetupMessages() {
return [];
}
public function getOverview(array $params) {
return [
'heading' => [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this
->t('TFA application'),
],
'link' => [
'#theme' => 'links',
'#links' => [
'admin' => [
'title' => !$params['enabled'] ? $this
->t('Set up application') : $this
->t('Reset application'),
'url' => Url::fromRoute('tfa.validation.setup', [
'user' => $params['account']
->id(),
'method' => $params['plugin_id'],
]),
],
],
],
];
}
}