ryanb / cancan

Authorization Gem for Ruby on Rails.
MIT License
6.27k stars 783 forks source link

How cancan will work with multiple models on single controller ? #996

Open rajesh2210 opened 10 years ago

rajesh2210 commented 10 years ago

Rails 4 + Cancan

Q. How to give authorize_resource at controller for multiple models ?

Example

ability.rb if user.is_sys_owner? can :manage, [MdmArea, MdmCity, MdmCountry, MdmState] end

class Admin::MdmController < ApplicationController authorize_resource :class => ['MdmArea', ' MdmCity', 'MdmCountry', 'MdmState'] # Not Working?

other code

end

zamith commented 10 years ago

Hi @rajesh2210, you're looking at it wrong. You can't authorize multiple models, as the name indicates authorize_resource authorizes one resource that it infers from the controller name (unless you pass it a class, not an array of classes). In your example I guess it would be Mdm.

You have a couple of options here:

  1. You can have a controller for each type of Mdm and authorize each one of those.
  2. Can authorize the Mdm constant (if there is one) only.
  3. You can authorize each of the controller action individually, instead of using the authorize_resource helper method.

Those are the options I can think of right now, but I'm sure there are others. But remember, you can only authorize one resource at a time.

xhoy commented 10 years ago

Dear submitter, Since cancan/raynB hasn't been active for more than 6 months and no body else then ryam himself has commit permissions the cancan project is on a stand still. Since cancan has several issues including missing support for rails 4 cancan is moving forward to cancancan. More details on: #994

If your feel that your pull request or bug is still applicable (and hasn't been merged in to cancan) it would be really appreciated if you would resubmit it to cancancan (https://github.com/cancancommunity/cancancan)

We hope to see you on the other side!