View source
<?php
namespace Drupal\pfm\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Form\UserPermissionsForm;
class PfmUserPermissionsForm extends UserPermissionsForm {
public function getFormId() {
return 'pfm_user_admin_permissions';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
$form['#prefix'] = '<div id="pfm-user-admin-permissions-wrapper">';
$form['#suffix'] = '</div>';
$form['permissions_filter'] = [
'#type' => 'fieldset',
'#title' => 'Permissions Filters',
'#weight' => -10,
'#attributes' => [
'class' => [
'form--inline',
'clearfix',
],
],
];
$role_names = [];
$role_permissions = [];
$admin_roles = [];
foreach ($this
->getRoles() as $role_name => $role) {
$role_names[$role_name] = $role
->label();
$role_permissions[$role_name] = $role
->getPermissions();
$admin_roles[$role_name] = $role
->isAdmin();
}
$roles_options = [
'all' => 'All Roles',
] + $role_names;
$form['permissions_filter']['roles'] = [
'#type' => 'select',
'#options' => $roles_options,
'#title' => $this
->t('Select Role'),
'#multiple' => TRUE,
'#default_value' => 'all',
'#weight' => -8,
'#ajax' => [
'callback' => [
$this,
'ajaxRefresh',
],
'wrapper' => 'pfm-user-admin-permissions-wrapper',
'progress' => [
'type' => 'fullscreen',
],
],
];
$form['role_names'] = [
'#type' => 'value',
'#value' => $role_names,
];
$form['permissions'] = [
'#type' => 'table',
'#header' => [
$this
->t('Permission'),
],
'#id' => 'permissions',
'#attributes' => [
'class' => [
'permissions',
'js-permissions',
],
],
'#sticky' => TRUE,
'#empty' => $this
->t('Please choose at least one module.'),
];
$selected_roles = isset($input['roles']) ? $input['roles'] : [];
$selected_roles = array_intersect($selected_roles, array_keys($roles_options));
foreach ($role_names as $role_id => $name) {
if (in_array('all', $selected_roles) || in_array($role_id, $selected_roles)) {
$form['permissions']['#header'][] = [
'data' => $name,
'class' => [
'checkbox',
],
];
}
}
$permissions = $this->permissionHandler
->getPermissions();
$providers = $permissions_by_provider = [];
foreach ($permissions as $permission_name => $permission) {
$permissions_by_provider[$permission['provider']][$permission_name] = $permission;
$providers[] = $permission['provider'];
}
$providers = array_unique($providers);
$modules_list = array_keys($this->moduleHandler
->getModuleList());
$modules_list = array_intersect($modules_list, $providers);
$modules_options = array_map(function ($module_name) {
return $this->moduleHandler
->getName($module_name);
}, array_combine($modules_list, $modules_list));
natcasesort($modules_options);
$selected_modules = isset($input['modules']) ? $input['modules'] : [];
$selected_modules = array_intersect($selected_modules, $modules_list);
$form['permissions_filter']['modules'] = [
'#type' => 'select',
'#options' => $modules_options,
'#title' => $this
->t('Select Modules'),
'#multiple' => TRUE,
'#weight' => -9,
'#ajax' => [
'callback' => [
$this,
'ajaxRefresh',
],
'wrapper' => 'pfm-user-admin-permissions-wrapper',
'progress' => [
'type' => 'fullscreen',
],
],
];
if (empty($selected_modules)) {
return $form;
}
if ($this->moduleHandler
->moduleExists('node')) {
$keys = array_keys($permissions_by_provider['node']);
$offset = (int) array_search('view own unpublished content', $keys);
$permissions_by_provider['node'] = array_merge(array_slice($permissions_by_provider['node'], 0, $offset), [
'access content' => $permissions_by_provider['system']['access content'],
], array_slice($permissions_by_provider['node'], $offset));
unset($permissions_by_provider['system']['access content']);
}
$table_build_info = [
$permissions_by_provider,
$role_names,
$role_permissions,
$admin_roles,
$selected_modules,
$selected_roles,
];
$this
->fillPermissionTable($form, $form_state, $table_build_info);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save permissions'),
'#button_type' => 'primary',
];
$form['#attached']['library'][] = 'user/drupal.user.permissions';
if ($this->moduleHandler
->moduleExists('permissions_dragcheck')) {
$form['#attached']['library'][] = 'permissions_dragcheck/drag-check-js';
$form['#attached']['library'][] = 'permissions_dragcheck/permissions-drag-check';
}
return $form;
}
protected function fillPermissionTable(array &$form, FormStateInterface &$form_state, array $table_build_info) {
list($permissions_by_provider, $role_names, $role_permissions, $admin_roles, $selected_modules, $selected_roles, ) = $table_build_info;
foreach ($permissions_by_provider as $provider => $permissions) {
if (!in_array($provider, $selected_modules)) {
continue;
}
$form['permissions'][$provider] = [
[
'#wrapper_attributes' => [
'colspan' => count($role_names) + 1,
'class' => [
'module',
],
'id' => 'module-' . $provider,
],
'#markup' => $this->moduleHandler
->getName($provider),
],
];
foreach ($permissions as $perm => $perm_item) {
$perm_item += [
'description' => '',
'restrict access' => FALSE,
'warning' => !empty($perm_item['restrict access']) ? $this
->t('Warning: Give to trusted roles only; this permission has security implications.') : '',
];
$form['permissions'][$perm]['description'] = [
'#type' => 'inline_template',
'#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>',
'#context' => [
'title' => $perm_item['title'],
],
];
$form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
$form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
foreach ($role_names as $rid => $name) {
if (in_array('all', $selected_roles) || in_array($rid, $selected_roles)) {
$checked = in_array($perm, $role_permissions[$rid]) ? TRUE : FALSE;
$form['permissions'][$perm][$rid] = [
'#title' => $name . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
'#type' => 'checkbox',
'#default_value' => $checked,
'#attributes' => [
'class' => [
'rid-' . $rid,
'js-rid-' . $rid,
],
'checked' => $checked,
],
'#parents' => [
$rid,
$perm,
],
];
if ($admin_roles[$rid]) {
$form['permissions'][$perm][$rid]['#disabled'] = TRUE;
$form['permissions'][$perm][$rid]['#default_value'] = TRUE;
}
}
}
}
}
}
public function ajaxRefresh(&$form, FormStateInterface $form_state) {
return $form;
}
}