Skip to content

Modified sessions controller to return 401 for destroy action with no user signed in#4878

Merged
tegon merged 4 commits intoheartcombo:5-rcfrom
aamarill:fix-4782
Dec 28, 2018
Merged

Modified sessions controller to return 401 for destroy action with no user signed in#4878
tegon merged 4 commits intoheartcombo:5-rcfrom
aamarill:fix-4782

Conversation

@aamarill
Copy link
Copy Markdown

@aamarill aamarill commented May 18, 2018

This PR is for issue #4782 @tegon
To clarify the 3 tests added:

1. Authenticated non-navigational request - returns 204.

  • This was the original behavior before I made any changes. I simply added this test to ensure this was not broken after my implementation.

2. Unauthenticated non-navigational request - returns 401.

  • This is the new behavior added.

3. Unauthenticated navigational request - returns 302.

  • This was the original behavior before I made any changes. I simply added this test to ensure this was not broken after my implementation.

set_flash_message! :notice, :already_signed_out

respond_to_on_destroy
respond_to do |format|
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could avoid the respond_to duplication changing the #respond_to_on_destroy to accept the status as parameter, something like this:

def respond_to_on_destroy(status: :no_content)
  respond_to do |format|
    format.all { head status }
    format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
  end
end

Then, here we just do respond_to_on_destroy(status: :unauthorized). WDTY?

Copy link
Copy Markdown
Collaborator

@feliperenan feliperenan Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the default :no_content isn't a good idea. I like the idea to have the status explicited in the caller.

respond_to_on_destroy(status: :no_content)
respond_to_on_destroy(status: :unauthorized)

assert_equal 204, @response.status
end

test "#destroy returns 204 status if the requested format is not navigational" do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is already tested in the test above. IMHO changing the test name would be enough.

#destroy doesn't set the flash and returns 204 status if the requested format is not navigational

assert_equal 401, @response.status
end

test "#destroy returns 302 status if user is not signed in and the requested format is navigational" do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@tegon
Copy link
Copy Markdown
Member

tegon commented Dec 27, 2018

@aamarill are you still going to work on this?

I'm asking to know if I should assign it to someone else.

@aamarill
Copy link
Copy Markdown
Author

@tegon please go ahead and assign to someone else. Thank you

@tegon tegon added this to the 5.0 milestone Dec 28, 2018
@tegon tegon changed the base branch from master to 5-rc December 28, 2018 13:17
@tegon tegon merged commit 31dd92b into heartcombo:5-rc Dec 28, 2018
@tegon
Copy link
Copy Markdown
Member

tegon commented Dec 28, 2018

@aamarill Since the changes were small, I committed them directly to your branch. Thanks for your contribution!

tegon pushed a commit that referenced this pull request Jan 2, 2019
feliperenan pushed a commit that referenced this pull request Jan 18, 2019
feliperenan pushed a commit that referenced this pull request Jan 28, 2019
feliperenan pushed a commit that referenced this pull request Jan 28, 2019
feliperenan pushed a commit that referenced this pull request Jan 29, 2019
tegon pushed a commit that referenced this pull request Feb 15, 2019
mracos pushed a commit that referenced this pull request Sep 19, 2019
carlosantoniodasilva pushed a commit that referenced this pull request Dec 31, 2025
It's an unauthenticated request, so return 401 Unauthorized like most
other similar requests.

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
carlosantoniodasilva pushed a commit that referenced this pull request Dec 31, 2025
It's an unauthenticated request, so return 401 Unauthorized like most
other similar requests.

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
carlosantoniodasilva pushed a commit that referenced this pull request Dec 31, 2025
It's an unauthenticated request, so return 401 Unauthorized like most
other similar requests.

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
carlosantoniodasilva pushed a commit that referenced this pull request Dec 31, 2025
It's an unauthenticated request, so return 401 Unauthorized like most
other similar requests.

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
@carlosantoniodasilva
Copy link
Copy Markdown
Member

Update: this was originally merged into a "v5" branch that never got into main or released. I just cherry-picked it with a few changes to main to finally (hopefully?) get it released soon: 9a149ff

sandbergja added a commit to pulibrary/orangelight that referenced this pull request Feb 3, 2026
le0pard pushed a commit to le0pard/devise that referenced this pull request Feb 9, 2026
…ombo#4878)

It's an unauthenticated request, so return 401 Unauthorized like most
other similar requests.

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants