Permission like 'Use Example form mode with user entity" does not grant permission

Created on 15 July 2024, 6 months ago

Problem/Motivation

The permission like "Use Example form mode with user entity" does not work as expected. If the user cannot 'administer users' access is denied. Either we should clarify that in the description or rectify the issue.

Steps to reproduce

  1. Create a form mode for user
  2. Grant permission to that form mode to e.g. an Editor role
  3. Do not grant permission 'administer users'

Proposed resolution

  1. Grant permission to the form mode
  2. Update tests

Remaining tasks

Merge request

User interface changes

Roles with this permission will actually get access

API changes

None

Data model changes

None

Workaround

Alternatively a module can use hook entity access like this:

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function MODULE_user_access(EntityInterface $entity, $operation, AccountInterface $account) {
  $route_match = \Drupal::routeMatch();
  if ($route_match && str_starts_with($route_match->getRouteName(), 'entity.user.edit_form.')) {

    // If the user has permission to use the particular form mode edit route,
    // ignore whether they have user entity access so that the 'administer
    // users' permission does not need to be granted.
    $route_parts = explode('.', $route_match->getRouteName());
    $form_mode = end($route_parts);
    if ($account->hasPermission('use user.' . $form_mode . ' form mode')) {
      return AccessResult::allowed();
    }
  }

  // No opinion.
  return AccessResult::neutral();
}
✨ Feature request
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom scott_euser

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024