View source
<?php
namespace Drupal\tfa\Plugin\TfaSetup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\tfa\Plugin\TfaSetupInterface;
use Drupal\tfa\Plugin\TfaValidation\TfaRecoveryCode;
use Drupal\tfa\TfaDataTrait;
class TfaRecoveryCodeSetup extends TfaRecoveryCode implements TfaSetupInterface {
use TfaDataTrait;
public function ready() {
return TRUE;
}
public function getOverview(array $params) {
return [
'heading' => [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this
->t('Recovery Codes'),
],
'description' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Generate one-time use codes for two-factor login. These are generally used to recover your account in case you lose access to another 2nd-factor device.'),
],
'setup' => [
'#theme' => 'links',
'#links' => [
'reset' => [
'title' => !$params['enabled'] ? $this
->t('Generate codes') : $this
->t('Reset codes'),
'url' => Url::fromRoute('tfa.plugin.reset', [
'user' => $params['account']
->id(),
'method' => $params['plugin_id'],
'reset' => 1,
]),
],
],
],
'show_codes' => [
'#theme' => 'links',
'#access' => $params['enabled'],
'#links' => [
'show' => [
'title' => $this
->t('Show codes'),
'url' => Url::fromRoute('tfa.validation.setup', [
'user' => $params['account']
->id(),
'method' => $params['plugin_id'],
]),
],
],
],
];
}
public function getSetupForm(array $form, FormStateInterface $form_state, $reset = 0) {
$codes = $this
->getCodes();
if (!empty($reset)) {
$codes = $this
->generateCodes();
foreach ($codes as $key => $code) {
$codes[$key] = implode(' ', str_split($code, 3));
}
$form['recovery_codes'] = [
'#type' => 'value',
'#value' => $codes,
];
}
$form['recovery_codes_output'] = [
'#title' => $this
->t('Recovery Codes'),
'#theme' => 'item_list',
'#items' => $codes,
];
$form['description'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Print or copy these codes and store them somewhere safe before continuing.'),
];
if (!empty($reset)) {
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Save codes to account'),
];
}
return $form;
}
public function validateSetupForm(array $form, FormStateInterface $form_state) {
if (!empty($form_state
->getValue('recovery_codes'))) {
return TRUE;
}
return FALSE;
}
public function submitSetupForm(array $form, FormStateInterface $form_state) {
$this
->storeCodes($form_state
->getValue('recovery_codes'));
return TRUE;
}
public function getHelpLinks() {
return [];
}
public function getSetupMessages() {
return $this->pluginDefinition['setupMessages'] ?: [];
}
}