Can any? #922

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
3 participants

sadjow commented Aug 20, 2013

Check if the user has any of the permissions on any of the resources

can_any? [:create, :destroy], [Project, User]

Return true if any of the actions is accepted for any of the
resources. You can check for one action in several resources, like:

can_any? :update, [Project, User]

Return true if the user has :update permission on Project or on User
Or, you can check for several actions on a single resource.

can_any? [:update, :destroy], Project

Return true if the current user any of the permissions on the given
resource.

Or even checks a single permission on a single resource

can_any? :update, Project

😃

sadjow commented Aug 20, 2013

Display a item based on a reverse hierarchy.

Like a display of a parent menu (like dropdown) if the user has any permission for any of the children. Then, the system checks for each child resource for display or not that child item on that menu. At least the system will know that has one to display.

But, this can be applied to several situations.

@halan halan commented on the diff Sep 2, 2013

lib/cancan/ability.rb
+ # resources. You can check for one action in several resources, like:
+ #
+ # can_any? :update, [Project, User]
+ #
+ # Return true if the user has :update permission on Project or on User
+ # Or, you can check for several actions on a single resource.
+ #
+ # can_any? [:update, :destroy], Project
+ #
+ # Return true if the current user any of the permissions on the given
+ # resource.
+ #
+ # You can also see the RSpec Matchers on the #can_any? section.
+ def can_any?(action, subject, *extra_args)
+ subject = [subject] unless subject.is_a? Array
+ action = [action] unless action.is_a? Array
@halan

halan Sep 2, 2013

subjects = [*subject]
actions = [*action]

it's enough. dont need ask for the type.

@sadjow

sadjow Sep 2, 2013

👍 I'll change.

halan commented Sep 2, 2013

You can group actions with aliases.

done that...

Might be nice to make a helper (in your app) do:

 [User, Project].any? { |resource| can? :something, resource }

Something like:

def can_any? role, resources
  [*resources].any? { |resource| can? role, resource }
end

sadjow commented Sep 2, 2013

@halan I want to DRY it including a helper into cancan. :) Because of that I've made a PR.

sadjow commented Sep 2, 2013

Thanks for the suggestion.

halan commented Sep 2, 2013

You can Dont Repeat Yourself just putting these 3 lines on a helper in your app. More KISS.

xhoy commented Jul 1, 2014

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!

sadjow closed this Oct 29, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment