Problem/Motivation

The logout link is displayed for anonymous users which makes no sense (in my opinion) because users can not logout if they are not logged in. The user can be confused if the logout link is displayed.

Steps to reproduce

1) Add a link to the main navigation with title "Logout" and link "/user/logout"
2) Logout from the site and see that the logout link is displayed

LogoutLink

Proposed resolution

Add the _user_is_logged_in: 'TRUE' requirement to the user.logout route.

Comments

paulocs created an issue. See original summary.

cilefen’s picture

cilefen’s picture

Wrong tag - sorry!

paulocs’s picture

Assigned: paulocs » Unassigned
Status: Active » Needs review
Issue tags: +Bug Smash Initiative
Related issues: -#2267603: "Login" link shows up for logged-in users as well
StatusFileSize
new1.59 KB
new2.01 KB

Attaching test only patch and a patch.
Tagging the Bug Smash Initiative to discussion.

paulocs’s picture

paulocs’s picture

Yes, I was working in the related issue as well, but I think the logout link and login link must have a different approach and that is why I opened na issue specifically for the logout link.

The last submitted patch, 4: 3182970-TEST-ONLY-2.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 4: 3182970-2.patch, failed testing. View results

cilefen’s picture

They are, let us say, thematically related but may have different causes.

paulocs’s picture

chandrashekhar_srijan’s picture

StatusFileSize
new1.24 KB

Please review. The solution is conflicting with https://www.drupal.org/project/drupal/issues/2193803.

chandrashekhar_srijan’s picture

StatusFileSize
new2.07 KB
abh.ai’s picture

Seems like the patch on this comment fixes this issue as well: https://www.drupal.org/project/drupal/issues/2267603#comment-13893903

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new4 KB
new2.77 KB

The solution to add _user_is_logged_in: 'TRUE' has an exception that it is redirecting the user to "Access denied" page which should not be the case , the anonymous user should get redirected to "Home page".

The tests are failing because of that , so made some changes to the user subscriber and updated few tests.

Thanks.

vakulrai’s picture

StatusFileSize
new3.65 KB

Fixed some Coding standard issues.

vikashsoni’s picture

StatusFileSize
new13.27 KB
new12.44 KB

Applied #15 patch working fine sharing screenshot...

guilhermevp’s picture

Patch #15 test is stuck.

sonam.chaturvedi’s picture

Assigned: Unassigned » sonam.chaturvedi
sonam.chaturvedi’s picture

StatusFileSize
new52.6 KB
new50.89 KB
new53.65 KB

Verified and tested patch#15. Patch applied successfully.

Testing Steps:
1. Goto Structure > Menus > Main navigation
2. Create a link with title "Logout" and link "/user/logout"
3. Logout from the site
4. Check "Logout" link is displayed for anonymous user
5. Now apply the patch
6. Again, check "Logout" link is not displayed for anonymous user
7. Login to the site and check "Logout" link is displayed for authenticated user

Testing Results:
1. "Logout" link is not displayed for anonymous user
2. Authenticated user is directed to homepage on logging out.

sonam.chaturvedi’s picture

Assigned: sonam.chaturvedi » Unassigned
adalbertov’s picture

Status: Needs review » Needs work

Just checked the patch, it indeed works, but there is a CI error happening with the tests runs for patch #15. It could be something not related with issue in hand(like a SSH key error), but since it didn't perform correctly, I'm moving the issue to "Needs Work". If its not related, please disconsider this point, and keep moving the issue.

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new3.52 KB
new1.17 KB

Thanks for the patch! I've slightly improved the asserts by using linkByHref(Not)Exists

Madhu kumar’s picture

StatusFileSize
new39.08 KB
new39.57 KB

Patch #22 applied cleanly and working as expected and sharing screenshot for reference.

Madhu kumar’s picture

Status: Needs review » Reviewed & tested by the community
acbramley’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php
@@ -72,6 +72,25 @@ public function onException(ExceptionEvent $event) {
+    $redirect_403_path = Url::fromRoute('<front>', [], ['absolute' => TRUE]);
+    if ($this->account->isAnonymous()) {
+      $event->setResponse(new RedirectResponse($redirect_403_path->toString()));
+    }

On further inspection I actually don't think this is right. This is effectively redirecting a user to the frontpage on any 403 that isn't the logout page which is a change in behaviour

acbramley’s picture

+++ b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php
@@ -72,6 +72,25 @@ public function onException(ExceptionEvent $event) {
+    if (!($exception instanceof AccessDeniedHttpException) && $route_name !== 'user.logout') {

Sorry it's this bit that's not right. It should be || so we don't redirect if it's not a 403 OR it's not the user.logout route. I think it'd make more sense to invert the conditions and put the whole block inside it rather than the early return.

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new3.64 KB
new2.36 KB

Here's an updated patch

  1. Moves the response alter into the existing onException as it was already doing something very similar
  2. Adds further test coverage to ensure non-logout links don't redirect
acbramley’s picture

StatusFileSize
new3.42 KB

Removes extra whitespace -_-

dww’s picture

Status: Needs review » Needs work

This looks great, thanks!

The test coverage seems pretty solid. I can't think of any aspect of this bug that isn't reflected in the new tests.

I only have 1 tiny ubernit that stopped me from RTBC'ing:

+++ b/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
@@ -492,6 +493,30 @@ public function doMenuTests() {
+    // Create user with required permissions

Could be:

// Create a user with the required permissions.

(or just remove this comment entirely, the code is self-documenting enough).

Thanks again!
-Derek

dww’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.34 KB
new833 bytes

Bah, this is trivial enough, I'm just gonna do it and self-RTBC... ;)

  • catch committed d2799a3 on 9.2.x
    Issue #3182970 by acbramley, paulocs, vakulrai, chandrashekhar_srijan,...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

This looks good, and the correct redirection fix for #2193803: Anonymous users receive access denied when attempting to logout which introduced the bug in the first place trying to fix a diferent one.

Committed/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!

  • catch committed 139d9ea on 9.1.x
    Issue #3182970 by acbramley, paulocs, vakulrai, chandrashekhar_srijan,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.