Skip to content

Rails Nested Controller Index Action Authorization #153

Answered by palkan
cgmcintyr asked this question in Q&A
Discussion options

You must be logged in to vote

To my opinion, your original implementation (authorize! @user, to: :show?) is correct: whenever you deal with a nested resource and a collection action (like #index), you should authorize access to the current scope (parent resource). That's how I've been dealing with this problem for a long time (even before Action Policy).

However, I see a different (maybe, even better) way of handling this situation with Action Policy. We may consider a user to be the authorization context, why not? If we agree on this, we can then use optional authorization context in the policy:

class RequestPolicy < ApplicationPolicy
  authorize :owner, optional: true

  default_rule :manage?
	
  def index
    allow…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@cgmcintyr
Comment options

@palkan
Comment options

@cgmcintyr
Comment options

@brendon
Comment options

@palkan
Comment options

Answer selected by cgmcintyr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #152 on February 02, 2021 12:23.