Deprecate redundant (role-/permission related) procedural wrappers from user.module

Created on 21 June 2013, about 12 years ago
Updated 14 March 2023, over 2 years ago

user.module has quite a few left-over helper functions from pre-config-entity days... Once #1872876: Turn role permission assignments into configuration. β†’ is resolved we can start to clean up user.module by removing those functions as RoleInterface will get a few nice methods for managing permissions. Also, things like user_role_names() really aren't required. That's 3 lines of code in a closure, same with loading or permission checks. We should change all the places (e.g. permission overview, etc.) to work with the full Role entity objects and use the methods that the RoleInterface exposes.

Here is a list of role/permission related functions we should remove.

  • user_role_permissions()
  • user_roles()
  • user_role_names()
  • user_role_change_permissions()
  • user_role_grant_permissions()
  • user_role_revoke_permissions()
πŸ“Œ Task
Status

Active

Version

10.1 ✨

Component
User moduleΒ  β†’

Last updated 12 days ago

Created by

πŸ‡¦πŸ‡ΉAustria fubhy

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Let's use the deprecation process here - more important due to things like πŸ› Role permissions are not sorted when saving via admin/people/permissions Fixed

  • Status changed to Needs review over 2 years ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
  • @alexpott opened merge request.
  • πŸ‡«πŸ‡·France andypost

    Do we really need changePermissions() not much useful API

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Role::changePermissions() is used 3 three time in run-time code in core. I've replaced all the obvious ones in tests in this issue - where the test was only granting but using this method. I think there is likely to be quite a lot of contrib usages - see https://git.drupalcode.org/search?group_id=2&repository_ref=&scope=blobs... - but given they'll need to change at this point I think it could be a good idea. I think what we should do is make this issue only about user_role_grant_permissions() and user_role_revoke_permissions(). I think we should still change any test usage of user_role_change_permissions() that we can because it is harmless.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Tighter issue scope.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I feel that we should tackle πŸ“Œ Deprecate the trusted data concept in configuration Active first as it will ensure that we don't need to give complex instructions about when to call trustData()

  • πŸ‡«πŸ‡·France andypost

    It could use follow-up to replace most of calls to grantPermission() with grantPermissions()

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @andypost

    It could use follow-up to replace most of calls to grantPermission() with grantPermissions()

    Why? I think it is okay to have both. I toyed with making grantPermission accept an array but that looked awkward.

  • πŸ‡«πŸ‡·France andypost

    @alexpott because many places doing chained calls which could be converted by rector to new method which accepts arrays

  • πŸ‡«πŸ‡·France andypost

    btw Maybe it could just extend existing methods with typed variable arguments
    grantPermissions(string ...$permissions): static or grantPermissions(string $first, string ...$permissions): static

    ref https://www.php.net/manual/en/functions.arguments.php#functions.variable...

  • @andypost opened merge request.
  • πŸ‡«πŸ‡·France andypost

    @alexpot I reverted some changes as in tests roles often has no permissions but grant/revoke without any permission sounds like bad API

    interdiff in new MR's commit https://git.drupalcode.org/project/drupal/-/merge_requests/3666/diffs?co...

  • πŸ‡«πŸ‡·France andypost

    approach with typed variadic also allows to keep array values as strings https://3v4l.org/49RgT

  • πŸ‡«πŸ‡·France andypost

    fixed IS

  • Status changed to Needs work over 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    All occurrences appear to be replaced

    Think this will need a deprecation test though.

  • πŸ‡«πŸ‡·France andypost

    and needs to extend testing for the methods with more values

  • Status changed to Needs review over 2 years ago
  • πŸ‡«πŸ‡·France andypost

    Added test, hope it covers both old and new usage

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @andypost 'm saddened that you removed the grantPermissions() method. We discussed it on issue and disagree. A method called grantPermission() accepting more that one permission is not great and there is no reason to not add another method to keep the syntax meaningful. Also changing to a variadic is an API change not an addition which is harder for alternate role implementations.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @andypost what does

    but grant/revoke without any permission sounds like bad API

    mean?
    Are you saying this is bad API because you can pass an empty array in? I disagree to be honest. If you pass an empty list into grantPermissions() and it grants no permissions this is exactly how you would expect a method that accepts a list to behave.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Also @andypost

    @alexpott because many places doing chained calls which could be converted by rector to new method which accepts arrays

    What does this mean?

    I think it means that you think there are places doing...

    $role
      ->grantPermission('perm 1')
      ->grantPermission('perm 2')
      ->save();
    

    I don't see why that is an argument for a variadic...

    We can change this to

    $role
      ->grantPermissions(['perm 1', 'perm 2'])
      ->save();
    

    just fine.

  • πŸ‡«πŸ‡·France andypost

    @alexpott I mean array argument is bad api because there's no check for array values (empty array is just useless ability). That's why variadics is better approach.

    I'm ok to add extra plural method but it looks more confusing and a way to more errors (similar methods on a class). I find a case of overriding roles's entity class is very edge case comparing to API extension which is functionally backward compatible

  • πŸ‡«πŸ‡·France andypost

    Main idea is to use less function calls and make code more readable

    $role
      ->grantPermission('perm 1')
      ->grantPermission('perm 2')
      ->save();
    

    vs

    $role
      ->grantPermission('perm 1',  'perm2')
      ->save();
    
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @andypost I think

    $role
      ->grantPermissions(['perm 1',  'perm2'])
      ->save();
    

    is better than the variadic extension of the grantPermission() method.

  • πŸ‡«πŸ‡·France andypost

    @alexpott this approach is more common but it does not check that each array element is string, that's why I find variadic exactly suitable for that specific case

  • Status changed to Needs work over 2 years ago
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    I'm happy to work on this issue but I think we need clear direction as to what we want. Trying to summarise below:

    Variadic or array:
    grantPermissions(string ...$permission) or grantPermissions(array $permissions)
    As andypost pointed out using an array has no type safety. We could add @param string[] $permissions and rely on phpstan to do type-checking, but not sure it will catch everything.

    Single and plural or just plural:
    grantPermission and grantPermissions or just grantPermissions
    Keeping both is perhaps easier, changing to plural would require deprecating the singular. If we have just the plural I think variadic is better so you don't need to pass an array when granting only one permission.

Production build 0.71.5 2024