You are here

public function ToggleController::index in Security Review 8

Handles check toggling.

Parameters

string $check_id: The ID of the check.

Return value

\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse The response.

1 string reference to 'ToggleController::index'
security_review.routing.yml in ./security_review.routing.yml
security_review.routing.yml

File

src/Controller/ToggleController.php, line 80

Class

ToggleController
Responsible for handling the toggle links on the Run & Review page.

Namespace

Drupal\security_review\Controller

Code

public function index($check_id) {

  // Determine access type.
  $ajax = $this->request->query
    ->get('js') == 1;

  // Validate token.
  $token = $this->request->query
    ->get('token');
  if ($this->csrfToken
    ->validate($token, $check_id)) {

    // Toggle.
    $check = $this->checklist
      ->getCheckById($check_id);
    if ($check != NULL) {
      if ($check
        ->isSkipped()) {
        $check
          ->enable();
      }
      else {
        $check
          ->skip();
      }
    }

    // Output.
    if ($ajax) {
      return new JsonResponse([
        'skipped' => $check
          ->isSkipped(),
        'toggle_text' => $check
          ->isSkipped() ? $this
          ->t('Enable') : $this
          ->t('Skip'),
        'toggle_href' => Url::fromRoute('security_review.toggle', [
          'check_id' => $check
            ->id(),
        ], [
          'query' => [
            'token' => $this->csrfToken
              ->get($check
              ->id()),
            'js' => 1,
          ],
        ])
          ->toString(),
      ]);
    }
    else {

      // Set message.
      if ($check
        ->isSkipped()) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('@name check skipped.', [
          '@name' => $check
            ->getTitle(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addMessage($this
          ->t('@name check no longer skipped.', [
          '@name' => $check
            ->getTitle(),
        ]));
      }

      // Redirect back to Run & Review.
      return $this
        ->redirect('security_review');
    }
  }

  // Go back to Run & Review if the access was wrong.
  return $this
    ->redirect('security_review');
}