Search code examples
ruby-on-railsruby-on-rails-4devisecancancomfortable-mexican-sofa

How to use CanCan with gem controllers?


I've spent a while trying figure out the best way to authorize a controller that's from a gem with CanCan. I'm specifically using Comfortable Mexican Sofa. I have it all setup with Devise and CanCan but having trouble authorizing specific controllers from within Comfy. The closest thing I can find similar to what I'm after is in the Fortress CMS gem.

I've tried using initializers and engines to extend before_action and write a simple auth method. The only thing I can find for CanCan and Comfy is here, but it's only addressing site login not specific controllers like pages, blogs, etc.

Basically, it comes to down - how am I able to extend a gem controller so I can authenticate a user for that controller specifically?


Solution

  • On the initializer you could customize your own authorization logic:

    # Uncomment this module and `config.public_authorization` above to use custom public authorization
    module ComfyPublicAuthorization
      def authorize
        # TODO: your own authorization logic. Check params variable here
        true
      end
    end