You are here

public function TfaDataTrait::tfaSaveTfaData in Two-factor Authentication (TFA) 8

Save TFA data for an account.

The data like status of tfa, timestamp of last activation or deactivation etc. is stored here.

Parameters

int $uid: The user id.

\Drupal\user\UserDataInterface $user_data: User data.

array $data: Data to be saved.

3 calls to TfaDataTrait::tfaSaveTfaData()
BasicOverview::resetSkipValidationAttempts in src/Form/BasicOverview.php
Resets TFA attempts for the given user account.
BasicSetup::submitForm in src/Form/BasicSetup.php
Form submission handler.
TfaContext::hasSkipped in src/TfaContext.php
Increment the count of $this->getUser() logins without setting up TFA.

File

src/TfaDataTrait.php, line 81

Class

TfaDataTrait
Provides methods to save tfa user settings.

Namespace

Drupal\tfa

Code

public function tfaSaveTfaData($uid, UserDataInterface $user_data, array $data = []) {

  // Check if existing data and update.
  $existing = $this
    ->tfaGetTfaData($uid, $user_data);
  if (isset($existing['validation_skipped']) && !isset($data['validation_skipped'])) {
    $validation_skipped = $existing['validation_skipped'];
  }
  else {
    $validation_skipped = isset($data['validation_skipped']) ? $data['validation_skipped'] : 0;
  }
  if (!empty($existing['data'])) {
    $tfa_data = $existing['data'];
  }
  else {
    $tfa_data = [
      'plugins' => [],
      'sms' => FALSE,
    ];
  }
  if (isset($data['plugins'])) {
    $tfa_data['plugins'][$data['plugins']] = $data['plugins'];
  }
  if (isset($data['sms'])) {
    $tfa_data['sms'] = $data['sms'];
  }
  $status = 1;
  if (isset($data['status']) && $data['status'] === FALSE) {
    $tfa_data = [];
    $status = 0;
  }
  $record = [
    'tfa_user_settings' => [
      'saved' => \Drupal::time()
        ->getRequestTime(),
      'status' => $status,
      'data' => $tfa_data,
      'validation_skipped' => $validation_skipped,
    ],
  ];
  $this
    ->setUserData('tfa', $record, $uid, $user_data);
}