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