3 namespace Drupal\security_review\Controller;
5 use Drupal\Core\Access\CsrfTokenGenerator;
6 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\security_review\Checklist;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\JsonResponse;
11 use Symfony\Component\HttpFoundation\RequestStack;
14 * Responsible for handling the toggle links on the Run & Review page.
16 class ToggleController extends ControllerBase {
19 * The security_review.checklist service.
21 * @var \Drupal\security_review\Checklist
26 * The CSRF Token generator.
28 * @var \Drupal\Core\Access\CsrfTokenGenerator $csrfToken
35 * @var \Symfony\Component\HttpFoundation\Request $request
40 * Constructs a ToggleController.
42 * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
43 * The CSRF Token generator.
44 * @param \Symfony\Component\HttpFoundation\RequestStack $request
46 * @param \Drupal\security_review\Checklist $checklist
47 * The security_review.checklist service.
49 public function __construct(CsrfTokenGenerator $csrf_token_generator, RequestStack $request, Checklist $checklist) {
50 $this->checklist = $checklist;
51 $this->csrfToken = $csrf_token_generator;
52 $this->request = $request->getCurrentRequest();
58 public static function create(ContainerInterface $container) {
60 $container->get('csrf_token'),
61 $container->get('request_stack'),
62 $container->get('security_review.checklist')
67 * Handles check toggling.
69 * @param string $check_id
70 * The ID of the check.
72 * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
75 public function index($check_id) {
76 // Determine access type.
77 $ajax = $this->request->query->get('js') == 1;
80 $token = $this->request->query->get('token');
81 if ($this->csrfToken->validate($token, $check_id)) {
83 $check = $this->checklist->getCheckById($check_id);
85 if ($check->isSkipped()) {
95 return new JsonResponse([
96 'skipped' => $check->isSkipped(),
97 'toggle_text' => $check->isSkipped() ? $this->t('Enable') : $this->t('Skip'),
98 'toggle_href' => Url::fromRoute(
99 'security_review.toggle',
100 ['check_id' => $check->id()],
103 'token' => $this->csrfToken->get($check->id()),
112 if ($check->isSkipped()) {
113 drupal_set_message($this->t(
114 '@name check skipped.',
115 ['@name' => $check->getTitle()]
119 drupal_set_message($this->t(
120 '@name check no longer skipped.',
121 ['@name' => $check->getTitle()]
125 // Redirect back to Run & Review.
126 return $this->redirect('security_review');
130 // Go back to Run & Review if the access was wrong.
131 return $this->redirect('security_review');