Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorize with multiple action in CanCan

I am trying to understand a bit better the capabilities of CanCan when it comes to authorization. Imagine this controller action:

def update
  if can? :action, Model or can? :resolve, Model or can? :authorize, AnotherModel
    # My Code here
    respond_with @model
  else
    raise CanCan::AccessDenied.new(nil, :update, Model)
  end
end

I got to this point while trying to find a solution to the above using authorize!. As far as I can see (also looking at the signature) authorize! only accepts one permission (action) and one subject, with an optional message, like this:

def authorize!(action, subject, *args)
  # code
end

Is there a way which I may be overlooking to instruct authorize to check for multiple actions? Putting two authorize one after the other will act as an AND condition between permissions, what I would like is it to work like an OR condition, basically similar to the custom code above (which has the problem of raising the AuthorizationNotPerformed in CanCan, avoidable with skip_authorize_resource which is not something I would really like to do).

like image 768
Tallmaris Avatar asked Dec 01 '25 02:12

Tallmaris


1 Answers

You can create an custom action and create as many or-conditions as you like.

can :my_update_action, Project do |project|
  can?(:read, ModelX) || can?(:read, ModelY) || ... 
end
like image 199
spas Avatar answered Dec 02 '25 17:12

spas