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