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

Conversation

chrisroos
Copy link
Contributor

@chrisroos chrisroos commented Mar 6, 2018

While investigating issue #450, I noticed that resetting/removing the access_limited status of an asset wasn't working as expected.

This is because when we pass an empty access_limited array to gds-api-adapters/rest-client it's turned into an empty string in Asset Manager. This results in an UnpermittedParameter exception for the access_limited parameter in Asset Manager as it's expecting an Array but receives a String. The fix is to detect the empty string in the controller and convert it back into the empty array that's intended. I don't think this is particularly nice but I think it's good enough for now.

Note that I haven't had to use handle_empty_access_limited_param in WhitehallAssetsController#asset_params as the problem only occurs when updating an access-limited asset, and the WhitehallAssetsController is only responsible for creating assets.

I also considered using two fields to implement the access-limiting functionality: a boolean to control whether access-limiting was enabled in conjunction with an array of user UIDs who are authorised to view the asset. I decided against that approach because it's more work and because it means the implementation in Asset Manager diverges from the access-limiting functionality in the Content Store.

Copy link
Contributor

@floehopper floehopper left a comment

Choose a reason for hiding this comment

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

I've added some comment which are mostly/all just suggestions for improvements. I'm happy for you to address them as you see fit. Marking as approved.

@@ -38,8 +38,9 @@ def restrict_request_format
end

def asset_params
asset_params = handle_empty_access_limited_param(params)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be useful to explain why you have to operate on the params before the calls to require and permit.

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 to explain why I need to modify the params before calling require and permit.

exclude_blank_redirect_url(
params
asset_params
.require(:asset)
.permit(:file, :draft, :redirect_url, :replacement_id, :parent_document_url, access_limited: [])
)
Copy link
Contributor

Choose a reason for hiding this comment

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

A possible alternative approach which I think looks a bit cleaner:

  def asset_params
      exclude_blank_redirect_url(
        handle_empty_access_limited_param(params)
          .require(:asset)
          .permit(:file, :draft, :redirect_url, :replacement_id, :parent_document_url, access_limited: [])
      )
    )
  end

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 changed this in a new commit in this branch.

@@ -22,6 +22,13 @@ def exclude_blank_redirect_url(params)
params.reject { |k, v| (k.to_sym == :redirect_url) && v.blank? }
end

def handle_empty_access_limited_param(params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: I think the _param suffix is unnecessary, given params are passed in as an argument, and inconsistent with exclude_blank_redirect_url. I'd prefer the method name to be handle_empty_access_limited.

Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: Possibly rename both these methods as follows:

  • exclude_blank_redirect_url -> normalize_redirect_url
  • handle_empty_access_limited_param -> normalize_access_limited

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 renamed the two methods as you've suggested - in two new commits in this branch.

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.

@chrisroos chrisroos force-pushed the allow-access-limited-to-be-reset-on-update branch from aadc1af to ee64cb6 Compare March 7, 2018 15:34
While investigating issue 450[1], I noticed that resetting/removing the
access_limited status of an asset wasn't working as expected.

This is because when we pass an empty `access_limited` array to
gds-api-adapters/rest-client it's turned into an empty string in Asset
Manager. This results in an `UnpermittedParameter` exception for the
`access_limited` parameter in Asset Manager as it's expecting an Array
but receives a String. The fix is to detect the empty string in the
controller and convert it back into the empty array that's intended. I
don't think this is particularly nice but I think it's good enough for
now.

Note that I haven't had to use `handle_empty_access_limited_param` in
`WhitehallAssetsController#asset_params` as the problem only occurs when
_updating_ an access-limited asset, and the `WhitehallAssetsController`
is only responsible for _creating_ assets.

I also considered using two fields to implement the access-limiting
functionality: a boolean to control whether access-limiting was enabled
in conjunction with an array of user UIDs who are authorised to view the
asset. I decided against that approach because it's more work and
because it means the implementation in Asset Manager diverges from the
access-limiting functionality in the Content Store.

[1]: #450
@chrisroos chrisroos force-pushed the allow-access-limited-to-be-reset-on-update branch from ee64cb6 to f2c34a9 Compare March 7, 2018 15:40
@chrisroos chrisroos merged commit 8ea0c5b into master Mar 7, 2018
@chrisroos chrisroos deleted the allow-access-limited-to-be-reset-on-update branch March 7, 2018 15:51
@chrisroos
Copy link
Contributor Author

Thanks for reviewing, @floehopper. I've addressed your feedback, rebased on master, force pushed and merged!

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.

2 participants