Problem/Motivation

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.

Proposed resolution

This issue will deprecate:

  • user_role_revoke_permissions()
  • user_role_grant_permissions()
  • user_role_change_permissions

RoleInterface has three new methods to replace the functions.

  • grantPermissions
  • changePermissions
  • revokePermissions

Remaining tasks

Review

User interface changes

N/A

API changes

RoleInterface has three new methods to replace the functions.

  • grantPermissions
  • changePermissions
  • revokePermissions

Data model changes

N/A

Release notes snippet

N/A

CommentFileSizeAuthor
#45 2025089-nr-bot.txt90 bytesneeds-review-queue-bot

Issue fork drupal-2025089

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andypost’s picture

Awesome clean-up suppose better finish #1872876: Turn role permission assignments into configuration. first
Then split this issue into parts

  • user_role_permissions() - 3
  • user_roles() - 7
  • user_role_names() - 18
  • user_permission_get_modules() - 1
  • user_role_change_permissions() - 20 (18 + 2)
  • user_role_grant_permissions() - 36 (34 +2)
  • user_role_revoke_permissions() - 8 (6 + 2)
andypost’s picture

Is there any suggestions on method names for RoleInterface?
Suppose some work could be done in parallel

fubhy’s picture

I already implemented all method names needed for this removal over at #1872876: Turn role permission assignments into configuration.. Let's wait for that to get in. :)

fubhy’s picture

user_permission_get_modules() already get's removed in #1872876: Turn role permission assignments into configuration.. I am not sure if we really have to split this issue up (We don't have that many occurrences of those functions left) but if we do, let's split it into two:

  • user_role_permissions() - 3
  • user_roles() - 7
  • user_role_names() - 18

and

  • user_role_change_permissions() - 20 (18 + 2)
  • user_role_grant_permissions() - 36 (34 +2)
  • user_role_revoke_permissions() - 8 (6 + 2)

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.1.x-dev » 8.3.x-dev
Issue summary: View changes

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Title: Remove redundant (role-/permission related) procedural wrappers from user.module » Deprecate redundant (role-/permission related) procedural wrappers from user.module
Related issues: +#3346953: Role permissions are not sorted when saving via admin/people/permissions

Let's use the deprecation process here - more important due to things like #3346953: Role permissions are not sorted when saving via admin/people/permissions

alexpott’s picture

Status: Active » Needs review

andypost’s picture

Do we really need changePermissions() not much useful API

alexpott’s picture

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.

alexpott’s picture

Title: Deprecate redundant (role-/permission related) procedural wrappers from user.module » Deprecate user_role_grant_permissions() and user_role_revoke_permissions()
Issue summary: View changes

Tighter issue scope.

alexpott’s picture

Issue summary: View changes
alexpott’s picture

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

andypost’s picture

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

alexpott’s picture

@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.

andypost’s picture

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

andypost’s picture

btw Maybe it could just extend existing methods with typed variable arguments
grantPermissions(string ...$permissions): static or grantPermission(string $permission, ?string ...$permissions): static https://3v4l.org/O85Hh

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

andypost’s picture

@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...

andypost’s picture

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

andypost’s picture

Issue summary: View changes

fixed IS

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

All occurrences appear to be replaced

Think this will need a deprecation test though.

andypost’s picture

and needs to extend testing for the methods with more values

andypost’s picture

Status: Needs work » Needs review

Added test, hope it covers both old and new usage

alexpott’s picture

@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.

alexpott’s picture

@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.

alexpott’s picture

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.

andypost’s picture

@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

andypost’s picture

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();
alexpott’s picture

@andypost I think

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

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

andypost’s picture

@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

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan’s picture

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

nod_’s picture

Updated search link for #23: user_role_change_permissions

claudiu.cristea’s picture

nicxvan’s picture

Issue summary: View changes

I looked at both approaches and I think Alex's approach is much cleaner.

I do understand @andyposts' point, but I think at this point we should just follow Alex, and open a follow up to decide what to do about the singular versions.

I'm going to add the change permissions function too.

I tried applying, but there are dozens of conflicts, this doesn't look to hard to manually reroll though.

nicxvan’s picture

Assigned: Unassigned » nicxvan

I'm about 90% of the way through, I'd like to finish in the morning so assigning to myself.

nicxvan’s picture

There is one remaining test failure, I think this is a side effect of something else going on.

core/modules/config_translation/tests/src/Functional/ConfigTranslationRoleUiTest.php
Is failing because the text Authenticated user doesn't exist which was added here: https://www.drupal.org/project/drupal/issues/3582050

I think the case the test is looking at is still an issue, the main issue is here #2910353: Prevent saving config entities when configuration overrides are applied which is still needs work.

I'm not sure if we just remove that part of the test and add a todo on the actual issue fixing it.

You can reproduce the issue with these steps:

  1. Install drupal
  2. Enable configuration translation
  3. Visit admin/people/roles/manage/authenticated/translate
  4. Add de as a language
  5. Visit admin/people/roles/manage/authenticated/translate/de/add
  6. Add the translation Angemeldeter Benutzer
  7. Visit de/admin/people/permissions
  8. Add change own username permission to Authenticated user role
  9. Visit admin/people/roles
  10. See the role is German
nicxvan’s picture

Title: Deprecate user_role_grant_permissions() and user_role_revoke_permissions() » Deprecate user_role_grant_permissions(), user_role_revoke_permissions() and user_role_change_permissions()
nicxvan’s picture

Status: Needs work » Needs review

I addressed the feedback.

The failure is random.
I looked through and think the static call is likely fine in most cases.

berdir’s picture

Status: Needs review » Needs work

Reviewed.

Considering how many calls we to these methods in tests, I could also see us adding some helpers to UserCreationTest, we alread have \Drupal\Tests\user\Traits\UserCreationTrait::grantPermissions for example. but I'm also fine not doing so, it would essentially just save us the save() call.

nicxvan’s picture

Status: Needs work » Needs review

Rebased after the loadOverrideFree issue landed

berdir’s picture

Status: Needs review » Needs work

Almost ready, just some minor things. And the CR is a placeholder and the IS could use an update too.

Went through it with a word diff, nothing changes around the list of permissions, just how it's called.

Personally, if I'd started this, I probably would have added a trait to UserCreationTrait or so, the vast majority of calls are in tests, and it would make it slightly more convenient, but at least for core, the work is done now and while there are 70ish calls, the replacement isn't that complicated, now that loadOverrideFree() exists.

contrib has 700 calls, but I think this should be easy to automate with rector: https://search.tresbien.tech/search?q=user_role_.*_permissions%20-r%3Adr...

nicxvan’s picture

Issue summary: View changes
nicxvan’s picture

Status: Needs work » Needs review

This should be ready for review again!

dcam’s picture

Status: Needs review » Needs work

I reviewed the whole MR and found an incorrect conversion. I left a comment on the MR. Please check it.

nicxvan’s picture

Status: Needs work » Needs review

@dcam thanks!

I went with @berdir's suggestion since I agreed it makes it easier to review.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

That's fine. I also appreciate when changes are minimized in large diffs. It does make those MRs easier to review.

It sounds like this one is ready for committers.

amateescu’s picture

Status: Reviewed & tested by the community » Needs review

Posted a few comments on the MR, should be fine to self-rtbc after they're fixed.

nicxvan’s picture

I applied most of your suggestions.

I updated the install files too.

I created a follow up for the trait: #3621087: Update UserCreationTrait::grantPermissions() to use new RoleInterface::grantPermissions

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

The failure is random builds/core/modules/system/tests/src/FunctionalJavascript/ThemeSettingsFormTest.php:85

Hopefully the follow up is OK!

  • amateescu committed 7065b237 on 11.x
    task: #2025089 Deprecate user_role_grant_permissions(),...

  • amateescu committed 0f7caab8 on main
    task: #2025089 Deprecate user_role_grant_permissions(),...
amateescu’s picture

Version: main » 11.x-dev
Assigned: nicxvan » Unassigned
Status: Reviewed & tested by the community » Fixed

Committed and pushed 0f7caab8579 to main and 7065b2372a3 to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.