Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access_limited to be reset on update #510

Merged
merged 4 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def restrict_request_format
end

def asset_params
exclude_blank_redirect_url(
params
normalize_redirect_url(
normalize_access_limited(params)
.require(:asset)
.permit(:file, :draft, :redirect_url, :replacement_id, :parent_document_url, access_limited: [])
)
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/base_assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ def create

protected

def exclude_blank_redirect_url(params)
def normalize_redirect_url(params)
params.reject { |k, v| (k.to_sym == :redirect_url) && v.blank? }
end

def normalize_access_limited(params)
if params.has_key?(:asset) && params[:asset].has_key?(:access_limited) && params[:asset][:access_limited].empty?
params[:asset][:access_limited] = []
end
params
end

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems quite "ugly", but I can't see a way to simplify it. I think it would be useful to mention other solutions you considered, e.g. having two fields: Asset#access_limit? & Asset#authorized_users, and ideally capturing these ideas in a GitHub issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the commit note in the initial commit to explain the alternative approach of having two fields.

I've captured the idea of using two fields in #511.

def cache_control
AssetManager.cache_control
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/whitehall_assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
private

def asset_params
exclude_blank_redirect_url(
normalize_redirect_url(
params
.require(:asset)
.permit(
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/assets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@
expect(assigns(:asset).access_limited).to eq(['user-id'])
end

it 'resets access_limited to an empty array for an existing asset with an access_limited array' do
asset.update_attributes!(access_limited: ['user-uid'])

# We have to use an empty string as that is what gds-api-adapters/rest-client
# will generate instead of an empty array
attributes = valid_attributes.merge(access_limited: '')
put :update, params: { id: asset.id, asset: attributes }

expect(assigns(:asset).access_limited).to eq([])
end

it 'stores redirect_url on existing asset' do
redirect_url = 'https://example.com/path/file.ext'
attributes = valid_attributes.merge(redirect_url: redirect_url)
Expand Down