You are here

public function MobileNumberTfa::sendCode in Mobile Number 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/TfaValidation/MobileNumberTfa.php \Drupal\mobile_number\Plugin\TfaValidation\MobileNumberTfa::sendCode()

Send the code via the client.

Return value

bool Where sending sms was successful.

2 calls to MobileNumberTfa::sendCode()
MobileNumberTfa::begin in src/Plugin/TfaValidation/MobileNumberTfa.php
MobileNumberTfa::submitForm in src/Plugin/TfaValidation/MobileNumberTfa.php

File

src/Plugin/TfaValidation/MobileNumberTfa.php, line 165
MobileNumberTfa.php

Class

MobileNumberTfa
Class MobileNumberTfa is a validation and sending plugin for TFA.

Namespace

Drupal\mobile_number\Plugin\TfaValidation

Code

public function sendCode() {
  $user = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->load($this->context['uid']);
  $this->code = $this->mobileNumberUtil
    ->generateVerificationCode($this->codeLength);
  try {
    $message = \Drupal::configFactory()
      ->getEditable('mobile_number.settings')
      ->get('tfa_message');
    $message = $message ? $message : $this->mobileNumberUtil->MOBILE_NUMBER_DEFAULT_SMS_MESSAGE;
    if (!($this->verificationToken = $this->mobileNumberUtil
      ->sendVerification($this->mobileNumber, $message, $this->code, [
      'user' => $user,
    ]))) {
      return FALSE;
    }

    // @todo Consider storing date_sent or date_updated to inform user.
    \Drupal::logger('mobile_number_tfa')
      ->info('TFA validation code sent to user @uid', [
      '@uid' => $this->context['uid'],
    ]);
    return TRUE;
  } catch (Exception $e) {
    \Drupal::logger('mobile_number_tfa')
      ->error('Send message error to user @uid. Status code: @code, message: @message', [
      '@uid' => $this->context['uid'],
      '@code' => $e
        ->getCode(),
      '@message' => $e
        ->getMessage(),
    ]);
    return FALSE;
  }
}