Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:20:59+00:00 2026-06-14T20:20:59+00:00

I am trying to understand a bit better the capabilities of CanCan when it

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:21:00+00:00Added an answer on June 14, 2026 at 8:21 pm

    In the end I added this rather nice solution to the ability class:

    def multi_authorize!(*actions, message_hash)
        message = nil
        if message_hash.kind_of?(Hash) && message_hash.has_key?(:message)
          message = message_hash[:message]
        end
        auth = false
        actions.each do |act|
          auth = auth || can?(act[0], act[1])
        end
        if !auth
          message ||= unauthorized_message(actions[0][0], actions[0][1])
          raise CanCan::AccessDenied.new(message, actions[0][0], actions[0][1])
        end
    end
    

    Included an helper for the Controllers:

    module CanCanAddition
      def multi_authorize!(*args)
        @_authorized = true
        current_ability.multi_authorize!(*args)
      end
    end
    
    if defined? ActionController::Base
      ActionController::Base.class_eval do
        include ApplicationHelper::CanCanAddition
      end
    end
    

    Which I call like this:

      def create
        multi_authorize! [:create, Model1], [:authorize, Model2], :message => "You are not authorized to perform this action!"
        # other code...
      end
    

    WARNING: Due to the code in the ability class, you must provide a message or the last pair of authorization will not be passed in the *args. I’ll take some time to overcome this but the idea of the solution I think fits nice with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
just trying to understand ObjectiveC a bit better. Shouldn't this two expressions be the
Im trying to understand how class generics work and this bit just doesnt make
Im trying to write a small class to better understand bit flags in c++.
I've been trying to read and understand a bit more about better code and
I am trying to understand something a bit better with being new to C#,
I'm just trying to understand character encoding a bit better, so I'm doing a
I am trying to understand a bit about partial postbacks using updatepanels and while
I have some legacy C++ code that I am trying to understand a bit
I had a bit of a struggle trying to understand why my code was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.