5551f328eb9a79ab3f1cd90c77ae5bfb3f10f2df
[yaffs-website] / web / modules / contrib / security_review / src / Checks / AdminPermissions.php
1 <?php
2
3 namespace Drupal\security_review\Checks;
4
5 use Drupal\Core\Link;
6 use Drupal\security_review\Check;
7 use Drupal\security_review\CheckResult;
8 use Drupal\user\Entity\Role;
9
10 /**
11  * Checks whether untrusted roles have restricted permissions.
12  */
13 class AdminPermissions extends Check {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getNamespace() {
19     return 'Security Review';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getTitle() {
26     return 'Drupal permissions';
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function getMachineTitle() {
33     return 'admin_permissions';
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function run() {
40     $result = CheckResult::SUCCESS;
41     $findings = [];
42
43     // Get every permission.
44     $all_permissions = $this->security()->permissions(TRUE);
45     $all_permission_strings = array_keys($all_permissions);
46
47     // Get permissions for untrusted roles.
48     $untrusted_permissions = $this->security()->untrustedPermissions(TRUE);
49     foreach ($untrusted_permissions as $rid => $permissions) {
50       $intersect = array_intersect($all_permission_strings, $permissions);
51       foreach ($intersect as $permission) {
52         if (isset($all_permissions[$permission]['restrict access'])) {
53           $findings[$rid][] = $permission;
54         }
55       }
56     }
57
58     if (!empty($findings)) {
59       $result = CheckResult::FAIL;
60     }
61
62     return $this->createResult($result, $findings);
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function help() {
69     $paragraphs = [];
70     $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.");
71     return [
72       '#theme' => 'check_help',
73       '#title' => $this->t('Admin and trusted Drupal permissions'),
74       '#paragraphs' => $paragraphs,
75     ];
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function evaluate(CheckResult $result) {
82     $output = [];
83
84     foreach ($result->findings() as $rid => $permissions) {
85       $role = Role::load($rid);
86       /** @var Role $role */
87       $paragraphs = [];
88       $paragraphs[] = $this->t(
89         "@role has the following restricted permissions:",
90         [
91           '@role' => Link::createFromRoute(
92             $role->label(),
93             'entity.user_role.edit_permissions_form',
94             ['user_role' => $role->id()]
95           )->toString(),
96         ]
97       );
98
99       $output[] = [
100         '#theme' => 'check_evaluation',
101         '#paragraphs' => $paragraphs,
102         '#items' => $permissions,
103       ];
104     }
105
106     return $output;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function evaluatePlain(CheckResult $result) {
113     $output = '';
114
115     foreach ($result->findings() as $rid => $permissions) {
116       $role = Role::load($rid);
117       /** @var Role $role */
118
119       $output .= $this->t(
120         '@role has @permissions',
121         [
122           '@role' => $role->label(),
123           '@permissions' => implode(', ', $permissions),
124         ]
125       );
126       $output .= "\n";
127     }
128
129     return $output;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function getMessage($result_const) {
136     switch ($result_const) {
137       case CheckResult::SUCCESS:
138         return $this->t('Untrusted roles do not have administrative or trusted Drupal permissions.');
139
140       case CheckResult::FAIL:
141         return $this->t('Untrusted roles have been granted administrative or trusted Drupal permissions.');
142
143       default:
144         return $this->t("Unexpected result.");
145     }
146   }
147
148 }