ryanb / cancan

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

DSL proposal for allowing negative conditions and shared conditions between abiltiies #803

Open dieb opened 11 years ago

dieb commented 11 years ago

Hello!

Firstly thanks for all the work on cancan. This is an awesome project and I'm having a great time using it.

This is more a suggestion than an issue. I'd like to gather your input if this is too much or a nice-to-have for cancan. I have two requirements on my current project.

Defining an ability with a condition along with a negative condition
# This user can assign a name to anyone from his group, except himself
can :assign_name, User, group_id: user.group.id
cannot :assign_name, User, user_id: user.id

This is something I've to do more than once. It'd be really cool if I could do:

can :assign_name, User, conditions: { group_id: user.group.id },
                        except: { user_id: user.id }

That is, pass a negative condition block. Under the hood I figure we can just use the negative block to create an equivalent to the cannot condition.

Multiple abilities sharing the same condition

I've been writing lots of rules that require the same condition to be applied. So code currently looks like this:

can [:something, :something_else], User, group_id: user.group.id
can :some_other_thing, Orange, group_id: user.group.id
can :some_other_stuff, Apple, group_id: user.group.id

So I was thinking about the possibility of having something like:

restrict_to group_id: user.group_id do |can|
  can [:something, :something_else], User
  can :some_other_thing, Orange
  can :some_other_stuff, Apple
end

From your experience, are any of those suggestions interesting and worth pursuing?

Thanks!

xhoy commented 10 years ago

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013. Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!