feat: allow if and unless options for skipping authorize and load, similar to check_authorization #808
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds
if
andunless
options for controller helpers for skipping authorization and loading of resources.The idea is to allow these helpers to behave the same as
check_authorization
.Why: These options allows more control over the behaviour of these helpers with run time conditions.
I'd also like to share the particular use case that prompted this feature.
If a controller action concern skips authorization for one of the actions it defines, it runs the risk of that skip
direction being overwritten by any other skip direction that might be invoked in the controller including the concern.
It can also conflict with any other concern included in the controller.
Take the following concern for example.
and a controller that includes the concern:
Now authorization will not be skipped for
concern_action
Having this feature allows to work around this problem by declaring the
skip_authorization
in thebase_controller
with a controller action to determine if the authorization should be skipped or not.
A list of actions is then maintained by the base controller.
Child controllers and concerns append actions to the shared list for skipping the authorization.
The controller action uses the shared list to determine if authorization should be skipped or not.
This allows multiple controllers and concerns down the tree to skip relevant actions without conflict.
Help needed
In addition to the normal feedback and review, I need a little help with the implementation.
I have violated the rubocop rule
Metrics/ClassLength
by 7 lines and am not quite sure how to avoid it.