Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 385
nick Avatar asked Nov 04 '15 18:11

nick


People also ask

Can Can Rails gem?

CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the Ability class) and not duplicated across controllers, views, and database queries.

Can Can Can Ruby on Rails?

CanCanCan is an authorization library for Ruby and Ruby on Rails which restricts what resources a given user is allowed to access.


1 Answers

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
like image 113
Mario Pérez Avatar answered Sep 21 '22 21:09

Mario Pérez