Get boolean if TFA is set?

Created on 26 April 2019, over 5 years ago
Updated 24 October 2024, 22 days ago

Hello everybody!

For my Drupal website I need to check if TFA is set. If it is not set I would like to make the page display only the message that TFA is not set up and not the rest of the content. My idea is to get the isReady() boolean in the $_SESSION and then use it in my twig to check before displaying parts of the page however I seem to have some issues with this.

In the TfaLoginForm.php I have the following code:

public function loginWithoutTfa(FormStateInterface $form_state) {
    // User may be able to skip TFA, depending on module settings and number of
    // prior attempts.
    $remaining = $this->tfaContext->remainingSkips();
    if ($remaining) {
      $user = $this->tfaContext->getUser();
      $tfa_setup_link = Url::fromRoute('tfa.overview', [
        'user' => $user->id(),
      ])->toString();
      drupal_set_message($this->t('You are required to setup two-factor authentication <a href="@link">here.</a> You have @remaining attempts left after this you will be unable to login.', [
        '@remaining' => $remaining - 1,
        '@link' => $tfa_setup_link,
      ]), 'error');
      $this->tfaContext->hasSkipped();
      $this->tfaContext->doUserLogin();
      $form_state->setRedirect('<front>');
      $_SESSION['lists']['CheckTFA'] = $this->tfaContext->isReady();   <--- The line I added.
    }

then in my theme hook I assign this value to the $variables array like this:

$variables["check"] = $_SESSION['lists']['CheckTFA'];

and I use it in the twig like this:

{% if check %}

This does not do anything and when I try to only display it on the page nothing shows up.
Can someone give a look and help me with what I am doing wrong?

Viktor

💬 Support request
Status

Closed: outdated

Component

Code

Created by

🇧🇬Bulgaria JustViktor

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇪Belgium matdemeue

    We had the same problem that we needed to check if a user had TFA enabled.

    The solution we provided was the following:

    $getUserTfaSettings =  \Drupal::service('user.data')->get('tfa', $user_id, 'tfa_user_settings');
    $userTfaStatus = $getUserTfaSettings ? $getUserTfaSettings['status'] : 0;
    

    I don't know if it is the perfect solution but in our case it does it job.

Production build 0.71.5 2024