You are here

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

Same name and namespace in other branches
  1. 8.2 src/Form/MoAuthLoginSettings.php \Drupal\miniorange_2fa\Form\MoAuthLoginSettings::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/MoAuthLoginSettings.php, line 18
Contains support form for miniOrange 2FA Login Module.

Class

MoAuthLoginSettings

Namespace

Drupal\miniorange_2fa\Form

Code

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  global $base_url;
  $url = $base_url . '/admin/config/people/miniorange_2fa/licensing';
  $form['markup_top_2'] = array(
    '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
  );
  $disabled = False;
  if (!MoAuthUtilities::isCustomerRegistered()) {
    $form['header'] = array(
      '#markup' => '<div class="mo_saml_register_message"><p>You need to <a href="' . $base_url . '/admin/config/people/miniorange_2fa/customer_setup">Register/Login</a> with miniOrange before using this module.</p></div><br>',
    );
    $disabled = True;
  }
  $form['markup_library'] = array(
    '#attached' => array(
      'library' => array(
        "miniorange_2fa/miniorange_2fa.admin",
        "miniorange_2fa/miniorange_2fa.license",
      ),
    ),
  );
  $form['mo_auth_enable_two_factor'] = array(
    '#type' => 'checkbox',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_enable_two_factor'),
    '#prefix' => '<div class="mo2f-setup-header mo2f-setup-header-top-left">Enable Two-Factor module</div><div class="mo2f-info">',
    '#suffix' => '</div><br>',
    '#title' => t('Enable Two-Factor module. <span style="color:red;">(If you disable this checkbox, Second-Factor authentication will not be invoked for any user during login.)</span>'),
    '#disabled' => $disabled,
  );
  $license_type = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_2fa_license_type') == '' ? 'DEMO' : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_2fa_license_type');
  $is_free = $license_type == 'DRUPAL_2FA_PLUGIN' || $license_type == 'PREMIUM' || $license_type == 'DRUPAL8_2FA_MODULE' ? FALSE : TRUE;
  $is_free_class = $is_free ? " mo2f-grayed-out" : "";
  $form['mo_auth_enforce_inline_registration'] = array(
    '#type' => 'checkbox',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_enforce_inline_registration'),
    '#prefix' => '<div class="mo2f-setup-header mo2f-setup-header-top-left">Invoke Inline Registration to setup 2nd factor for users <a href= "' . $url . '"><b>[PREMIUM]</b></a></span></div><div class="mo2f-info ' . $is_free_class . '">',
    '#suffix' => '</div><br>',
    '#disabled' => $is_free,
    '#title' => t('Enforce 2 Factor registration for users at login time.'),
  );
  $form['markup_idp_login_note'] = array(
    '#markup' => '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Select Login Screen Options <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info"><div class="mo_saml_highlight_background_note"><b>Note: </b>By default 2nd Factor is enabled after password authentication.
             If you do not want to remember passwords anymore and just login with 2nd Factor, please select below option.</div>',
  );
  $form['mo_auth_two_factor_instead_password'] = array(
    '#type' => 'checkbox',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_use_only_2nd_factor'),
    '#prefix' => '',
    '#suffix' => '</div></div><br>',
    '#disabled' => $is_free,
    '#title' => t('Login with 2nd Factor only (No password required.)'),
    '#description' => '<strong>Note:</strong> To use this feature make sure you have enabled the "ENFORCE 2 FACTOR REGISTRATION FOR USERS AT LOGIN TIME" feature.',
  );
  $form['mo_auth_two_factor_invoke_2fa_depending_upon_IP'] = array(
    '#type' => 'checkbox',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_enable_whitelist_IPs'),
    '#prefix' => '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Whitelist IP addresses <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info">',
    '#disabled' => $is_free,
    '#title' => t('Whitelist IP addresses'),
    '#description' => '<strong>Note:</strong> If you whitelist the IPs then 2FA will not be invoked for those IPs.',
  );
  $form['mo_auth_two_factor_whitelist_IP'] = array(
    '#type' => 'textarea',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_whitelisted_IP_address'),
    '#disabled' => $is_free,
    '#attributes' => array(
      'placeholder' => 'Enter semicolon(;) separated IP addresses ( Format for range: lower_range - upper_range )',
    ),
    '#states' => array(
      // Only show this field when the checkbox is enabled.
      'visible' => array(
        ':input[name="mo_auth_two_factor_invoke_2fa_depending_upon_IP"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#suffix' => '</div></div>',
  );
  $form['mo_auth_two_factor_enable_role_based_2fa'] = array(
    '#type' => 'checkbox',
    '#default_value' => \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_enable_role_based_2fa'),
    '#prefix' => '<br><div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Role based 2FA <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info">
                        <div class="mo_saml_highlight_background_note"><b>Note: </b>If you have enabled "LOGIN WITH 2ND FACTOR ONLY" feature, Second-Factor authentication will invoke for all roles.</div>',
    '#disabled' => $is_free,
    '#title' => t('Enable role based 2FA'),
    '#description' => "<strong>Note:</strong> If you want to enable 2FA for specific roles then, enable this checkbox and select appropriate roles.</br></br>",
  );
  $roles_arr = MoAuthUtilities::get_Existing_Drupal_Roles();
  $selected_roles = (array) json_decode(\Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_role_based_2fa_roles'));
  foreach ($roles_arr as $sysName => $displayName) {
    $form['mo_auth_two_factor_role_based_2fa_roles_' . strtolower($sysName)] = array(
      '#type' => 'checkbox',
      '#default_value' => array_key_exists($sysName, $selected_roles) ? TRUE : FALSE,
      '#states' => array(
        // Only show this field when the checkbox is enabled.
        'visible' => array(
          ':input[name="mo_auth_two_factor_enable_role_based_2fa"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#title' => t($displayName),
    );
  }
  $form['Submit_LoginSettings_form'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
    '#disabled' => $disabled,
    '#prefix' => '</div></div><br><br>',
    '#suffix' => '<br><br><br><br><br><br><br></div>',
  );
  MoAuthUtilities::AddsupportTab($form, $form_state);
  return $form;
}