Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage accessible_by from can? when possible #813

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/cancan/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,26 @@ module Ability
# Also see the RSpec Matchers to aid in testing.
def can?(action, subject, attribute = nil, *extra_args)
match = extract_subjects(subject).lazy.map do |a_subject|
rules_for_query = rules_except_cannot_with_attributes(action, a_subject)
subject_class = subject_class?(a_subject) ? a_subject : a_subject.class
if can_use_accessible_by?(subject_class, a_subject, rules_for_query)
next subject_class.accessible_by(self, action).exists?(a_subject.send(subject_class.primary_key)) ? rules_for_query.first : nil
end

relevant_rules_for_match(action, a_subject).detect do |rule|
rule.matches_conditions?(action, a_subject, attribute, *extra_args) && rule.matches_attributes?(attribute)
end
end.reject(&:nil?).first
match ? match.base_behavior : false
end

def can_use_accessible_by?(subject_class, subject, rules)
subject_class.respond_to?(:accessible_by) &&
defined?(ActiveRecord::Base) && subject.is_a?(ActiveRecord::Base) &&
!subject.new_record? &&
rules.none?(&:only_block?)
end

# Convenience method which works the same as "can?" but returns the opposite value.
#
# cannot? :destroy, @project
Expand Down
12 changes: 8 additions & 4 deletions lib/cancan/ability/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ def relevant_rules_for_match(action, subject)
end

def relevant_rules_for_query(action, subject)
rules = relevant_rules(action, subject).reject do |rule|
# reject 'cannot' rules with attributes when doing queries
rule.base_behavior == false && rule.attributes.present?
end
rules = rules_except_cannot_with_attributes(action, subject)
if rules.any?(&:only_block?)
raise Error, "The accessible_by call cannot be used with a block 'can' definition." \
"The SQL cannot be determined for #{action.inspect} #{subject.inspect}"
end
rules
end

def rules_except_cannot_with_attributes(action, subject)
relevant_rules(action, subject).reject do |rule|
# reject 'cannot' rules with attributes when doing queries
rule.base_behavior == false && rule.attributes.present?
end
end

# Optimizes the order of the rules, so that rules with the :all subject are evaluated first.
def optimize_order!(rules)
first_can_in_group = -1
Expand Down