Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Form / UserPermissionsForm.php
1 <?php
2
3 namespace Drupal\user\Form;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\user\PermissionHandlerInterface;
9 use Drupal\user\RoleStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides the user permissions administration form.
14  */
15 class UserPermissionsForm extends FormBase {
16
17   /**
18    * The permission handler.
19    *
20    * @var \Drupal\user\PermissionHandlerInterface
21    */
22   protected $permissionHandler;
23
24   /**
25    * The role storage.
26    *
27    * @var \Drupal\user\RoleStorageInterface
28    */
29   protected $roleStorage;
30
31   /**
32    * The module handler.
33    *
34    * @var \Drupal\Core\Extension\ModuleHandlerInterface
35    */
36   protected $moduleHandler;
37
38   /**
39    * Constructs a new UserPermissionsForm.
40    *
41    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
42    *   The permission handler.
43    * @param \Drupal\user\RoleStorageInterface $role_storage
44    *   The role storage.
45    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
46    *   The module handler.
47    */
48   public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler) {
49     $this->permissionHandler = $permission_handler;
50     $this->roleStorage = $role_storage;
51     $this->moduleHandler = $module_handler;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function create(ContainerInterface $container) {
58     return new static(
59       $container->get('user.permissions'),
60       $container->get('entity.manager')->getStorage('user_role'),
61       $container->get('module_handler')
62     );
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function getFormId() {
69     return 'user_admin_permissions';
70   }
71
72   /**
73    * Gets the roles to display in this form.
74    *
75    * @return \Drupal\user\RoleInterface[]
76    *   An array of role objects.
77    */
78   protected function getRoles() {
79     return $this->roleStorage->loadMultiple();
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function buildForm(array $form, FormStateInterface $form_state) {
86     $role_names = [];
87     $role_permissions = [];
88     $admin_roles = [];
89     foreach ($this->getRoles() as $role_name => $role) {
90       // Retrieve role names for columns.
91       $role_names[$role_name] = $role->label();
92       // Fetch permissions for the roles.
93       $role_permissions[$role_name] = $role->getPermissions();
94       $admin_roles[$role_name] = $role->isAdmin();
95     }
96
97     // Store $role_names for use when saving the data.
98     $form['role_names'] = [
99       '#type' => 'value',
100       '#value' => $role_names,
101     ];
102     // Render role/permission overview:
103     $hide_descriptions = system_admin_compact_mode();
104
105     $form['system_compact_link'] = [
106       '#id' => FALSE,
107       '#type' => 'system_compact_link',
108     ];
109
110     $form['permissions'] = [
111       '#type' => 'table',
112       '#header' => [$this->t('Permission')],
113       '#id' => 'permissions',
114       '#attributes' => ['class' => ['permissions', 'js-permissions']],
115       '#sticky' => TRUE,
116     ];
117     foreach ($role_names as $name) {
118       $form['permissions']['#header'][] = [
119         'data' => $name,
120         'class' => ['checkbox'],
121       ];
122     }
123
124     $permissions = $this->permissionHandler->getPermissions();
125     $permissions_by_provider = [];
126     foreach ($permissions as $permission_name => $permission) {
127       $permissions_by_provider[$permission['provider']][$permission_name] = $permission;
128     }
129
130     foreach ($permissions_by_provider as $provider => $permissions) {
131       // Module name.
132       $form['permissions'][$provider] = [
133         [
134           '#wrapper_attributes' => [
135             'colspan' => count($role_names) + 1,
136             'class' => ['module'],
137             'id' => 'module-' . $provider,
138           ],
139           '#markup' => $this->moduleHandler->getName($provider),
140         ],
141       ];
142       foreach ($permissions as $perm => $perm_item) {
143         // Fill in default values for the permission.
144         $perm_item += [
145           'description' => '',
146           'restrict access' => FALSE,
147           'warning' => !empty($perm_item['restrict access']) ? $this->t('Warning: Give to trusted roles only; this permission has security implications.') : '',
148         ];
149         $form['permissions'][$perm]['description'] = [
150           '#type' => 'inline_template',
151           '#template' => '<div class="permission"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em> {% endif %}{{ description }}</div>{% endif %}</div>',
152           '#context' => [
153             'title' => $perm_item['title'],
154           ],
155         ];
156         // Show the permission description.
157         if (!$hide_descriptions) {
158           $form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
159           $form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
160         }
161         foreach ($role_names as $rid => $name) {
162           $form['permissions'][$perm][$rid] = [
163             '#title' => $name . ': ' . $perm_item['title'],
164             '#title_display' => 'invisible',
165             '#wrapper_attributes' => [
166               'class' => ['checkbox'],
167             ],
168             '#type' => 'checkbox',
169             '#default_value' => in_array($perm, $role_permissions[$rid]) ? 1 : 0,
170             '#attributes' => ['class' => ['rid-' . $rid, 'js-rid-' . $rid]],
171             '#parents' => [$rid, $perm],
172           ];
173           // Show a column of disabled but checked checkboxes.
174           if ($admin_roles[$rid]) {
175             $form['permissions'][$perm][$rid]['#disabled'] = TRUE;
176             $form['permissions'][$perm][$rid]['#default_value'] = TRUE;
177           }
178         }
179       }
180     }
181
182     $form['actions'] = ['#type' => 'actions'];
183     $form['actions']['submit'] = [
184       '#type' => 'submit',
185       '#value' => $this->t('Save permissions'),
186       '#button_type' => 'primary',
187     ];
188
189     $form['#attached']['library'][] = 'user/drupal.user.permissions';
190
191     return $form;
192   }
193
194   /**
195    * {@inheritdoc}
196    */
197   public function submitForm(array &$form, FormStateInterface $form_state) {
198     foreach ($form_state->getValue('role_names') as $role_name => $name) {
199       user_role_change_permissions($role_name, (array) $form_state->getValue($role_name));
200     }
201
202     drupal_set_message($this->t('The changes have been saved.'));
203   }
204
205 }