You are here

public function test_otp_over_sms_and_email::buildForm in Google Authenticator / 2 Factor Authentication - 2FA 8

Same name and namespace in other branches
  1. 8.2 src/Form/test_otp_over_sms_and_email.php \Drupal\miniorange_2fa\Form\test_otp_over_sms_and_email::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/test_otp_over_sms_and_email.php, line 25
OTP Over SMS and Email(test) functions.

Class

test_otp_over_sms_and_email
Menu callback for testing OTP Over SMS and Email.

Namespace

Drupal\miniorange_2fa\Form

Code

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form['markup_top_2'] = array(
    '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
  );
  $form['markup_library'] = array(
    '#attached' => array(
      'library' => array(
        "miniorange_2fa/miniorange_2fa.admin",
        "miniorange_2fa/miniorange_2fa.license",
      ),
    ),
  );
  global $base_url;
  $url = $base_url . '/admin/config/people/miniorange_2fa/setup_twofactor';
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $user_phone = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_phone');
  $authTypeCode = AuthenticationType::$SMS_AND_EMAIL['code'];
  $messageHeader = 'An OTP has been sent to <strong>' . $user_phone . '</strong> and <strong>' . $user_email . '</strong>. Please enter it here to complete the test.';
  $txId_Value = \Drupal::config('miniorange_2fa.settings')
    ->get('txId_Value');
  if ($txId_Value == 'EMPTY_VALUE') {
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), NULL, $user_phone, NULL, $authTypeCode, $user_email);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->challenge($miniorange_user);
    if (isset($response->status) && $response->status != 'SUCCESS') {
      $message = 'An error occured while sending passcode. <em>(' . $response->message . ')</em>';
      MoAuthUtilities::show_error_or_success_message($message, 'error');
    }
    else {
      \Drupal::messenger()
        ->addMessage(t($messageHeader), 'status');
    }

    //Store txId
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('txId_Value', $response->txId)
      ->save();
  }
  $form['header']['#markup'] = '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Test OTP Over SMS and Email</div></div>';
  $form['mo_auth_otpoversms_and_email_token'] = array(
    '#type' => 'textfield',
    '#maxlength' => 6,
    '#attributes' => array(
      'placeholder' => t('Enter passcode.'),
      'class' => array(
        'mo2f-textbox',
        'mo2f-textbox-otp',
      ),
      'style' => 'width:200px',
      'autofocus' => 'true',
    ),
  );
  $form['actions_form_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Verify'),
  );
  $form['actions_form_cancel'] = array(
    '#markup' => '<span>&nbsp;&nbsp;</span><a href="' . $url . ' "> Cancel Test</a>',
  );
  $form['main_layout_div_end'] = array(
    '#markup' => '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>',
  );
  MoAuthUtilities::AddsupportTab($form, $form_state);
  return $form;
}