Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Form / RunForm.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Form\RunForm.
6  */
7
8 namespace Drupal\security_review\Form;
9
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\security_review\Checklist;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides implementation for the Run form.
17  */
18 class RunForm extends FormBase {
19
20   /**
21    * The security_review.checklist service.
22    *
23    * @var \Drupal\security_review\Checklist
24    */
25   protected $checklist;
26
27   /**
28    * Constructs a RunForm.
29    *
30    * @param \Drupal\security_review\Checklist $checklist
31    *   The security_review.checklist service.
32    */
33   public function __construct(Checklist $checklist) {
34     $this->checklist = $checklist;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static(
42       $container->get('security_review.checklist')
43     );
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function getFormId() {
50     return 'security-review-run';
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function buildForm(array $form, FormStateInterface $form_state) {
57     if (!$this->currentUser()->hasPermission('run security checks')) {
58       return [];
59     }
60
61     $form['run_form'] = [
62       '#type' => 'details',
63       '#title' => $this->t('Run'),
64       '#description' => $this->t('Click the button below to run the security checklist and review the results.') . '<br />',
65       '#open' => TRUE,
66     ];
67
68     $form['run_form']['submit'] = [
69       '#type' => 'submit',
70       '#value' => $this->t('Run checklist'),
71     ];
72
73     // Return the finished form.
74     return $form;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function submitForm(array &$form, FormStateInterface $form_state) {
81     $batch = [
82       'operations' => [],
83       'finished' => '_security_review_batch_run_finished',
84       'title' => $this->t('Performing Security Review'),
85       'init_message' => $this->t('Security Review is starting.'),
86       'progress_message' => $this->t('Progress @current out of @total.'),
87       'error_message' => $this->t('An error occurred. Rerun the process or consult the logs.'),
88     ];
89
90     foreach ($this->checklist->getEnabledChecks() as $check) {
91       $batch['operations'][] = [
92         '_security_review_batch_run_op',
93         [$check],
94       ];
95     }
96
97     batch_set($batch);
98   }
99
100 }