Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Checks / AdminPermissions.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Checks\AdminPermissions.
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\user\Entity\Role;
14
15 /**
16  * Checks whether untrusted roles have restricted permissions.
17  */
18 class AdminPermissions 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 'Drupal permissions';
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function getMachineTitle() {
38     return 'admin_permissions';
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function run() {
45     $result = CheckResult::SUCCESS;
46     $findings = [];
47
48     // Get every permission.
49     $all_permissions = $this->security()->permissions(TRUE);
50     $all_permission_strings = array_keys($all_permissions);
51
52     // Get permissions for untrusted roles.
53     $untrusted_permissions = $this->security()->untrustedPermissions(TRUE);
54     foreach ($untrusted_permissions as $rid => $permissions) {
55       $intersect = array_intersect($all_permission_strings, $permissions);
56       foreach ($intersect as $permission) {
57         if (isset($all_permissions[$permission]['restrict access'])) {
58           $findings[$rid][] = $permission;
59         }
60       }
61     }
62
63     if (!empty($findings)) {
64       $result = CheckResult::FAIL;
65     }
66
67     return $this->createResult($result, $findings);
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function help() {
74     $paragraphs = [];
75     $paragraphs[] = $this->t("Drupal's permission system is extensive and allows for varying degrees of control. Certain permissions would allow a user total control, or the ability to escalate their control, over your site and should only be granted to trusted users.");
76     return [
77       '#theme' => 'check_help',
78       '#title' => $this->t('Admin and trusted Drupal permissions'),
79       '#paragraphs' => $paragraphs,
80     ];
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function evaluate(CheckResult $result) {
87     $output = [];
88
89     foreach ($result->findings() as $rid => $permissions) {
90       $role = Role::load($rid);
91       /** @var Role $role */
92       $paragraphs = [];
93       $paragraphs[] = $this->t(
94         "@role has the following restricted permissions:",
95         [
96           '@role' => $this->l(
97             $role->label(),
98             Url::fromRoute(
99               'entity.user_role.edit_permissions_form',
100               ['user_role' => $role->id()]
101             )
102           ),
103         ]
104       );
105
106       $output[] = [
107         '#theme' => 'check_evaluation',
108         '#paragraphs' => $paragraphs,
109         '#items' => $permissions,
110       ];
111     }
112
113     return $output;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function evaluatePlain(CheckResult $result) {
120     $output = '';
121
122     foreach ($result->findings() as $rid => $permissions) {
123       $role = Role::load($rid);
124       /** @var Role $role */
125
126       $output .= $this->t(
127         '@role has @permissions',
128         [
129           '@role' => $role->label(),
130           '@permissions' => implode(', ', $permissions),
131         ]
132       );
133       $output .= "\n";
134     }
135
136     return $output;
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function getMessage($result_const) {
143     switch ($result_const) {
144       case CheckResult::SUCCESS:
145         return $this->t('Untrusted roles do not have administrative or trusted Drupal permissions.');
146
147       case CheckResult::FAIL:
148         return $this->t('Untrusted roles have been granted administrative or trusted Drupal permissions.');
149
150       default:
151         return $this->t("Unexpected result.");
152     }
153   }
154
155 }