Skip to content

add resource policy for download to match show#1909

Merged
jmilljr24 merged 3 commits into
mainfrom
fix-download-button-on-public-resource
Jun 25, 2026
Merged

add resource policy for download to match show#1909
jmilljr24 merged 3 commits into
mainfrom
fix-download-button-on-public-resource

Conversation

@jmilljr24

Copy link
Copy Markdown
Collaborator

Closes #1908

What is the goal of this PR and why is this important?

Fix resource download link for unauthenticated-users

How did you approach the change?

skip authentication check for download and add download policy

UI Testing Checklist

Anything else to add?

@jmilljr24 jmilljr24 requested a review from maebeale June 24, 2026 19:18
@jmilljr24

Copy link
Copy Markdown
Collaborator Author

@maebeale I think this is fine for a quick fix.

Question:
This highlighted a discrepency on the Resource show page. There are two places a user can click to download the asset. 1. the download button 2. the preview image link. The download button uses the download controller action. The preview link is a direct url_for(@file).

To some degree I think that both should follow the same pattern. Using the direct url makes sense because the show page is already gated to the correct policy on who can see (in turn who can download). The download button/action however taps into the the ahoy tracking which we don't have with the direct url.

I can create an issue for this, just wanted to see which direct you thought before I wrote it up. It is a low priority.


def download?
true
show?

@maebeale maebeale Jun 25, 2026

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.

i don't think show will be true for these hidden_from_search resources. i think true is better, or, we add show? || record.hidden_from_search?, or, update the show policy action.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There is a mix up between the policy scope and the policies related to controller actions.

hidden_from_search is not a controller action, it is just the flag the search scope looks at to filter out results.

I'm confident show? is set up correctly. A resource that is hidden from search would still need the correct settings for the correct users to see it regardless of if it is hidden from search or not. In turn, if a user can see the show page, they should be able to download it as well.

Comment thread spec/policies/resource_policy_spec.rb Outdated
.not_to be_allowed_to(:download?)
end

it "can download a publicly visible resource that is hidden from search" 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.

regular_user should be able to download it too. they may still refer to their ticket from time to time.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This test is actually unneeded. hidden_from_search is only used on the scope for search results. There is no direct policy gated on it.

@maebeale

Copy link
Copy Markdown
Collaborator

@jmilljr24 i don't think the policies are right yet

@maebeale

Copy link
Copy Markdown
Collaborator

@jmilljr24 i'm fine w consolidating Resource download behavior, though i thought there were cases for both. def should add the ahoy tracking to direct if we keep it or pick it as the unified behavior. if we pick one i pick direct where you get the full preview.

@maebeale maebeale left a comment

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.

i don't think the policy is correct yet so surprised tests aren't catching it. i must be missing something.

@jmilljr24

jmilljr24 commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

i pick direct where you get the full preview.

I'm not sure I know what you mean by this.

@jmilljr24 jmilljr24 merged commit ecdb303 into main Jun 25, 2026
3 checks passed
@jmilljr24 jmilljr24 deleted the fix-download-button-on-public-resource branch June 25, 2026 17:16
@jmilljr24

Copy link
Copy Markdown
Collaborator Author

i don't think the policy is correct yet so surprised tests aren't catching it. i must be missing something.

I tried to explain it on the individual comments but here is the big picture:

resources/1 hits the show action -> show? policy

def show?
    admin? || record.publicly_visible? || (authenticated? && record.published?)
  end

So if you are authorized to view the show page, you should be authorized to download. There is no reason to duplicate the show method so I made download? call the show? method. Realistically I could have use an alias but I know that action policy isn't always intuitive if you don't work with it much. The registration ticket routes never hit the resource show action or policy. So the user can see the registration version of the resource, the download button which was copied from there resource show still points to resource#download.

hidden_from_search is only used on the Resource index results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resource - download link produces html, not document

2 participants