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
| Comment | File | Size | Author |
|---|
Issue fork drupal-2025089
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
Comment #1
andypostAwesome clean-up suppose better finish #1872876: Turn role permission assignments into configuration. first
Then split this issue into parts
Comment #2
andypostIs there any suggestions on method names for RoleInterface?
Suppose some work could be done in parallel
Comment #3
fubhy commentedI 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. :)
Comment #4
fubhy commenteduser_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:and
Comment #6
andypostComment #19
alexpottLet's use the deprecation process here - more important due to things like #3346953: Role permissions are not sorted when saving via admin/people/permissions
Comment #20
alexpottComment #22
andypostDo we really need
changePermissions()not much useful APIComment #23
alexpottRole::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.
Comment #24
alexpottTighter issue scope.
Comment #25
alexpottComment #26
alexpottI 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()
Comment #27
andypostIt could use follow-up to replace most of calls to
grantPermission()withgrantPermissions()Comment #28
alexpott@andypost
Why? I think it is okay to have both. I toyed with making grantPermission accept an array but that looked awkward.
Comment #29
andypost@alexpott because many places doing chained calls which could be converted by rector to new method which accepts arrays
Comment #30
andypostbtw Maybe it could just extend existing methods with typed variable arguments
grantPermissions(string ...$permissions): staticorgrantPermission(string $permission, ?string ...$permissions): statichttps://3v4l.org/O85Hhref https://www.php.net/manual/en/functions.arguments.php#functions.variable...
Comment #32
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...
Comment #33
andypostapproach with typed variadic also allows to keep array values as strings https://3v4l.org/49RgT
Comment #34
andypostfixed IS
Comment #35
smustgrave commentedAll occurrences appear to be replaced
Think this will need a deprecation test though.
Comment #36
andypostand needs to extend testing for the methods with more values
Comment #37
andypostAdded test, hope it covers both old and new usage
Comment #38
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.
Comment #39
alexpott@andypost what does
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.
Comment #40
alexpottAlso @andypost
What does this mean?
I think it means that you think there are places doing...
I don't see why that is an argument for a variadic...
We can change this to
just fine.
Comment #41
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
Comment #42
andypostMain idea is to use less function calls and make code more readable
vs
Comment #43
alexpott@andypost I think
is better than the variadic extension of the grantPermission() method.
Comment #44
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
Comment #45
needs-review-queue-bot commentedThe 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.
Comment #47
mstrelan commentedI'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)orgrantPermissions(array $permissions)As andypost pointed out using an array has no type safety. We could add
@param string[] $permissionsand rely on phpstan to do type-checking, but not sure it will catch everything.Single and plural or just plural:
grantPermissionandgrantPermissionsor justgrantPermissionsKeeping 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.
Comment #49
nod_Updated search link for #23: user_role_change_permissions
Comment #50
claudiu.cristeaComment #51
nicxvan commentedI 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.
Comment #52
nicxvan commentedI'm about 90% of the way through, I'd like to finish in the morning so assigning to myself.
Comment #54
nicxvan commentedThere 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.phpIs 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:
Comment #55
nicxvan commentedComment #56
nicxvan commentedI addressed the feedback.
The failure is random.
I looked through and think the static call is likely fine in most cases.
Comment #57
berdirReviewed.
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.
Comment #58
nicxvan commented#3620216: Add loadOverrideFree and loadMultipleOverrideFree to ConfigEntityBase is ready for review!
Comment #59
nicxvan commentedRebased after the loadOverrideFree issue landed
Comment #60
berdirAlmost 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...
Comment #61
nicxvan commentedComment #62
nicxvan commentedThis should be ready for review again!
Comment #63
dcam commentedI reviewed the whole MR and found an incorrect conversion. I left a comment on the MR. Please check it.
Comment #64
nicxvan commented@dcam thanks!
I went with @berdir's suggestion since I agreed it makes it easier to review.
Comment #65
dcam commentedThat'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.
Comment #66
amateescu commentedPosted a few comments on the MR, should be fine to self-rtbc after they're fixed.
Comment #67
nicxvan commentedI 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
Comment #68
nicxvan commentedThe failure is random builds/core/modules/system/tests/src/FunctionalJavascript/ThemeSettingsFormTest.php:85
Hopefully the follow up is OK!
Comment #71
amateescu commentedCommitted and pushed 0f7caab8579 to main and 7065b2372a3 to 11.x. Thanks!