diff --git a/core/includes/path.inc b/core/includes/path.inc index 911cfe0..d17f58f 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -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); - } -} diff --git a/core/modules/user/lib/Drupal/user/Form/UserPermissionsForm.php b/core/modules/user/lib/Drupal/user/Form/UserPermissionsForm.php new file mode 100644 index 0000000..b3c28cc --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Form/UserPermissionsForm.php @@ -0,0 +1,202 @@ +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)); + } + +} diff --git a/core/modules/user/lib/Drupal/user/Form/UserPermissionsRoleSpecificForm.php b/core/modules/user/lib/Drupal/user/Form/UserPermissionsRoleSpecificForm.php new file mode 100644 index 0000000..19d5dce --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Form/UserPermissionsRoleSpecificForm.php @@ -0,0 +1,40 @@ +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); + } + +} diff --git a/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc index 58063e8..c8e4fd7 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -97,130 +97,6 @@ function user_admin_account() { } /** - * 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. * * @param $variables diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 924b221..1a8c53c 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -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( diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 10ec95d..90570ea 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -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: