public function TfaTrustedBrowserSetup::getSetupForm in Two-factor Authentication (TFA) 8
Get the setup form for the validation method.
Parameters
array $form: The configuration form array.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array Form API array.
Overrides TfaSetupInterface::getSetupForm
File
- src/
Plugin/ TfaSetup/ TfaTrustedBrowserSetup.php, line 31
Class
- TfaTrustedBrowserSetup
- TFA Trusted Browser Setup Plugin.
Namespace
Drupal\tfa\Plugin\TfaSetupCode
public function getSetupForm(array $form, FormStateInterface $form_state) {
$existing = $this
->getTrustedBrowsers();
$time = $this->expiration / 86400;
$form['info'] = [
'#type' => 'markup',
'#markup' => '<p>' . $this
->t("Trusted browsers are a method for\n simplifying login by avoiding verification code entry for a set amount of\n time, @time days from marking a browser as trusted. After @time days, to\n log in you'll need to enter a verification code with your username and\n password during which you can again mark the browser as trusted.", [
'@time' => $time,
]) . '</p>',
];
// Present option to trust this browser if its not currently trusted.
if (isset($_COOKIE[$this->cookieName]) && $this
->trustedBrowser($_COOKIE[$this->cookieName]) !== FALSE) {
$current_trusted = $_COOKIE[$this->cookieName];
}
else {
$current_trusted = FALSE;
$form['trust'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Trust this browser?'),
'#default_value' => empty($existing) ? 1 : 0,
];
// Optional field to name this browser.
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name this browser'),
'#maxlength' => 255,
'#description' => $this
->t('Optionally, name the browser on your browser (e.g.
"home firefox" or "office desktop windows"). Your current browser user
agent is %browser', [
'%browser' => $_SERVER['HTTP_USER_AGENT'],
]),
'#default_value' => $this
->getAgent(),
'#states' => [
'visible' => [
':input[name="trust"]' => [
'checked' => TRUE,
],
],
],
];
}
if (!empty($existing)) {
$form['existing'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Existing browsers'),
'#description' => $this
->t('Leave checked to keep these browsers in your trusted log in list.'),
'#tree' => TRUE,
];
foreach ($existing as $browser_id => $browser) {
$date_formatter = \Drupal::service('date.formatter');
$vars = [
'@set' => $date_formatter
->format($browser['created']),
];
if (isset($browser['last_used'])) {
$vars['@time'] = $date_formatter
->format($browser['last_used']);
}
if ($current_trusted == $browser_id) {
$name = '<strong>' . $this
->t('@name (current browser)', [
'@name' => $browser['name'],
]) . '</strong>';
}
else {
$name = Html::escape($browser['name']);
}
if (empty($browser['last_used'])) {
$message = $this
->t('Marked trusted @set', $vars);
}
else {
$message = $this
->t('Marked trusted @set, last used for log in @time', $vars);
}
$form['existing']['trusted_browser_' . $browser_id] = [
'#type' => 'checkbox',
'#title' => $name,
'#description' => $message,
'#default_value' => 1,
];
}
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Save'),
];
return $form;
}