added cannot support and multiple can support
This commit is contained in:
		
							parent
							
								
									344832d199
								
							
						
					
					
						commit
						55c8a5045b
					
				@ -16,14 +16,22 @@ module CanCan
 | 
				
			|||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def database_records
 | 
					      def database_records
 | 
				
			||||||
        @model_class.where(conditions)
 | 
					        if @rules.size == 0  
 | 
				
			||||||
      end
 | 
					          @model_class.where(false_query)
 | 
				
			||||||
 | 
					 | 
				
			||||||
      def conditions
 | 
					 | 
				
			||||||
        if @rules.size == 0
 | 
					 | 
				
			||||||
          false_query
 | 
					 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
          @rules.first.conditions
 | 
					          criteria = @model_class.all
 | 
				
			||||||
 | 
					          @rules.each do |rule|
 | 
				
			||||||
 | 
					            criteria = chain_criteria(rule, criteria)
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					          criteria
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      def chain_criteria rule, criteria
 | 
				
			||||||
 | 
					        if rule.base_behavior
 | 
				
			||||||
 | 
					          criteria.or(rule.conditions)
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          criteria.excludes(rule.conditions)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -56,7 +56,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
				
			|||||||
        lord  = MongoidProject.create(:title => 'Lord')
 | 
					        lord  = MongoidProject.create(:title => 'Lord')
 | 
				
			||||||
        dude  = MongoidProject.create(:title => 'Dude')
 | 
					        dude  = MongoidProject.create(:title => 'Dude')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        MongoidProject.accessible_by(@ability, :read).should == [sir]
 | 
					        MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it "should return everything when the defined ability is manage all" do
 | 
					      it "should return everything when the defined ability is manage all" do
 | 
				
			||||||
@ -154,7 +154,24 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
				
			|||||||
        @ability.can :read, MongoidProject, :foo => {:bar => 1}
 | 
					        @ability.can :read, MongoidProject, :foo => {:bar => 1}
 | 
				
			||||||
        MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
 | 
					        MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      it "should exclude from the result if set to cannot" do
 | 
				
			||||||
 | 
					        obj = MongoidProject.create(:bar => 1)
 | 
				
			||||||
 | 
					        obj2 = MongoidProject.create(:bar => 2)
 | 
				
			||||||
 | 
					        @ability.can :read, MongoidProject
 | 
				
			||||||
 | 
					        @ability.cannot :read, MongoidProject, :bar => 2
 | 
				
			||||||
 | 
					        MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it "should combine the rules" do
 | 
				
			||||||
 | 
					        obj = MongoidProject.create(:bar => 1)
 | 
				
			||||||
 | 
					        obj2 = MongoidProject.create(:bar => 2)
 | 
				
			||||||
 | 
					        obj3 = MongoidProject.create(:bar => 3)
 | 
				
			||||||
 | 
					        @ability.can :read, MongoidProject, :bar => 1
 | 
				
			||||||
 | 
					        @ability.can :read, MongoidProject, :bar => 2
 | 
				
			||||||
 | 
					        MongoidProject.accessible_by(@ability, :read).entries.should =~ [obj, obj2]
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
      it "should not allow to fetch records when ability with just block present" do
 | 
					      it "should not allow to fetch records when ability with just block present" do
 | 
				
			||||||
        @ability.can :read, MongoidProject do
 | 
					        @ability.can :read, MongoidProject do
 | 
				
			||||||
          false
 | 
					          false
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user