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..e932e98 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Form/UserPermissionsForm.php @@ -0,0 +1,181 @@ +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); + + foreach ($modules as $module => $display_name) { + if ($permissions = $this->moduleHandler->invoke($module, 'permission')) { + $form['permission'][] = array( + '#markup' => $module_info[$module]->info['name'], + '#id' => $module, + ); + 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['permission'][$perm] = array( + '#type' => 'item', + '#markup' => $perm_item['title'], + '#description' => drupal_render($user_permission_description), + ); + foreach ($role_names as $rid => $name) { + // Builds arrays for checked boxes for each role + if (in_array($perm, $role_permissions[$rid])) { + $status[$rid][] = $perm; + } + } + } + } + } + + // Have to build checkboxes here after checkbox arrays are built + foreach ($role_names as $rid => $name) { + $form['checkboxes'][$rid] = array( + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => isset($status[$rid]) ? $status[$rid] : array(), + '#attributes' => array('class' => array('rid-' . $rid)), + ); + $form['role_names'][$rid] = array('#markup' => $name, '#tree' => TRUE); + } + + $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/UserPermissionsSpecificForm.php b/core/modules/user/lib/Drupal/user/Form/UserPermissionsSpecificForm.php new file mode 100644 index 0000000..efc5be4 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Form/UserPermissionsSpecificForm.php @@ -0,0 +1,37 @@ +roleId => $this->roleStorage->load($this->roleId)); + } + + /** + * {@inheritdoc} + */ + 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 bb486ff..e95872f 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -97,109 +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); - - foreach ($modules as $module => $display_name) { - if ($permissions = module_invoke($module, 'permission')) { - $form['permission'][] = array( - '#markup' => $module_info[$module]['name'], - '#id' => $module, - ); - 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['permission'][$perm] = array( - '#type' => 'item', - '#markup' => $perm_item['title'], - '#description' => drupal_render($user_permission_description), - ); - foreach ($role_names as $rid => $name) { - // Builds arrays for checked boxes for each role - if (in_array($perm, $role_permissions[$rid])) { - $status[$rid][] = $perm; - } - } - } - } - } - - // Have to build checkboxes here after checkbox arrays are built - foreach ($role_names as $rid => $name) { - $form['checkboxes'][$rid] = array( - '#type' => 'checkboxes', - '#options' => $options, - '#default_value' => isset($status[$rid]) ? $status[$rid] : array(), - '#attributes' => array('class' => array('rid-' . $rid)), - ); - $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE); - } - - $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 the administer permissions page. * * @param $variables diff --git a/core/modules/user/user.module b/core/modules/user/user.module index cb1ab7e..ea456fa 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -883,10 +883,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..5c88ed8 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\UserPermissionsSpecificForm' + requirements: + _permission: 'administer permissions' + user_role_list: pattern: '/admin/people/roles' defaults: