Add hasRole() method to AccountProxy and UserSession classes

Created on 7 August 2018, about 6 years ago
Updated 7 February 2023, over 1 year ago

Problem/Motivation

UserInterface defines the hasRole() method, but the Drupal::currentUser() returns an object implementing AccountInterface, which does not have this method unless the current user is authenticated, in which case a UserInterface object is returned.

Because of this, hasRole() cannot be used without first checking the object type.

Proposed resolution

Since UserInterface extends AccountInterface, move the hasRole method there.

Quoting @catch from #28 πŸ“Œ Add hasRole() method to AccountProxy and UserSession classes Fixed :

We do allow adding methods to interfaces that aren't explicitly tagged with @api, see https://www.drupal.org/about/core/policies/core-change-policies/drupal-8 β†’ ...

However in practice, we'd want to avoid breakage in a minor for this change, so I think we should grep contrib.

If we can't change the interface in a Drupal 9 minor, we could still add the implementations of the method, and document that the additional method will be on the interface in Drupal 10 etc. just without changing the interface itself.

And @xjm:

So, I think we should do what @catch says in the second part of his comment in #28.

To summarize:
In this task, we add hasRole() implementations to AccountInterface implementations: AccountProxy and UserSession.
In the follow-up task, we add a method to AccountInterface in Drupal 10 - #3228209: Move hasRole() method from UserInterface to AccountInterface β†’ .

Remaining tasks

Review https://git.drupalcode.org/project/drupal/-/merge_requests/1059;
Address comment #42;
Commit;

API changes

Drupal\Core\Session\AccountProxy and Drupal\Core\Session\UserSession now implement hasRole() method. See the change record: https://www.drupal.org/node/3002289 β†’

πŸ“Œ Task
Status

Fixed

Version

10.1 ✨

Component
User moduleΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States jhedstrom Portland, OR

Live updates comments and jobs are added and updated live.
  • Needs release manager review

    It is used to alert the release manager core committer(s) that an issue significantly affects the overall technical debt or release timeline of Drupal, and their signoff is needed. See the governance policy draft for more information.

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 catch

    The re-roll in #60 was posted less then 24 hours after the patch in #58 and doesn't change anything from that patch. I'm removing issue credit.

Production build 0.71.5 2024