Problem/Motivation
In D7, a manual created menu link pointing to "user/login" only showed up if I'm not already logged in.
In D8, the link shows up no matter whether I'm already logged in or not.
Guess that has something to do with the new routing system.
Steps to replicate this issue:
- Goto: /admin/structure/menu/manage/main
- Add a menu link "Login" to the path "/user/login"
- Go to the home page, , whilst logged in, you should have 2 links "Home" and "Login"

On Drupal 7 you wouldn't see this link.
Proposed resolution
Before

After

Remaining tasks
Resolve questions raised in #114
Is this working as designed? If not, what other places need updating.
User interface changes
Yes, login menu item will not display if one is already logged in
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #132 | 2267603--after--patch--pic.png | 7.31 KB | vikashsoni |
| #132 | 2267603--before--patch--pic.png | 8.2 KB | vikashsoni |
| #125 | AP NA 2267603.png | 171.35 KB | chetanbharambe |
| #125 | AP 2267603.png | 284.9 KB | chetanbharambe |
| #125 | BP 2267603.png | 265.03 KB | chetanbharambe |
Issue fork drupal-2267603
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
dman commentedCannot replicate here today - can you provide a screenshot of where you see this link?
Comment #2
stefan lehmannComment #3
stefan lehmannCan replicate this behavior. See the updated issue description. As possibly quite a lot of websites rely on this behavior I'd suggest to have the same behavior in D8?
Comment #4
stefan lehmannNot sure if it's the right approach, but this patch makes the old behavior available. Can somebody please review the patch. Also not 100% sure about naming conventions etc. Thanks!
By the way I tried using:
But that wouldn't work. I assume because LoginStatusCheck.php returns static::DENY in this case and to allow something based on a DENY would be inconsequent? Not sure .. :-)
Comment #5
stefan lehmannFound out why:
$request->attributes->get('_menu_admin')has to be added as well. So you're actually allowed to create that link in the admin UI. This patch fixes it.
Comment #6
stefan lehmannComment #7
panchoComment #8
panchoWorks, but why can't we simply use the negated existing rule:
_user_is_logged_in: 'FALSE'Respectively why doesn't this approach work? 'FALSE' is being evaluated as 'TRUE'.
Comment #9
panchoHere's the - non-working - patch to test and find out why #8 doesn't work:
Comment #11
stefan lehmannEven if your condition would successfully validate for anonymous users, you would still have the problem, that you can't even add this link in the admin ui anymore, as you're not allowed to access that path.
Comment #12
tim.plunkettSee \Drupal\user\Access\LoginStatusCheck, it doesn't actually care about the value. Only the key matters. so 'TRUE' and 'FALSE' are the same here.
Comment #13
panchoRerolled #5 to catch up with HEAD.
Comment #14
panchoComment #16
panchoSorry, now this one should do.
Comment #17
wiifmThe patch by @pancho was great, I have made a few tweaks:
AnonymousStatusCheckand also renamedLoginStatusChecktoAuthenticatedStatusCheckfor consistency. I believe the previous naming was going to be confusing in the future.Created a new menu link that pointed to
user/loginand verified that only anonymous users could see this in the menu.Comment #19
wiifm17: 2267603-17.patch queued for re-testing.
Comment #20
ekl1773This patch applied and worked properly for me. Login menu item displays for anonymous users and not for logged in users. Someone else might want to take a look at the code, but so far as I can tell, this is good.
Comment #21
pwolanin commentedIs something that needs to be killed, though I'm not sure how.
Comment #24
stefan lehmannComment #25
pushpinderchauhan commentedJust rerolled of #17.
Comment #26
stefan lehmannPlease assign a ticket to yourself er.pushpinderrana when you start working on it. I just wasted half an hour or so. :-/
The problem with:
is imho out of scope of this ticket, as this feature is also used by core\modules\menu_ui\src\MenuForm.php.
I just tested the patch from #25 and it works as expected. Can somebody else review it please? I think it's ready to be RBTC then.
Comment #27
pushpinderchauhan commented@Stefan Lehmann, sorry for that. Unfortunately I started on this issue when this was not assigned to you and I would assuming it just take a while to rerolled this so forget to assign to me. Thankyou for your time and kindness.
Comment #28
stefan lehmannNo problemo er.pushpinderrana :-) ..
Comment #29
stefan lehmannOk .. actually I think this is finally RBTC!
Comment #30
alexpottWe need some tests for this - so we don;t break it again.
Comment #31
wiifmI had a crack at this, 3 tests still fail, and I have no idea what I am doing wrong. Can someone more fluent in the menu system help here? Only marking as needs review to kick the testbot.
Tests run with
sudo -u www-data php core/scripts/run-tests.sh --verbose --url http://d8.localhost/ --file core/modules/user/src/Tests/UserMenuLinksTest.phpComment #33
dawehnerThis issue is strongly related to #2306991: MyAccountMenuLink should dynamically adjust route parameters, not hidden property
Comment #34
wuinfo - bill wu commented@Pancho, regarding the comment #7, I think we can remove the appended text. They are unnecessary and waste computing power to render them.
Comment #35
wuinfo - bill wu commentedCurrent on dev HEAD is:
_user_is_logged_in: 'FALSE'
So, we can use that status to exclude link from showing up on logged in user.
I think we do have a bug here.
_user_is_logged_in: 'FALSE' is not working properly, while _user_is_logged_in: 'TRUE' is working.
(Change it to TRUE, we can make the login link show up for authenticated user only. But not working on the other way.)
Need to redo the patch.
Comment #36
wuinfo - bill wu commentedHi @tim.plunkett regarding #12, I think Value TURE and FALSE is making a difference.
In file user.routing.yml if we change
To:
The "log out" link will appear for anonymous users.
Well, this change will prevent a user from logging out. We can do something to fix it in LoginStatusCheck.php.
Comment #37
wuinfo - bill wu commentedIt only happens to the role having permission 'link to any page'. Here is the patch to remove this check.
If we want to keep the way it is now. Then we can close this ticket. The regular authenticated users do not have this problem. The only user has the permission to access link to any page have the "problem".
Comment #38
wuinfo - bill wu commentedHere is a least impact change which replace the checkAccess with checkDisplay method.
Comment #40
andypostThis is a serious regression, suppose there should be a fix without API changes, maybe menu caching is broken somehow.
Added screenshot to IS
I find this argument name non-descriptive
also that needs doc-blco update, change record about AOI change
That's another api change
Looks that a reason - do calculate access only once
Comment #41
dcrocks commentedI have to ask: does this really need to be fixed? If it only affects users with the 'link to any page' permission, which should be really rare, the only probable impact is on the admin role. And to be honest, what's the big deal?
Comment #42
wuinfo - bill wu commentedHi @andypost,
Thanks for the comment. Is your attached image from applying patch of #38, or #37 or none of them? I thought change at #38 had been safe enough not causing a regression.
Comment #43
andypost@wuinfo I added a screenshot without patch to show the regression
Comment #44
wuinfo - bill wu commented@andypost, agree the argument name needs to be changed. What about 'permission_override' or just 'override'.
Comment #45
wuinfo - bill wu commentedWe have one more problem here: Can create menu link with an invalid URL
Comment #46
wuinfo - bill wu commentedHere is the test patch.
Comment #48
wuinfo - bill wu commentedTest failed as we expected.
Comment #49
dcrocks commented#2503755: Switch from user login block to login menu link and search block in standard profile is in but behavior remains same for this issue.
Comment #51
dawehnerThe problem here is that you have to reuse the existing user login link which is provided by default.
With that particular link, it works as expected.
Comment #52
wim leersIndeed. And that was fixed in #2503755: Switch from user login block to login menu link and search block in standard profile.
Comment #53
dcrocks commentedSorry, if you create a menu with a link '/user/login' it will still be displayed even though you are logged in. So the behavior hasn't changed.
Comment #54
dawehner@dcrocks
Right you need to reuse the existing login link.
Comment #55
andypostComment #56
dcrocks commentedStill don't quite understand what you mean. How do you 'reuse' the link? The link is part of the user account block, which is not what is wanted. Simply adding /user/login' link to a new or existing menu creates the problem. Placing the mouse over the link always produces '/.../user/login'.
Comment #62
b-prod commentedAny news on this?
@dawehner: maybe we need to add some explanation to the expected behaviour.
For example, if we need to avoid the "log out" link to appear in the user menu (or any other menu), this is not possible to use the default link.
So logically we disable the default link and create a new menu item with the path
/user/login, so anonymous users can log in. As authenticated users are not able to log in twice, the link should not appear...I hope those explanations may help to understand the criticality of this issue, as we need to do some hacks to get the expected result...
Comment #64
jnettikI've also just ran into this issue, which is very un-intuitive and restrictive in the current functionality. The text of the default link can't be (within the interface) altered, which some projects will need the ability to change. You can't also duplicate this link without recreating it in custom code, meaning you only get a simple Log In/Out link in one place on the site. To me, the most intuitive and flexible option for site builders is to have this check on the routes. `/user/logout` already seems to do this, if you're not logged in that route doesn't show, because an anonymous user can't log out. Shouldn't the `/user/login` route function with the same logic?
Comment #65
mcgowanm commentedRe-rolled #38 for D8.8.x
Comment #66
ajfernandez commentedHi, I applied patch #65 to my Drupal 8.8.1 but it does not work for me. I still have the issue.
The logout link works for me. It is shown only for logged in users and hidden for anonymous users. But the login link is always shown even for logged in users.
Any ideas? Thanks in advance!
Comment #67
swatichouhan012 commentedHii @ajFernandez, @mcgowanm i checked wrt 8.8.x and patch #65 working fine for me.
moving in Needs review.
Comment #68
ajfernandez commentedHi @swatichouhan012, I am trying to create a link to /user/login in my main menu, but this link is always visible. However, a link to /user/logout in my main menu is only visible when user is logged in. Could you try if the patch is working with /user/login link in any menu or it only works with user menu?
Thanks in advance!
Comment #70
swatichouhan012 commentedRerolled against 8.9.x ,
Comment #72
quietone commentedAn @param needs to be added to the doc block. Surely, the name 'nonedisplay' can be improved.
I added a before and after screenshot. The IS needs an update too. Also needs a test. Based on the original IS this is a regression, adding that tag too.
Comment #73
bunty badgujar commented#70 working fine. Re-adding for 9.1.x with suggestion #72
Comment #74
pankaj.singh commented@Bunty Badgujar, Tested on 9.1.x
Patch didn't work for me, the issue still persists on 9.1.x
Comment #75
quietone commented@pankaj.singh, Thanks for testing the patch. The problem is seen on the home page. Did you follow the steps to reproduce in the IS? I've updated it.
This is an area I don't know much about but that comment is vague to me. I want to know why type of check is being forced. It is currently also optional, so needs (optional). Something like "(optional) Set TRUE to .... on the menu link. Defaults to FALSE."
This still needs a test.
Comment #76
pankaj.singh commented@quietone, I think I attached the wrong screenshot in my last comment #74.
Attaching the correct screenshot here.
As the issue still visible, also once clicked on the login button it redirects to my account page.
Comment #77
quietone commented@pankaj.singh, I've just checked again and the patch works correctly and the screen shots I uploaded in #72 are still correct. Did you clear cache after applying the patch?
Comment #78
pankaj.singh commented@quieton, yes I cleared the cache post applying the patch. Also, I retested again but had no luck.
Comment #79
pradeepjha commentedHi @pankaj.singh,
First replicate this issue:
- Goto: /admin/structure/menu/manage/main
- Add a menu link "Login" to the path "user/login"
- Go to the home page, , whilst logged in, you should have 2 links "Home" and "Login"
Then apply the patch and clear site level cache and browser cache ( ctrl + R ). Then it will work for you. Please try again. If not then let us know all environment versions.
I've tested #73, it's working fine for me.
Comment #80
pankaj.singh commented@quieton @pradeepjha I was looking for the possible reasons why it's not happening on my end. So, I just re-setup 9.1.x-dev on my local reproduced the issue and applied the patch given in #73.
Patch worked for me as well, apologies for the inconvenience. RTBC+1
Comment #81
bunty badgujar commentedMoving need review. Lets see if we can move it to RTBC.
Comment #82
priyanka.sahni commentedComment #83
priyanka.sahni commentedVerified and tested by applying the patch #73.It looks good.Moving it to RTBC.
Steps to test -
1. Go to admin site.
2. Go to /admin/structure/menu/manage/main
3. Add a menu link "Login" to the path "user/login"
4. Go to the home page, , whilst logged in, you should have 2 links "Home" and "Login"
5. Verify
Patch Applied -

Configuration -

Before Patch -

After Patch -

Comment #84
priyanka.sahni commentedComment #85
alexpottAllow force check on menu linkcan be improved. I think we should document that this results in the check ignoring the 'link to any page' permission. Also the name of the variable is not great because checking the permission is a kind of access check. Not sure what a better name is.This check should be the other way around - we should check the local variable first.
Comment #86
mohrerao commentedWorking on automated tests.
Comment #87
hardik_patel_12 commentedtried to cover point 85.2,85.4 and 85.3 but
still needs to replace.
Comment #88
mohrerao commentedReplaced deprecated methods from #86 and also moved code to a specific method.
Comment #89
mohrerao commentedComment #90
alexpottThis test is now running twice - because it starts with test... the best thing here is to remove it from testMenu or call it doTest...
I think i'd remove the call in testMenu... and also move the user create into the test menu itself and out of setUp.
I still think we should address #85.1 by special casing the login and logout links and not add this whole extra menu manipulator,
Comment #91
quietone commentedSetting to NW for the points in #90.
Comment #92
mohrerao commentedWorking on changes requested in #90
Comment #93
mohrerao commentedI have moved user with permission 'link to any page' from setUp to testLoginLinkVisibility() as this is the only test which uses this permission.
We will not be able to moe the other create user out of setUp as it has been used in many tests.
@alexpott, as per you comments in #85.1, are you suggesting that a separate permission for login link than using link to any page permission?
Comment #94
tanubansal commentedTested via updated test steps and patch #93
It's working fine and can be moved to RTBC
Comment #95
mohrerao commentedAdding tage 'Bug Smash Initiative'.
Comment #96
abhijith s commentedApplied patch #93 . Its works fine.Now the login link will be gone


before patch :
after patch:
RTBC
Comment #98
abhijith s commentedComment #99
alexpottre #93 - no I'm not suggesting a special permission. I'm suggesting the we hard code special behaviour for these links
Change to
!in_array($link->getRouteName(), static::FORCE_ACCESS_CHECK, TRUE) && $this->account->hasPermission('link to any page')and
or something like that.
Comment #100
paulocsComment #101
anmolgoyal74 commentedAddressed #99
Sorry paulocs, I have not assigned this issue to myself and started working on it.
Comment #102
paulocsAttaching a new patch based on comment #99. Ps: not sure if I declare the const in the right place, please review.
Also removing unnecessary tags.
Comment #103
paulocsComment #104
paulocsSo about patch #101, as @alexpott pointed on comment #90, there is no need to create another menu verification and that is why I removed
checkDisplay()in patch #103.Comment #105
anmolgoyal74 commentedSmall typo:
+ * The routes to force to check access.
Comment #108
kishor_kolekar commentedWorked on comment #105
Comment #110
paulocsI did some tests and if we just replace
if ((!$link_access) && ($this->account->hasPermission('link to any page')))to!in_array($link->getRouteName(), static::FORCE_ACCESS_CHECK, TRUE) && $this->account->hasPermission('link to any page'), the site administrator will not be able to see the login link in the menu link list. So patch #88 is a better solution.One thing that patch #88 is missing is when the user is logged out, the logout link is still displayed. We should fix this and add tests for it.
Comment #111
ramya balasubramanian commentedHi @paulocs,
I have added a patch for logout issue. Please have a look. But 2 test cases are failed. Will check that.
Comment #113
paulocsPatch #111 does not work because the login link are not displayed in the menu list.
I added a test case for when the user is logout and it should not show the logout link.
I did not addressed 85.1 or 99 (same observation) because I did not find a way to work it properly. I tried it in patch #103 but patch is not show the login link the menu list.
Comment #114
alexpottThis can't be necessary given the route declaration.
There's part of me that I think given the it's 100% related to the link to any page permission that we're in works as designed territory. FWIW there are places other than system menu block that needs updating to use the new display manipulator... for example
\Drupal\system\SystemManager::getAdminBlock()and the toolbar (\Drupal\toolbar\Controller\ToolbarController::preRenderAdministrationTray()) and System overview page (\Drupal\system\Controller\SystemController::overview())Comment #115
paulocsSo if it is worked as designed, we should add only the
_user_is_logged_in: 'TRUE'requirement key to the user.logout route because for now, logout link is displayed when user not logged in.Comment #116
quietone commentedI tried the suggestion in #114 and it worked for a non-admin user but not for the admin. For the admin. the 'login' link is always displayed.
Comment #117
abh.ai commentedPatch on comment #113 works fine. This issue is also resolved with the same patch: https://www.drupal.org/project/drupal/issues/3182970#comment-14037003
Comment #118
guilhermevp commentedPatch #113 works as intended!
Comment #119
chetanbharambe commentedComment #120
chetanbharambe commentedI tried to apply the patch and have the following message on the terminal.
I think This patch needs re-roll.
Comment #121
vakulrai commentedI have validated the patch by #114, and its working as expected.
This
_user_is_logged_in: 'TRUE'argument only works with non -authenticated users. The logout issue is addressed here : https://www.drupal.org/project/drupal/issues/3182970#comment-14037003.I have Rerolled the patch.
Comment #122
vakulrai commentedUploaded the wrong patch in #121, Please follow this patch.
Thanks.
Comment #124
chetanbharambe commentedComment #125
chetanbharambe commentedVerified and tested patch #122.
Patch applied successfully and looks good to me.
Testing Steps:
# Goto: /admin/structure/menu/manage/main
# Add a menu link "Login" to the path "user/login"
# Go to the home page with the logged-in user
# User should not see "Log in" navigation in Menu once the user is Authenticated.
# User should see "Log in" navigation in the menu once the user is Non-Authenticated.
Looks good to me.
Can be a move to RTBC
Please refer attached screenshots for Before and After patch.
Comment #126
chetanbharambe commentedComment #127
quietone commentedSee that this is RTBC I cam back to find what the resolution of comment #114 is. In #vakulrai says that they have validated the by #114, and its working as expected. Can you explain what 'validated by' means?
I think there needs to be answers to the questions in #114, particularly when a committer is suggesting this is working as designed. Why is that block of code necessary? And further what about the other places that this need to be fixed that are mentioned in the last paragraph on #114. Do they need fixes as well.
Setting back to needs work.
Comment #129
chetanbharambe commentedComment #130
bart lambert commentedseems like the patch is not working in drupal 9.2.10
Comment #132
vikashsoni commentedApplied patch #122
working and applied successfully
after patch login button has been gone
Thanks for the patch
Comment #133
drupal-ramesh commentedPatch is not working for admin users in drupal 9.3.9.
Comment #137
bart lambert commented@drupal-ramesh
I think this is because you are logged in as administrator (supe-user). Try a other non-admin account and you will see it is ok.
(I think?)
Comment #139
anybodyI can't reproduce this issue like reported anymore.
/user/login(set in the menu link) is only accessible for guests! So works as expected for me.But now there's the issue, that
/userpage is accessible for guests (redirects to user/login), but the menu item linking/user(which is expected to do the same as the page) isn't visible for guests.Is that a separate issue or part of the discussion here?
To sum up my tests:
Expected / actual for guests:
/user=> accessible | not accessible (bug)/user/login=> accessible | accessible/user/logout=> not accessible | not accessible/user/edit=> not accessible | not accessibleExpected / actual for authenticated:
/user=> accessible | accessible/user/login=> not accessible | not accessible/user/logout=> accessible | accessible/user/edit=> accessible | accessibleaccessible = shown
not accessible = hidden
Can someone else confirm this in 11.x? How should we proceed?
Comment #140
johnpicozziI was having this issue in Drupal 10.0.8 - user/login path on menu item was showing for logged in user - Then I applied the patch in #122 and cleared my cache. The login link was not visible for logged in users and was visible for logged out users. Hope this is helpful in pushing this forward.
Comment #141
bkosborneStruggling to see how this is different from #2463753: [regression] Do not bypass route access with 'link to any page' permissions for menu links. Seems like that solves this problem too?
Comment #142
catch#2463753: [regression] Do not bypass route access with 'link to any page' permissions for menu links fixes this and also adds test coverage, agreed with @bkosborne and marking duplicate.