Commit d3d54f17 authored by alexpott's avatar alexpott

Issue #1938888 by tim.plunkett, ayelet_Cr, amateescu: Convert...

Issue #1938888 by tim.plunkett, ayelet_Cr, amateescu: Convert user_admin_permissions() to a new-style Form object.
parent 6a9d9d50
......@@ -207,44 +207,16 @@ function drupal_valid_path($path, $dynamic_allowed = FALSE) {
$item['options'] = '';
_menu_link_translate($item);
}
if (empty($item['access'])) {
// Nothing was found in the old routing system, so try the new one.
$item = _drupal_valid_path_new_router($path);
}
}
else {
$item = menu_get_item($path);
if (empty($item['access'])) {
// Nothing was found in the old routing system, so try the new one.
$item = _drupal_valid_path_new_router($path);
}
}
// Check the new routing system.
if (!empty($item['route_name'])) {
$map = array();
$route = Drupal::service('router.route_provider')->getRouteByName($item['route_name']);
$item['access'] = menu_item_route_access($route, $path, $map);
}
$menu_admin = FALSE;
return $item && $item['access'];
}
/**
* Temporary helper function to check a path in the new routing system.
*
* @param string $path
* The path string as expected by drupal_valid_path().
*
* @return array|NULL
* An array containing 'access' => TRUE or NULL for paths that were not found
* or the user has no access to.
*/
function _drupal_valid_path_new_router($path) {
$request = Request::create('/' . $path);
$request->attributes->set('system_path', $path);
try {
$dc = drupal_container();
$route = $dc->get('router.dynamic')->matchRequest($request);
if (!empty($route)) {
$dc->get('access_manager')->check($route['_route_object'], $request);
}
return array('access' => TRUE);
}
catch (Exception $e) {
drupal_set_message($e->getMessage(), 'menu', WATCHDOG_ERROR);
}
}
<?php
/**
* @file
* Contains \Drupal\user\Form\UserPermissionsForm.
*/
namespace Drupal\user\Form;
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\user\RoleStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the user permissions administration form.
*/
class UserPermissionsForm implements FormInterface, ControllerInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The role storage.
*
* @var \Drupal\user\RoleStorageControllerInterface
*/
protected $roleStorage;
/**
* Constructs a new UserPermissionsForm.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\user\RoleStorageControllerInterface $role_storage
* The role storage.
*/
public function __construct(ModuleHandlerInterface $module_handler, RoleStorageControllerInterface $role_storage) {
$this->moduleHandler = $module_handler;
$this->roleStorage = $role_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('plugin.manager.entity')->getStorageController('user_role')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'user_admin_permissions';
}
/**
* Gets the roles to display in this form.
*
* @return \Drupal\user\RoleInterface[]
* An array of role objects.
*/
protected function getRoles() {
return $this->roleStorage->loadMultiple();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$role_names = array();
$role_permissions = array();
foreach ($this->getRoles() as $role_name => $role) {
// Retrieve role names for columns.
$role_names[$role_name] = String::checkPlain($role->label());
// Fetch permissions for the roles.
$role_permissions[$role_name] = $role->getPermissions();
}
// Store $role_names for use when saving the data.
$form['role_names'] = array(
'#type' => 'value',
'#value' => $role_names,
);
// Render role/permission overview:
$options = array();
$module_info = system_rebuild_module_data();
$hide_descriptions = system_admin_compact_mode();
// Get a list of all the modules implementing a hook_permission() and sort by
// display name.
$modules = array();
foreach ($this->moduleHandler->getImplementations('permission') as $module) {
$modules[$module] = $module_info[$module]->info['name'];;
}
asort($modules);
$form['system_compact_link'] = array(
'#theme' => 'system_compact_link',
);
$form['permissions'] = array(
'#type' => 'table',
'#header' => array(t('Permission')),
'#id' => 'permissions',
);
foreach ($role_names as $rid => $name) {
$form['permissions']['#header'][] = array(
'data' => $name,
'class' => array('checkbox'),
);
}
foreach ($modules as $module => $display_name) {
if ($permissions = $this->moduleHandler->invoke($module, 'permission')) {
// Module name.
$form['permissions'][$module] = array(array(
'#wrapper_attributes' => array(
'colspan' => count($role_names) + 1,
'class' => array('module'),
'id' => 'module-' . $module,
),
'#markup' => $module_info[$module]->info['name'],
));
foreach ($permissions as $perm => $perm_item) {
// Fill in default values for the permission.
$perm_item += array(
'description' => '',
'restrict access' => FALSE,
'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications.') : '',
);
$options[$perm] = '';
$user_permission_description = array(
'#theme' => 'user_permission_description',
'#permission_item' => $perm_item,
'#hide' => $hide_descriptions,
);
$form['permissions'][$perm]['description'] = array(
'#wrapper_attributes' => array(
'class' => array('permission'),
),
'#type' => 'item',
'#markup' => $perm_item['title'],
'#description' => drupal_render($user_permission_description),
);
$options[$perm] = '';
foreach ($role_names as $rid => $name) {
$form['permissions'][$perm][$rid] = array(
'#title' => $name . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => array(
'class' => array('checkbox'),
),
'#type' => 'checkbox',
'#default_value' => in_array($perm,$role_permissions[$rid]) ? 1 : 0,
'#attributes' => array('class' => array('rid-' . $rid)),
'#parents' => array($rid, $perm),
);
}
}
}
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
$form['#attached']['library'][] = array('user', 'drupal.user.permissions');
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
function submitForm(array &$form, array &$form_state) {
foreach ($form_state['values']['role_names'] as $role_name => $name) {
user_role_change_permissions($role_name, $form_state['values'][$role_name]);
}
drupal_set_message(t('The changes have been saved.'));
// Clear the cached pages and blocks.
Cache::invalidateTags(array('content' => TRUE));
}
}
<?php
/**
* @file
* Contains \Drupal\user\Form\UserPermissionsRoleSpecificForm.
*/
namespace Drupal\user\Form;
/**
* Provides the user permissions administration form for a specific role.
*/
class UserPermissionsRoleSpecificForm extends UserPermissionsForm {
/**
* The specific role for this form.
*
* @var string
*/
protected $roleId;
/**
* {@inheritdoc}
*/
protected function getRoles() {
return array($this->roleId => $this->roleStorage->load($this->roleId));
}
/**
* {@inheritdoc}
*
* @param string $role_id
* The user role ID used for this form.
*/
public function buildForm(array $form, array &$form_state, $role_id = NULL) {
$this->roleId = $role_id;
return parent::buildForm($form, $form_state);
}
}
......@@ -95,130 +95,6 @@ function user_admin_account() {
return $form;
}
/**
* Menu callback: administer permissions.
*
* @ingroup forms
* @see user_admin_permissions_submit()
* @see theme_user_admin_permissions()
*/
function user_admin_permissions($form, $form_state, $rid = NULL) {
// Retrieve role names for columns.
$role_names = user_role_names();
if (isset($rid)) {
$role_names = array($rid => $role_names[$rid]);
}
// Fetch permissions for all roles or the one selected role.
$role_permissions = user_role_permissions(array_keys($role_names));
// Store $role_names for use when saving the data.
$form['role_names'] = array(
'#type' => 'value',
'#value' => $role_names,
);
// Render role/permission overview:
$options = array();
$module_info = system_get_info('module');
$hide_descriptions = system_admin_compact_mode();
// Get a list of all the modules implementing a hook_permission() and sort by
// display name.
$modules = array();
foreach (module_implements('permission') as $module) {
$modules[$module] = $module_info[$module]['name'];
}
asort($modules);
$form['system_compact_link'] = array(
'#theme' => 'system_compact_link',
);
$form['permissions'] = array(
'#type' => 'table',
'#header' => array(t('Permission')),
'#id' => 'permissions',
);
foreach ($role_names as $rid => $name) {
$form['permissions']['#header'][] = array(
'data' => $name,
'class' => array('checkbox'),
);
}
foreach ($modules as $module => $display_name) {
if ($permissions = module_invoke($module, 'permission')) {
// Module name.
$form['permissions'][$module] = array(array(
'#wrapper_attributes' => array(
'colspan' => count($role_names) + 1,
'class' => array('module'),
'id' => 'module-' . $module,
),
'#markup' => $module_info[$module]['name'],
));
foreach ($permissions as $perm => $perm_item) {
// Fill in default values for the permission.
$perm_item += array(
'description' => '',
'restrict access' => FALSE,
'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications.') : '',
);
$options[$perm] = '';
$user_permission_description = array(
'#theme' => 'user_permission_description',
'#permission_item' => $perm_item,
'#hide' => $hide_descriptions,
);
$form['permissions'][$perm]['description'] = array(
'#wrapper_attributes' => array(
'class' => array('permission'),
),
'#type' => 'item',
'#markup' => $perm_item['title'],
'#description' => drupal_render($user_permission_description),
);
$options[$perm] = '';
foreach ($role_names as $rid => $name) {
$form['permissions'][$perm][$rid] = array(
'#title' => $name . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => array(
'class' => array('checkbox'),
),
'#type' => 'checkbox',
'#default_value' => in_array($perm,$role_permissions[$rid]) ? 1 : 0,
'#attributes' => array('class' => array('rid-' . $rid)),
'#parents' => array($rid, $perm),
);
}
}
}
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
$form['#attached']['library'][] = array('user', 'drupal.user.permissions');
return $form;
}
/**
* Save permissions selected on the administer permissions page.
*
* @see user_admin_permissions()
*/
function user_admin_permissions_submit($form, &$form_state) {
foreach ($form_state['values']['role_names'] as $rid => $name) {
user_role_change_permissions($rid, $form_state['values'][$rid]);
}
drupal_set_message(t('The changes have been saved.'));
// Clear the cached pages and blocks.
cache_invalidate_tags(array('content' => TRUE));
}
/**
* Returns HTML for an individual permission description.
*
......
......@@ -858,10 +858,7 @@ function user_menu() {
$items['admin/people/permissions'] = array(
'title' => 'Permissions',
'description' => 'Determine access to features by selecting permissions for roles.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_admin_permissions'),
'access arguments' => array('administer permissions'),
'file' => 'user.admin.inc',
'route_name' => 'user_admin_permissions',
'type' => MENU_LOCAL_TASK,
);
$items['admin/people/roles'] = array(
......
......@@ -40,6 +40,20 @@ user_admin_create:
requirements:
_permission: 'administer users'
user_admin_permissions:
pattern: '/admin/people/permissions'
defaults:
_form: '\Drupal\user\Form\UserPermissionsForm'
requirements:
_permission: 'administer permissions'
user_admin_permission:
pattern: '/admin/people/permissions/{role_id}'
defaults:
_form: '\Drupal\user\Form\UserPermissionsRoleSpecificForm'
requirements:
_permission: 'administer permissions'
user_role_list:
pattern: '/admin/people/roles'
defaults:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment