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

Unify action checks #1941

Merged
merged 1 commit into from
Aug 8, 2022
Merged

Conversation

pablobm
Copy link
Collaborator

@pablobm pablobm commented Mar 11, 2021

Fixes: #1861

When referring to a route in the code, we run two checks:

  • valid_action? is true if the route is defined, false otherwise.
  • show_action? is expected to be overridden by developers in order to implement authorization. For example, it's implemented by Administrate::Punditize in order to integrate Administrate with Pundit. It should return true if the current user can access a given route, false otherwise.

These two check should (almost) always happen together. For this reason, our code is peppered with if statements where both are checked... and a few others where we forget one or the other.

These checks should be unified into a single method call, in order to avoid issues like the one described at #1861. Here I propose such a method, which I call accessible_action?.

The original methods should still exist, as they do have their uses individually. The new method will delegate to the existing ones.

I have also renamed the two existing methods to something that I hope will make their intent clear:

  • valid_action? becomes existing_action?
  • show_action? becomes authorized_action?
  • In order to provide a clear upgrade path, the old names still exist and work, but they show a deprecation warning when used. They can be removed properly at a later version of Administrate.

Open question!

The original methods have a signature the_old_question?(action, resource). I have flipped the order of parameters to be the_new_question?(resource, action), as I find that more natural. But if people think the original order is better (or have some other proposal), I'm happy to oblige.

Also! The old valid_action? has a default parameter value (the full signature is valid_action?(name, resource = resource_class)). The new version doesn't have it, as I can't see that it has a good use case. Incidentally, this makes sense with the parameter order flip just described.

Copy link
Contributor

@c4lliope c4lliope left a comment

Choose a reason for hiding this comment

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

This change looks good; for your checklist, maybe you could rename:

  • valid_action? -> possible_action?
  • show_action? -> permissible_action?

...though I'm unsure because I'm barely involved here.
I'm sure you'll choose sensible names.

Much obliged for your changes, @pablobm !

app/views/administrate/application/_collection.html.erb Outdated Show resolved Hide resolved
@pablobm pablobm force-pushed the unify-action-checks branch 6 times, most recently from 4355d12 to 00d1247 Compare April 28, 2022 12:34
@pablobm pablobm force-pushed the unify-action-checks branch from 00d1247 to 4831e78 Compare May 19, 2022 21:42
@pablobm pablobm force-pushed the unify-action-checks branch 3 times, most recently from a97b0a1 to ae297b3 Compare May 26, 2022 13:39
@pablobm pablobm marked this pull request as ready for review May 26, 2022 13:51
@pablobm pablobm requested a review from nickcharlton May 26, 2022 13:51
@pablobm
Copy link
Collaborator Author

pablobm commented May 26, 2022

@c4lliope @nickcharlton - After more than a year, this PR is finally out of draft!

@pablobm pablobm changed the title WIP: Unify action checks Unify action checks May 26, 2022
When referring to a route in the code, we run two checks:

* `valid_action?` is `true` if the route is defined, `false` otherwise.
* `show_action?` is expected to be overridden by developers in order to
  implement authorization. For example, it's implemented by
  `Administrate::Punditize` in order to integrate Administrate with Pundit.
  It should return `true` if the current user can access a given route,
  `false` otherwise.

These two check should (almost) always happen together. For this reason,
our code is peppered with `if` statements where both are checked... and a
few others where we forget one or the other.

These checks should be unified into a single method call, in order to avoid
issues like the one described at thoughtbot#1861. This introduces a new method, called
`accessible_action?`.

The original methods should still exist, as they do have their uses
individually. The new method will delegate to the existing ones.

We also rename the two existing methods to something that will make their
intent clear:

* `valid_action?` becomes `existing_action?`
* `show_action?` becomes `authorized_action?`

In order to provide a clear upgrade path, the old names still exist and
work, but they show a deprecation warning when used. They can be removed
properly at a later version of Administrate.
Copy link
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

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

🎉

@nickcharlton nickcharlton merged commit f10b556 into thoughtbot:main Aug 8, 2022
jazairi added a commit to MITLibraries/thing that referenced this pull request Aug 22, 2022
This commit includes an update to the administrate gem that
requires a small change to our custom `show` template. (See
thoughtbot/administrate#1941 for more info).

Additionally, I noticed that administrate added a `delete` link
to the show template earlier this year: thoughtbot/administrate@e510efe.
We haven't yet added this to our custom template, and I'm assuming
that for now it's still not needed.
jazairi added a commit to MITLibraries/thing that referenced this pull request Aug 26, 2022
Why these changes are being introduced:

The most recent version of administrate refactors authorization
in a way that has downstream effects. See [#1941](thoughtbot/administrate#1941)
for more info.

Relevant ticket(s):

https://mitlibraries.atlassian.net/browse/ENGX-192

How this addresses that need:

This replaces our override of the `show_action?` method with
`authorized_action?` in the administrate application controller,
and it replaces our combined check of `valid_action?` and
`show_action?` with `accessible_action?` in the administrate views.

This also updates the ability so thesis processors can continue to
access the submitter dashboard. This is not currently needed
as we do not have any users with the thesis_processor role, but it
seemed wise to address it sooner than later.

Side effects of this change:

* Thesis processors can now see links to controller actions in the
submitter admin dashboard, as well as a link to the admin dashboard
itself from the main site nav.
* The admin submitter dashboard test will move to the admin/
subdirectory with the other individual dashboard tests. This change
will come in a subsequent commit to make the git history easier
to read.
francois-ferrandis added a commit to betagouv/rdv-service-public that referenced this pull request Nov 9, 2022
francois-ferrandis added a commit to betagouv/rdv-service-public that referenced this pull request Nov 10, 2022
francois-ferrandis added a commit to betagouv/rdv-service-public that referenced this pull request Nov 14, 2022
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.

Check if an associated model's route exists before trying to link it
3 participants