You are here

public function SettingsForm::buildForm in Security Review 8

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 ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 93

Class

SettingsForm
Settings page for Security Review.

Namespace

Drupal\security_review\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get the list of checks.
  $checks = $this->checklist
    ->getChecks();

  // Get the user roles.
  $roles = user_roles();
  $options = [];
  foreach ($roles as $rid => $role) {
    $options[$rid] = $role
      ->label();
  }

  // Notify the user if anonymous users can create accounts.
  $message = '';
  if (in_array(AccountInterface::AUTHENTICATED_ROLE, $this->security
    ->defaultUntrustedRoles())) {
    $message = $this
      ->t('You have allowed anonymous users to create accounts without approval so the authenticated role defaults to untrusted.');
  }

  // Show the untrusted roles form element.
  $form['untrusted_roles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Untrusted roles'),
    '#description' => $this
      ->t('Define which roles are for less trusted users. The anonymous role defaults to untrusted. @message Most Security Review checks look for resources usable by untrusted roles.', [
      '@message' => $message,
    ]),
    '#options' => $options,
    '#default_value' => $this->security
      ->untrustedRoles(),
  ];
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced'),
    '#open' => TRUE,
  ];

  // Show the logging setting.
  $form['advanced']['logging'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Log checklist results and skips'),
    '#description' => $this
      ->t('The result of each check and skip can be logged to watchdog for tracking.'),
    '#default_value' => $this->securityReview
      ->isLogging(),
  ];

  // Skipped checks.
  $values = [];
  $options = [];
  foreach ($checks as $check) {

    // Determine if check is being skipped.
    if ($check
      ->isSkipped()) {
      $values[] = $check
        ->id();
      $label = $this
        ->t('@name <em>skipped by UID @uid on @date</em>', [
        '@name' => $check
          ->getTitle(),
        '@uid' => $check
          ->skippedBy()
          ->id(),
        '@date' => $this->dateFormatter
          ->format($check
          ->skippedOn()),
      ]);
    }
    else {
      $label = $check
        ->getTitle();
    }
    $options[$check
      ->id()] = $label;
  }
  $form['advanced']['skip'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Checks to skip'),
    '#description' => $this
      ->t('Skip running certain checks. This can also be set on the <em>Run & review</em> page. It is recommended that you do not skip any checks unless you know the result is wrong or the process times out while running.'),
    '#options' => $options,
    '#default_value' => $values,
  ];

  // Iterate through checklist and get check-specific setting pages.
  foreach ($checks as $check) {

    // Get the check's setting form.
    $check_form = $check
      ->settings()
      ->buildForm();

    // If not empty, add it to the form.
    if (!empty($check_form)) {

      // If this is the first non-empty setting page initialize the 'details'
      if (!isset($form['advanced']['check_specific'])) {
        $form['advanced']['check_specific'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Check-specific settings'),
          '#open' => FALSE,
          '#tree' => TRUE,
        ];
      }

      // Add the form.
      $sub_form =& $form['advanced']['check_specific'][$check
        ->id()];
      $title = $check
        ->getTitle();

      // If it's an external check, show its namespace.
      if ($check
        ->getMachineNamespace() != 'security_review') {
        $title .= $this
          ->t('%namespace', [
          '%namespace' => $check
            ->getNamespace(),
        ]);
      }
      $sub_form = [
        '#type' => 'details',
        '#title' => $title,
        '#open' => TRUE,
        '#tree' => TRUE,
        'form' => $check_form,
      ];
    }
  }

  // Return the finished form.
  return parent::buildForm($form, $form_state);
}