Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Updated: Comment #0
Problem/Motivation
Calling user->getRoles needn't always return the authenticated and anonymous roles.
Proposed resolution
Add paramater to the method to optionally filter those roles out
Remaining tasks
Write patch
User interface changes
None
API changes
New optional parameter on User::getRoles()
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#52 | interdiff-2096717-50-52.txt | 1.57 KB | chakrapani |
#52 | add_paramteres_to_getRoels-2096717-52.patch | 4.36 KB | chakrapani |
#50 | interdiff-2096717-47-50.txt | 1.55 KB | chakrapani |
#50 | add_paramteres_to_getRoels-2096717-50.patch | 5.58 KB | chakrapani |
#47 | interdiff-2096717-43-47.txt | 2.67 KB | chakrapani |
Comments
Comment #1
BerdirI think I opened a similar issue too...
Comment #2
-enzo- CreditAttribution: -enzo- commentedHello guys
This is a small patch to add the functionality requested in this issue. I modify the EntityUser and the UserSession.
I tested the changes creating two different accounts using the following code
Please check and let me know any fix required for this patch.
Berdir, if you can remember the other issue you mentioned you created, please let me know.
Comment #3
BerdirComment #4
pingers CreditAttribution: pingers commentedParameter name should be something more readable, E.g. $exclude_locked_roles. It should also be TRUE or FALSE. I.e. It's either excluding the roles or it isn't. True or false makes this clearer.
Add space between if and (.
Comment #5
BerdirYes, see also the notes in #2057999: Add argument to AccountInterface::getRoles() that allows to exclude the anonymous/authenticated roles
Comment #6
-enzo- CreditAttribution: -enzo- commentedHello folks
I follow the recommendations from Berdir and pingers and I use constants for locked roles.
Please check my new patch.
Comment #7
BerdirThere needs to be a space afer the , same for the other implementation.
There is at least one use case where we can use this, in user_admin_account().
We will possibly also need some specific tests for it.
Comment #8
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedremove spaces as per @Berdir comments.
Comment #9
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedComment #10
dawehnerWe do have unit tests for both User and UserSession so it would be cool to extend that with this functionality.
Let's use && instead of AND in core and use proper smaling after the ',' in in_array()
Comment #11
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedThanks @dawehner. Update patch as per comments.
Comment #12
-enzo- CreditAttribution: -enzo- commentedhello folks
I follow the @dawehner recommendations, include the changes in test unit.
IMHO we need to move the session unit test to user section, instead of PHP Unit where is located right now.
Comment #14
BerdirYeah, that's why I just used the string of the constant and not the actual string, phpunit doesn't like code that it can't autoload.
You could define them in the test, but that's just a hack. Personally, I'd just use the string and add a @todo about moving the constants to a class/interface but not sure what others think.
Comment #15
-enzo- CreditAttribution: -enzo- commentedHello Bendir
I applied the change to use the value instead of the constants and add the @todo message was added.
Comment #17
andypostThis change should be added to interface as well
needs space after
$role->value,
Comment #18
andypostComment #19
-enzo- CreditAttribution: -enzo- commentedHello Andy
Can you explain a little better what you mean when you said "This change should be added to interface as well", can you provide an example.
Regards,
Comment #20
dawehnerThere is an interface: core/lib/Drupal/Core/Session/AccountInterface.php which is implemented by both User as well as UserSession, so you have to change the arguments of the signature
of getRoles there as well.
Comment #21
pingers CreditAttribution: pingers commentedThe signature of a method implementing one defined in a parent class OR interface must match. I.e. the parameters must be the same. E.g.
setColor() has the same number of parameters (one called $color).
Comment #22
-enzo- CreditAttribution: -enzo- commentedHello folks
I did the changes related with spaces and the changes in interface, please review
Comment #24
pingers CreditAttribution: pingers commentedYou have also changed the signature of User::getRoles(), which is inherited from UserInterface and AccountInterface, so you would need to update those interfaces too.
Comment #25
pingers CreditAttribution: pingers commentedComment #26
-enzo- CreditAttribution: -enzo- commentedUpdated patch with missing interface
Comment #27
pingers CreditAttribution: pingers commentedUh, now
Now UserInterface:getRoles() doesn't conform to AccountInterface::getRoles(). All inherited methods would need to be updated.
Comment #29
BerdirUnlike the implementations, this is missing a default value. Also needs to have a = FALSE.
Then, the documentation standard for those is something lke "(optional) If TRUE, locked roles (anonymous/authenticated) are not returned. Defaults to FALSE."
I think it's ok for the test to use the DRUPAL_AUTHENTICATED constant. No need for the @todo then as this actually tests the user entity (and not the UserSession class), where we don't have as much of a problem with the constant.
Also, we don't write "Please" in the UI or code documentation :)
There's no reason to have the method declaration duplicated here. Just remove it completely :)
"Tests the getRoles() method." ?
Same here, no please, please :)
Personally, I'd leave out the @todo completely and instead create a follow-up issue and reference it here. If you want to work on that, awesome, but it's an API change and needs approval.
Comment #30
-enzo- CreditAttribution: -enzo- commentedHey @Berdir
I did almost all recommendations you did.
I maintain the Todo with some changes, because in the comment #14 yourself recommend me replace for the values because the patch unit test fail, event if in my local environment works.
About change propose in items #5, please create a new issues and I could work on that.
Please check the new patch and let me know if works for you.
Comment #31
-enzo- CreditAttribution: -enzo- commentedHey guys any update about my last comment and patch?
Comment #32
BerdirRemember to set issues to needs review to have them tested by the testbot and get reviews. Will try to review ASAP.
Comment #34
BerdirIn PHPUnit tests, you need to use assertEquals(), not assertEqual().
Comment #35
BerdirComment #36
ofry CreditAttribution: ofry commentedI've rerolled patch #30.
Comment #37
ofry CreditAttribution: ofry commentedComment #39
JeroenTFixed some whitespaces + replaced the assertEqual() with assertEquals().
There is still a failing test because the constant DRUPAL_ANONYMOUS_RID is undefined.
Drupal\Tests\Core\Session\UserSessionTest::testUserGetRoles Use of undefined constant DRUPAL_ANONYMOUS_RID - assumed 'DRUPAL_ANONYMOUS_RID' /var/www/drupal/core/lib/Drupal/Core/Session/UserSession.php:122 /var/www/drupal/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php:154
Comment #40
JeroenTComment #42
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedRe-rolled patch.
Comment #43
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedRe-rolled Patch.
Comment #44
sandipmkhairnar CreditAttribution: sandipmkhairnar commentedComment #46
chakrapani CreditAttribution: chakrapani commentedI am checking this..
Comment #47
chakrapani CreditAttribution: chakrapani commentedFixed the failing tests with the following changes:
Comment #49
chakrapani CreditAttribution: chakrapani commentedThe test is failing because the getRoles() mocked in UserTest.php doesn't have any arguments defined but we are trying to call the getRoles() method with arguments in UserSessionTest.php.
I got this: http://privatepaste.com/57e9d421ee from IRC. But still its not helping much.
Comment #50
chakrapani CreditAttribution: chakrapani commentedAfter some difficulty, here we have a working patch. Ran the tests locally and passed.
Thanks to Cottser and the author of this privatepaste(http://privatepaste.com/57e9d421ee) :)
Comment #51
dawehnerTo match 100% our codeing styles, the" (optional)" should be part of the next line, see https://drupal.org/node/1354
It is a bit odd to basically test the same code twice. This is also tested by the UserTest class below but I am fine with that.
Comment #52
chakrapani CreditAttribution: chakrapani commentedThanks dawehner,
I have made the changes suggested in #51.
Comment #53
dawehnerAwesome, thank you!
Comment #55
catchCommitted/pushed to 8.x, thanks!
Comment #57
heddnThis issue introduced a regression. See follow-up: #2277547: User::getRoles test coverage & regression fix