You are here

public function TfaContext::isTfaRequired in Two-factor Authentication (TFA) 8

Check whether $this->getUser() is required to use TFA.

Return value

bool TRUE if $this->getUser() is required to use TFA.

Overrides TfaContextInterface::isTfaRequired

File

src/TfaContext.php, line 133

Class

TfaContext
Provide context for the current login attempt.

Namespace

Drupal\tfa

Code

public function isTfaRequired() {

  // If TFA has been set up for the user, then it is required.
  $user_tfa_data = $this
    ->tfaGetTfaData($this
    ->getUser()
    ->id(), $this->userData);
  if (!empty($user_tfa_data['status']) && !empty($user_tfa_data['data']['plugins'])) {
    return TRUE;
  }

  // If the user has a role that is required to use TFA, then return TRUE.
  $required_roles = array_filter($this->tfaSettings
    ->get('required_roles'));
  $user_roles = $this
    ->getUser()
    ->getRoles();
  return (bool) array_intersect($required_roles, $user_roles);
}