Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Controller / ToggleController.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Controller\ToggleController.
6  */
7
8 namespace Drupal\security_review\Controller;
9
10 use Drupal\Core\Access\CsrfTokenGenerator;
11 use Drupal\Core\Controller\ControllerBase;
12 use Drupal\Core\Url;
13 use Drupal\security_review\Checklist;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\HttpFoundation\JsonResponse;
16 use Symfony\Component\HttpFoundation\RequestStack;
17
18 /**
19  * Responsible for handling the toggle links on the Run & Review page.
20  */
21 class ToggleController extends ControllerBase {
22
23   /**
24    * The security_review.checklist service.
25    *
26    * @var \Drupal\security_review\Checklist
27    */
28   protected $checklist;
29
30   /**
31    * The CSRF Token generator.
32    *
33    * @var \Drupal\Core\Access\CsrfTokenGenerator $csrfToken
34    */
35   protected $csrfToken;
36
37   /**
38    * The request stack.
39    *
40    * @var \Symfony\Component\HttpFoundation\Request $request
41    */
42   protected $request;
43
44   /**
45    * Constructs a ToggleController.
46    *
47    * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
48    *   The CSRF Token generator.
49    * @param \Symfony\Component\HttpFoundation\RequestStack $request
50    *   The request stack.
51    * @param \Drupal\security_review\Checklist $checklist
52    *   The security_review.checklist service.
53    */
54   public function __construct(CsrfTokenGenerator $csrf_token_generator, RequestStack $request, Checklist $checklist) {
55     $this->checklist = $checklist;
56     $this->csrfToken = $csrf_token_generator;
57     $this->request = $request->getCurrentRequest();
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container) {
64     return new static(
65       $container->get('csrf_token'),
66       $container->get('request_stack'),
67       $container->get('security_review.checklist')
68     );
69   }
70
71   /**
72    * Handles check toggling.
73    *
74    * @param string $check_id
75    *   The ID of the check.
76    *
77    * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
78    *   The response.
79    */
80   public function index($check_id) {
81     // Determine access type.
82     $ajax = $this->request->query->get('js') == 1;
83
84     // Validate token.
85     $token = $this->request->query->get('token');
86     if ($this->csrfToken->validate($token, $check_id)) {
87       // Toggle.
88       $check = $this->checklist->getCheckById($check_id);
89       if ($check != NULL) {
90         if ($check->isSkipped()) {
91           $check->enable();
92         }
93         else {
94           $check->skip();
95         }
96       }
97
98       // Output.
99       if ($ajax) {
100         return new JsonResponse([
101           'skipped' => $check->isSkipped(),
102           'toggle_text' => $check->isSkipped() ? $this->t('Enable') : $this->t('Skip'),
103           'toggle_href' => Url::fromRoute(
104             'security_review.toggle',
105             ['check_id' => $check->id()],
106             [
107               'query' => [
108                 'token' => $this->csrfToken->get($check->id()),
109                 'js' => 1,
110               ],
111             ]
112           )->toString(),
113         ]);
114       }
115       else {
116         // Set message.
117         if ($check->isSkipped()) {
118           drupal_set_message($this->t(
119             '@name check skipped.',
120             ['@name' => $check->getTitle()]
121           ));
122         }
123         else {
124           drupal_set_message($this->t(
125             '@name check no longer skipped.',
126             ['@name' => $check->getTitle()]
127           ));
128         }
129
130         // Redirect back to Run & Review.
131         return $this->redirect('security_review');
132       }
133     }
134
135     // Go back to Run & Review if the access was wrong.
136     return $this->redirect('security_review');
137   }
138
139 }