PermissionsForm.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Drupal\filter_perms\Form;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  6. use Drupal\user\Form\UserPermissionsForm;
  7. use Drupal\user\PermissionHandlerInterface;
  8. use Drupal\user\RoleStorageInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Provides an enhanced user permissions administration form.
  12. */
  13. class PermissionsForm extends UserPermissionsForm {
  14. /**
  15. * Indicates that all options should be user for filter.
  16. */
  17. const ALL_OPTIONS = '-1';
  18. /**
  19. * The expirable key value store.
  20. *
  21. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  22. */
  23. protected $keyValueExpirable;
  24. /**
  25. * Constructs a new PermissionsForm.
  26. *
  27. * @param \Drupal\user\PermissionHandlerInterface $permission_handler
  28. * The permission handler.
  29. * @param \Drupal\user\RoleStorageInterface $role_storage
  30. * The role storage.
  31. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  32. * The module handler.
  33. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
  34. * The key value expirable factory.
  35. */
  36. public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, KeyValueStoreExpirableInterface $key_value_expirable) {
  37. parent::__construct($permission_handler, $role_storage, $module_handler);
  38. $this->keyValueExpirable = $key_value_expirable;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function create(ContainerInterface $container) {
  44. return new static(
  45. $container->get('user.permissions'),
  46. $container->get('entity.manager')->getStorage('user_role'),
  47. $container->get('module_handler'),
  48. $container->get('keyvalue.expirable')->get('filter_perms_list')
  49. );
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function buildForm(array $form, FormStateInterface $form_state) {
  55. // Render role/permission overview:
  56. $hide_descriptions = system_admin_compact_mode();
  57. $form['system_compact_link'] = array(
  58. '#id' => FALSE,
  59. '#type' => 'system_compact_link',
  60. );
  61. $permissions = $this->permissionHandler->getPermissions();
  62. $providers = array();
  63. foreach ($permissions as $permission) {
  64. $providers[$permission['provider']] = $permission['provider'];
  65. }
  66. $roles = $this->getRoles();
  67. $defined_roles = array();
  68. foreach ($roles as $role_name => $role) {
  69. $defined_roles[$role_name] = $role->label();
  70. }
  71. $filter = $this->getFilterSettings();
  72. $form['filters'] = array(
  73. '#type' => 'details',
  74. '#title' => $this->t('Permission Filters'),
  75. '#open' => TRUE,
  76. );
  77. $form['filters']['container'] = array(
  78. '#type' => 'container',
  79. '#attributes' => array('class' => array('form--inline', 'clearfix')),
  80. );
  81. // Displays all user roles.
  82. $form['filters']['container']['roles'] = array(
  83. '#title' => $this->t('Roles to display'),
  84. '#type' => 'select',
  85. '#options' => array(self::ALL_OPTIONS => '--All Roles') + $defined_roles,
  86. '#default_value' => $filter['roles'],
  87. '#size' => 8,
  88. '#multiple' => TRUE,
  89. );
  90. // Displays all modules which define permissions.
  91. $form['filters']['container']['modules'] = array(
  92. '#title' => $this->t('Modules to display'),
  93. '#type' => 'select',
  94. '#options' => array(self::ALL_OPTIONS => '--All Modules') + $providers,
  95. '#default_value' => $filter['modules'],
  96. '#size' => 8,
  97. '#multiple' => TRUE,
  98. );
  99. $form['filters']['action'] = array('#type' => 'actions');
  100. $form['filters']['action']['submit'] = array(
  101. '#type' => 'submit',
  102. '#value' => $this->t('Filter Permissions'),
  103. '#submit' => array('::submitFormFilter'),
  104. );
  105. $role_names = $role_permissions = $admin_roles = array();
  106. foreach ($roles as $role_name => $role) {
  107. if (in_array(self::ALL_OPTIONS, $filter['roles']) || in_array($role_name, $filter['roles'])) {
  108. // Retrieve role names for columns.
  109. $role_names[$role_name] = $role->label();
  110. // Fetch permissions for the roles.
  111. $role_permissions[$role_name] = $role->getPermissions();
  112. $admin_roles[$role_name] = $role->isAdmin();
  113. }
  114. }
  115. // Store $role_names for use when saving the data.
  116. $form['role_names'] = array(
  117. '#type' => 'value',
  118. '#value' => $role_names,
  119. );
  120. $permissions_by_provider = array();
  121. foreach ($permissions as $permission_name => $permission) {
  122. if (in_array(self::ALL_OPTIONS, $filter['modules']) || in_array($permission['provider'], $filter['modules'])) {
  123. $permissions_by_provider[$permission['provider']][$permission_name] = $permission;
  124. }
  125. }
  126. $form['permissions'] = array(
  127. '#type' => 'table',
  128. '#header' => array($this->t('Permission')),
  129. '#id' => 'permissions',
  130. '#attributes' => ['class' => ['permissions', 'js-permissions']],
  131. '#sticky' => TRUE,
  132. '#empty' => $this->t('Please select at least one value from both the Roles and Modules select boxes above and then click the "Filter Permissions" button.'),
  133. );
  134. // Only build the rest of the form if there are any filter settings.
  135. if (empty($role_names) || empty($permissions_by_provider)) {
  136. return $form;
  137. }
  138. foreach ($role_names as $name) {
  139. $form['permissions']['#header'][] = array(
  140. 'data' => $name,
  141. 'class' => array('checkbox'),
  142. );
  143. }
  144. foreach ($permissions_by_provider as $provider => $permissions) {
  145. // Module name.
  146. $form['permissions'][$provider] = array(array(
  147. '#wrapper_attributes' => array(
  148. 'colspan' => count($role_names) + 1,
  149. 'class' => array('module'),
  150. 'id' => 'module-' . $provider,
  151. ),
  152. '#markup' => $this->moduleHandler->getName($provider),
  153. ));
  154. foreach ($permissions as $perm => $perm_item) {
  155. // Fill in default values for the permission.
  156. $perm_item += array(
  157. 'description' => '',
  158. 'restrict access' => FALSE,
  159. 'warning' => !empty($perm_item['restrict access']) ? $this->t('Warning: Give to trusted roles only; this permission has security implications.') : '',
  160. );
  161. $form['permissions'][$perm]['description'] = array(
  162. '#type' => 'inline_template',
  163. '#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>',
  164. '#context' => array(
  165. 'title' => $perm_item['title'],
  166. ),
  167. );
  168. // Show the permission description.
  169. if (!$hide_descriptions) {
  170. $form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
  171. $form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
  172. }
  173. foreach ($role_names as $rid => $name) {
  174. $form['permissions'][$perm][$rid] = array(
  175. '#title' => $name . ': ' . $perm_item['title'],
  176. '#title_display' => 'invisible',
  177. '#wrapper_attributes' => array(
  178. 'class' => array('checkbox'),
  179. ),
  180. '#type' => 'checkbox',
  181. '#default_value' => in_array($perm, $role_permissions[$rid]) ? 1 : 0,
  182. '#attributes' => array('class' => array('rid-' . $rid, 'js-rid-' . $rid)),
  183. '#parents' => array($rid, $perm),
  184. );
  185. // Show a column of disabled but checked checkboxes.
  186. if ($admin_roles[$rid]) {
  187. $form['permissions'][$perm][$rid]['#disabled'] = TRUE;
  188. $form['permissions'][$perm][$rid]['#default_value'] = TRUE;
  189. }
  190. }
  191. }
  192. }
  193. $form['actions'] = array('#type' => 'actions');
  194. $form['actions']['submit'] = array(
  195. '#type' => 'submit',
  196. '#value' => $this->t('Save permissions'),
  197. '#button_type' => 'primary',
  198. );
  199. $form['#attached']['library'][] = 'user/drupal.user.permissions';
  200. return $form;
  201. }
  202. /**
  203. * Saves the roles and modules selection.
  204. */
  205. public function submitFormFilter(array &$form, FormStateInterface $form_state) {
  206. $this->saveFilterSettings($form_state->getValue('roles'), $form_state->getValue('modules'));
  207. }
  208. /**
  209. * Saves the filter settings for the current user.
  210. *
  211. * @param array $roles
  212. * The roles to filter by.
  213. * @param array $modules
  214. * The modules to filter by.
  215. */
  216. protected function saveFilterSettings(array $roles, array $modules) {
  217. $values = array('roles' => $roles, 'modules' => $modules);
  218. $this->keyValueExpirable->setWithExpire($this->currentUser()->id(), $values, 3600);
  219. }
  220. /**
  221. * Retrieve the filter settings for the current user.
  222. *
  223. * @return array
  224. * The filter setting for the current user.
  225. */
  226. protected function getFilterSettings() {
  227. $default = array('roles' => array(), 'modules' => array());
  228. return $this->keyValueExpirable->get($this->currentUser()->id(), $default);
  229. }
  230. }