From ad62d60b200c53ac167b01c7f2832849465799f8 Mon Sep 17 00:00:00 2001 From: Ryan Ahearn Date: Tue, 10 May 2011 11:52:29 -0400 Subject: [PATCH 1/2] Fixes bug in mongoid_adapter with empty conditions hash * adds mongoid query that matches every record when rule.conditions.empty? is true --- lib/cancan/model_adapters/mongoid_adapter.rb | 4 ++-- spec/cancan/model_adapters/mongoid_adapter_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/cancan/model_adapters/mongoid_adapter.rb b/lib/cancan/model_adapters/mongoid_adapter.rb index fb45c86..bdae93e 100644 --- a/lib/cancan/model_adapters/mongoid_adapter.rb +++ b/lib/cancan/model_adapters/mongoid_adapter.rb @@ -23,14 +23,14 @@ module CanCan end def database_records - if @rules.size == 0 + if @rules.size == 0 @model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria) @rules[0].conditions else @rules.inject(@model_class.all) do |records, rule| if rule.conditions.empty? - records + records.or(:_id => {'$exists' => true}) # match everything in Mongoid elsif rule.base_behavior records.or(rule.conditions) else diff --git a/spec/cancan/model_adapters/mongoid_adapter_spec.rb b/spec/cancan/model_adapters/mongoid_adapter_spec.rb index 175a846..1ce0466 100644 --- a/spec/cancan/model_adapters/mongoid_adapter_spec.rb +++ b/spec/cancan/model_adapters/mongoid_adapter_spec.rb @@ -68,6 +68,15 @@ if ENV["MODEL_ADAPTER"] == "mongoid" MongoidProject.accessible_by(@ability, :read).entries.should == [sir] end + it "should be able to mix empty conditions and hashes" do + @ability.can :read, MongoidProject + @ability.can :read, MongoidProject, :title => 'Sir' + sir = MongoidProject.create(:title => 'Sir') + lord = MongoidProject.create(:title => 'Lord') + + MongoidProject.accessible_by(@ability, :read).count.should == 2 + end + it "should return everything when the defined ability is manage all" do @ability.can :manage, :all sir = MongoidProject.create(:title => 'Sir') From 08824502326b7c050dd8759db3850433e4e8c556 Mon Sep 17 00:00:00 2001 From: Ryan Ahearn Date: Thu, 12 May 2011 09:24:38 -0400 Subject: [PATCH 2/2] Processes can rules only if no empty conditions rules are present 1) remove all empty conditions hashes from the rules, they are included in the records through `@model_class.all` 2) only process can rules if the new and old rules lists are the same length (meaning there were no empty conditions hashes) 3) always process cannot rules --- lib/cancan/model_adapters/mongoid_adapter.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/cancan/model_adapters/mongoid_adapter.rb b/lib/cancan/model_adapters/mongoid_adapter.rb index bdae93e..7993252 100644 --- a/lib/cancan/model_adapters/mongoid_adapter.rb +++ b/lib/cancan/model_adapters/mongoid_adapter.rb @@ -28,13 +28,17 @@ module CanCan elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria) @rules[0].conditions else - @rules.inject(@model_class.all) do |records, rule| - if rule.conditions.empty? - records.or(:_id => {'$exists' => true}) # match everything in Mongoid - elsif rule.base_behavior - records.or(rule.conditions) + # we only need to process can rules if + # there are no rules with empty conditions + rules = @rules.reject { |rule| rule.conditions.empty? } + process_can_rules = @rules.count == rules.count + rules.inject(@model_class.all) do |records, rule| + if process_can_rules && rule.base_behavior + records.or rule.conditions + elsif !rule.base_behavior + records.excludes rule.conditions else - records.excludes(rule.conditions) + records end end end