Problem/Motivation
In #2991232: Add hasRole() method to AccountProxy and UserSession classes we added hasRole() implementation to AccountProxy.php and UserSession.php classes. The AccountInterface remained unchanged to keep it compatible with existing contrib and custom AccountInterface implementations.
Proposed resolution
Move method hasRole() definition to AccountInterface.
Remaining tasks
Wait for 10.x branch- Wait for #2991232: Add hasRole() method to AccountProxy and UserSession classes to land
Do the changeOpen MRDraft CR: https://www.drupal.org/node/3283218Make sure all tests are still happy- Reviews / refinements
- RTBC
API changes
AccountInterface has been extended with a new method - AccountInterface::hasRole().
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | 3228209-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-3228209
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 #2
tr commentedComment #5
dwwGreat idea! Let's see what breaks. 😅
Comment #6
dwwI guess this is the more accurate title...
Comment #7
dwwComment #8
dwwOh hah! I somehow thought #2991232: Add hasRole() method to AccountProxy and UserSession classes had already landed. Tee hee. This issue isn't blocked on 10.0.x branch, it's blocked on 2991232. Updated remaining tasks accordingly.
Comment #9
alexpottIn this issue we should update \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext to use the method.
Comment #10
alexpottThis is now postponed on the Drupal 11 version being selectable.
Comment #11
berdirThe blocker landed now.
Comment #12
alexpott@Berdir I was leaving this postponed until we had a D11 version selector... guess we can use the title for that :(
Comment #13
berdirI'm a bit confused about that part. We can't just add a new method in D11 to an existing interface? We need to prepare for that in 10.x, trigger a deprecation if it's not there and so on?
I don't get the the split between the issue that just got in and this. I did expect the other issue to do that and this one to then enforce it. But the other issues just added two standalone methods that are not backed by an interface, so IMHO their usefulness is quite limited.
Comment #14
catchWe can add methods to interfaces as long as they're not tagged @api and have a 1-1 relationship with a class, or an associated base class/trait:
https://www.drupal.org/about/core/policies/core-change-policies/bc-polic...
I haven't been following the two issues though, so no opinion on the split as yet.
Comment #15
berdirThis is not a 1:1 relationship, that's why that other issue exists (until now, you had to load the user entity to use hasRole() from the user session object), there 3 implementations in core and I know that there are contrib implementations as well, for example simple_oauth.
Comment #16
catchhmm what am I missing.
So we have one class directly implementing AccountInterface, one interface (AccountProxyInterface) extending AccountInterface, and one class implementing AccountProxyInterface.
That is 2:2 rather than 1:1 but would be covered I think. It doesn't have to be 1:1 that's just one of the examples.
Neither the 5.0.x or 6.0.x branches of simple_oauth appear to implement AccountInterface or AccountProxyInterface afaict.
Comment #17
berdirWell fair enough, there is UserInterface of course, but that already has the method, so nothing changes there. And simple_auth actually implements that, which is IMHO weird but actually helpful here.
IMHO, AccountInterface really does feel like an @api interface to me, it's tied to \Drupal\basic_auth\Authentication\Provider\BasicAuth::authenticate(), so authentication providers return that and you'd expect different kind of implementations from that. UserSession is basically the internal implementation of \Drupal\user\Authentication\Provider\Cookie and if we ever do #2345611: Load user entity in Cookie AuthenticationProvider instead of using manual queries we would probably deprecate that.
That said, an account is pretty much expected to also exist as a user entity, the separation between the two is vague and IMHO really just exists to be not have a hard dependency in user module, but as soon as you have code doing User::load($account->id()), that dependency is back and very few if any drupal sites would imho be able to handle accounts that aren't user entities.
So... :shrug:
Comment #18
catchMoving back to 10.1.x but tagging for a second/third opinion.
Comment #21
rosk0Just stumbled across a TODO pointing to this issue in the core.
I'm not in the position to judge about the big picture consequences following from this change, but to me it looks logical and brings consistency.
Rebased original code to the
11.x, removed TODO's from where they were referring to this issue, updated method signatures where necessary. Code style checked - no new violations added.Will see what test results would look like.
Comment #23
rosk0Had to create new branch and a merge request as it's impossible (at least for me) to change existing merge request target.
New MR is https://git.drupalcode.org/project/drupal/-/merge_requests/6931
Comment #24
andypost@Berdir re #17 maybe easier to move UserInterface to core and get rid of AccountInterface?
Comment #26
nicxvan commentedTests are still passing, @andypost, do you have specific objections to this?
From a developer perspective this is really nice for consistency.
Comment #27
smustgrave commentedAsked about this in #needs-review-queue-initative and @xjm mentioned can move this to RTBC if feel this is ready and just missing release manager sign off.
Haven't seen a follow up to #26 so looking at just the MR I do not see any issue with the move. Typehints appears good too.
Comment #28
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 necessarily 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 #30
avpadernoI am setting back the status set by comment #27.
Comment #31
alexpottAs @berdir has pointed out we can't add the return typehint in Drupal 11. So this is definitely not rtbc - although maybe it is for 12.x - but that's not open.
Comment #32
pritishkumar commentedWorking on the remaining issue fix in DrupalConVienna2025 with help from @anmolgoyal74
Comment #33
pritishkumar commentedRemoved the return typehint
Comment #34
pritishkumar commentedPipeline is failing on php 8.3 so moving back to needs work
Comment #36
shalini_jha commentedI checked the pipeline failure and tested it locally — it appears to be a random failure. It has now passed, so I am moving the ticket back to NR.
Comment #37
dcam commentedThe feedback was addressed. I don't have anything to add. So I'm going to move this back to RTBC.
Comment #39
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily 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 #40
dcam commentedPost-bot-rebellion rebase
Comment #41
longwaveLet's just do this in 11.4, it's been waiting long enough and nobody has any better ideas. The split between account and user is unclear and awkward, but we don't have a better solution right now. Removing RM review tag.
Committed and pushed 83869c4f1b3 to main and bb5178f2101 to 11.x. Thanks!
Comment #45
longwaveComment #47
catch@mondrake pointed out in slack this seems to have broken 11.x tests, reverting for now to see if that gets us back to green.
Comment #49
catchNo, that's not it, reverted the revert.