Yuriy Kolodovskyy
Yuriy Kolodovskyy

Reputation: 408

CanCan abilities and additional rules by engine

I have application and engines (gems, Rails::Engine). CanCan used for authorization and I want to use it at engines. Engines isolated with namespace, but for example:

module MyEngine
  class ApplicationController < ::ApplicationController
  end
end

So, I can use load_and_authorize_resource at controllers (must specify model class name) and 'can' helper also. All abilities I must write to Ability at main application (models from engines must be namespaced). It is not nice way. I want specify abilities for engine at this engine, but without create new ability object. How can I do this? Any idea?

Upvotes: 0

Views: 839

Answers (1)

Chiubaka
Chiubaka

Reputation: 789

It wasn't super pretty, but I did something similar with a Rails application I'm writing currently. I have written my own engine inside this application called FormX, which is sort of a form CMS engine. I wanted users to have the ability to only edit certain responses to forms, and only some users to have the ability to create forms.

Given that I had a Form model in the Formx namespace, I was able to define abilities in my MainApp ability.rb file by doing using Formx::Form syntax.

Ex.

can :manage, Formx::Form

Then in my controller I had to manually place the authorize! calls in each action, as I couldn't get load_and_authorize_resource to work within the engine's namespace.

Upvotes: 2

Related Questions