Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Checks / ViewsAccess.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Checks\ViewsAccess.
6  */
7
8 namespace Drupal\security_review\Checks;
9
10 use Drupal\Core\Url;
11 use Drupal\security_review\Check;
12 use Drupal\security_review\CheckResult;
13 use Drupal\views\Entity\View;
14
15 /**
16  * Checks for Views that do not check access.
17  */
18 class ViewsAccess extends Check {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getNamespace() {
24     return 'Security Review';
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function getTitle() {
31     return 'Views access';
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function run() {
38     // If views is not enabled return with INFO.
39     if (!$this->moduleHandler()->moduleExists('views')) {
40       return $this->createResult(CheckResult::INFO);
41     }
42
43     $result = CheckResult::SUCCESS;
44     $findings = [];
45
46     $views = View::loadMultiple();
47     /** @var View[] $views */
48
49     // Iterate through views and their displays.
50     foreach ($views as $view) {
51       if ($view->status()) {
52         foreach ($view->get('display') as $display_name => $display) {
53           $access = &$display['display_options']['access'];
54           if (isset($access) && $access['type'] == 'none') {
55             // Access is not controlled for this display.
56             $findings[$view->id()][] = $display_name;
57           }
58         }
59       }
60     }
61
62     if (!empty($findings)) {
63       $result = CheckResult::FAIL;
64     }
65
66     return $this->createResult($result, $findings);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function help() {
73     $paragraphs = [];
74     $paragraphs[] = $this->t("Views can check if the user is allowed access to the content. It is recommended that all Views implement some amount of access control, at a minimum checking for the permission 'access content'.");
75
76     return [
77       '#theme' => 'check_help',
78       '#title' => $this->t('Views access'),
79       '#paragraphs' => $paragraphs,
80     ];
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function evaluate(CheckResult $result) {
87     $findings = $result->findings();
88     if (empty($findings)) {
89       return [];
90     }
91
92     $paragraphs = [];
93     $paragraphs[] = $this->t('The following View displays do not check access.');
94
95     $items = [];
96     foreach ($findings as $view_id => $displays) {
97       $view = View::load($view_id);
98       /** @var View $view */
99
100       foreach ($displays as $display) {
101         $items[] = $this->l(
102           $view->label() . ': ' . $display,
103           Url::fromRoute(
104             'entity.view.edit_display_form',
105             [
106               'view' => $view_id,
107               'display_id' => $display,
108             ]
109           )
110         );
111       }
112     }
113
114     return [
115       '#theme' => 'check_evaluation',
116       '#paragraphs' => $paragraphs,
117       '#items' => $items,
118     ];
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function evaluatePlain(CheckResult $result) {
125     $findings = $result->findings();
126     if (empty($findings)) {
127       return '';
128     }
129
130     $output = $this->t('Views without access check:') . ":\n";
131     foreach ($findings as $view_id => $displays) {
132       $output .= "\t" . $view_id . ": " . implode(', ', $displays) . "\n";
133     }
134
135     return $output;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function getMessage($result_const) {
142     switch ($result_const) {
143       case CheckResult::SUCCESS:
144         return $this->t('Views are access controlled.');
145
146       case CheckResult::FAIL:
147         return $this->t('There are Views that do not provide any access checks.');
148
149       case CheckResult::INFO:
150         return $this->t('Module views is not enabled.');
151
152       default:
153         return $this->t('Unexpected result.');
154     }
155   }
156
157 }