You are here

public function configure_otp_over_sms_and_email::buildForm in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 src/Form/configure_otp_over_sms_and_email.php \Drupal\miniorange_2fa\form\configure_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/configure_otp_over_sms_and_email.php, line 20

Class

configure_otp_over_sms_and_email

Namespace

Drupal\miniorange_2fa\form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['markup_top_2'] = array(
    '#markup' => '<div class="mo_2fa_table_layout_1"><div class="mo_2fa_table_layout mo_2fa_container">',
  );
  $form['markup_library'] = array(
    '#attached' => array(
      'library' => array(
        "miniorange_2fa/miniorange_2fa.admin",
        "miniorange_2fa/miniorange_2fa.license",
      ),
    ),
  );
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $phoneNumber = MoAuthUtilities::getUserPhoneNumber($user_id);
  $utilities = new MoAuthUtilities();
  $custom_attribute = $utilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;

  /**
   * To check which method (OTP Over Email, OTP Over SMS, OTP Over Email and SMS, OTP Over Phone') is being configured by user
   */
  $url_parts = $utilities::mo_auth_get_url_parts();
  if (in_array(AuthenticationType::$OTP_OVER_EMAIL['id'], $url_parts)) {
    $authTypeCode = AuthenticationType::$OTP_OVER_EMAIL['code'];
    $pageTitle = 'Configure OTP Over Email';
    $note = '<li>Customize Email template.</li>';
    $method_name = strtoupper(AuthenticationType::$OTP_OVER_EMAIL['name']);
  }
  elseif (in_array(AuthenticationType::$SMS['id'], $url_parts)) {
    $authTypeCode = AuthenticationType::$SMS['code'];
    $pageTitle = 'Configure OTP Over SMS';
    $note = '<li>Customize SMS template.</li>';
    $method_name = strtoupper(AuthenticationType::$SMS['name']);
  }
  elseif (in_array(AuthenticationType::$SMS_AND_EMAIL['id'], $url_parts)) {
    $authTypeCode = AuthenticationType::$SMS_AND_EMAIL['code'];
    $pageTitle = 'Configure OTP Over SMS and Email';
    $note = '<li>Customize Email template.</li><li>Customize SMS template.</li>';
    $method_name = strtoupper(AuthenticationType::$SMS_AND_EMAIL['name']);
  }
  elseif (in_array(AuthenticationType::$OTP_OVER_PHONE['id'], $url_parts)) {
    $authTypeCode = AuthenticationType::$OTP_OVER_PHONE['code'];
    $pageTitle = 'Configure OTP Over Phone Call';
    $note = '<li>Customize SMS template.</li>';
    $method_name = strtoupper(AuthenticationType::$OTP_OVER_PHONE['name']);
  }
  elseif (in_array(AuthenticationType::$HARDWARE_TOKEN['id'], $url_parts)) {
    $authTypeCode = AuthenticationType::$HARDWARE_TOKEN['code'];
    $pageTitle = 'Configure Yubikey Hardware Token';
    $note = '';
    $method_name = '';
  }
  $mo_note = "<ul>{$note}<li>" . t('Customize OTP Length and Validity.') . '</li><li>' . t('For customization goto') . ' <a href="' . MoAuthUtilities::get_mo_tab_url('LOGIN') . '">' . t('Login Settings') . '</a> ' . t('tab and navigate to') . ' <u>' . t('CUSTOMIZE SMS AND EMAIL TEMPLATE') . '</u> ' . t('section.') . '</li></ul>';

  /**
   * Create container to hold @ConfigureOTP_Over_SMS_Email_Phone form elements.
   */
  $form['mo_configure_otp_over_sms_email_phone'] = array(
    '#type' => 'fieldset',
    '#title' => t($pageTitle),
    '#attributes' => array(
      'style' => 'padding:2% 2% 30% 2%; margin-bottom:2%',
    ),
  );

  /**
   * Show Yubikey configuration form
   */
  if ($authTypeCode === AuthenticationType::$HARDWARE_TOKEN['code']) {
    $mo_dashboard_url = MoAuthConstants::getBaseUrl() . '/login?username=' . $user_email . '&redirectUrl=' . MoAuthConstants::getBaseUrl() . '/admin/customer/showcustomerconfiguration';
    $form['mo_configure_otp_over_sms_email_phone']['mo_configure_hardware_token_instruction'] = array(
      '#markup' => '<br><hr><br><div class="mo_hardware_token_line_height">
                      <div>' . t('Please follow below-mentioned steps to configure the Yubikey Hardware Token:') . '</div>
                      <ol>
                          <li>' . t('Click') . ' <a target="_blank" href="' . $mo_dashboard_url . '"><strong>' . t('here') . '</strong></a> ' . t('to login into your miniOrange dashboard.') . '</li>
                          <li>' . t('Please navigate to') . '<strong>' . t('YUBIKEY HARDWARE TOKEN') . '</strong> ' . t('and click on ') . '<strong>' . t('Configure') . '</strong>.</li>
                          <li>' . t('Follow the instructions to configure the Yubikey.') . '</li>
                          <li>' . t('When you are done with the configuration, please click on the <strong>Done</strong> button below.</li>') . '
                      </ol>
                    </div><br>',
    );
    $form['mo_configure_otp_over_sms_email_phone']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Done'),
      '#button_type' => 'primary',
      '#submit' => array(
        '\\Drupal\\miniorange_2fa\\MoAuthUtilities::mo_handle_form_cancel',
      ),
      '#limit_validation_errors' => array(),
      '#suffix' => '</div>',
    );
    $utilities::miniOrange_advertise_network_security($form, $form_state);
    return $form;
  }
  $form['mo_configure_otp_over_sms_email_phone']['header']['#markup'] = t('<br><hr><br><div class="mo_2fa_highlight_background_note"><strong>You can customize the following things of the ' . $method_name . ' method:</strong>' . $mo_note . '</div><br>');
  if ($authTypeCode == AuthenticationType::$EMAIL['code'] || $authTypeCode == AuthenticationType::$SMS_AND_EMAIL['code']) {
    $form['mo_configure_otp_over_sms_email_phone']['miniorange_email'] = array(
      '#type' => 'textfield',
      '#title' => t('Verify Your Email') . ' <span style="color: red">*</span>',
      '#value' => $user_email,
      '#attributes' => array(
        'style' => 'width:60%',
      ),
      '#disabled' => TRUE,
    );
  }
  if ($authTypeCode == AuthenticationType::$SMS['code'] || $authTypeCode == AuthenticationType::$SMS_AND_EMAIL['code'] || $authTypeCode == AuthenticationType::$OTP_OVER_PHONE['code']) {
    $form['mo_configure_otp_over_sms_email_phone']['miniorange_phone'] = array(
      '#type' => 'textfield',
      '#title' => t('Verify Your Phone number') . '<span style="color: red">*</span>',
      '#id' => 'query_phone',
      '#default_value' => $phoneNumber,
      '#description' => t('<strong>Note:</strong> Enter phone number with country code Eg. +00xxxxxxxxxx'),
      '#attributes' => array(
        'class' => array(
          'query_phone',
        ),
        'pattern' => '[\\+]?[0-9]{1,4}\\s?[0-9]{7,12}',
        'placeholder' => t('Enter phone number with country code Eg. +00xxxxxxxxxx'),
        'style' => 'width:60%',
      ),
    );
  }
  $form['mo_configure_otp_over_sms_email_phone']['verifyphone'] = array(
    '#type' => 'submit',
    '#value' => t('Verify'),
    '#button_type' => 'primary',
    '#submit' => array(
      '::mo_auth_configure_otp_over_sms_and_email_submit',
    ),
  );
  $form['mo_configure_otp_over_sms_email_phone']['miniorange_saml_customer_setup_resendotp'] = array(
    '#type' => 'submit',
    '#value' => t('Resend OTP'),
    '#submit' => array(
      '::mo_auth_configure_otp_over_sms_and_email_submit',
    ),
    '#suffix' => '<br><br>',
  );
  $form['mo_configure_otp_over_sms_email_phone']['miniorange_OTP'] = array(
    '#type' => 'textfield',
    '#maxlength' => 8,
    '#attributes' => array(
      'placeholder' => t('Enter passcode'),
      'style' => 'width:60%',
    ),
    '#title' => t('OTP <span style="color: red">*</span>'),
  );
  $form['mo_configure_otp_over_sms_email_phone']['authTypeCode'] = array(
    '#type' => 'hidden',
    '#value' => $authTypeCode,
  );
  $form['mo_configure_otp_over_sms_email_phone']['miniorange_saml_customer_validate_otp_button'] = array(
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => t('Validate OTP'),
    '#submit' => array(
      '::miniorange_saml_validate_otp_submit',
    ),
  );
  $form['mo_configure_otp_over_sms_email_phone']['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#button_type' => 'danger',
    '#submit' => array(
      '\\Drupal\\miniorange_2fa\\MoAuthUtilities::mo_handle_form_cancel',
    ),
    '#limit_validation_errors' => array(),
  );
  $form['main_layout_div_end'] = array(
    '#markup' => '<br></div>',
  );
  $utilities::miniOrange_advertise_network_security($form, $form_state);
  return $form;
}