535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
# File 'lib/declarative_authorization/authorization.rb', line 535
def validate?(attr_validator, object = nil, hash = nil)
object ||= attr_validator.object
return false unless object
if Authorization.is_a_association_proxy?(object) && object.respond_to?(:empty?)
return false if object.empty?
object.each do |member|
return true if validate?(attr_validator, member, hash)
end
return false
end
(hash || @conditions_hash).all? do |attr, value|
attr_value = object_attribute_value(object, attr)
if value.is_a?(Hash)
if attr_value.is_a?(Enumerable)
attr_value.any? do |inner_value|
validate?(attr_validator, inner_value, value)
end
elsif attr_value == nil
raise NilAttributeValueError, "Attribute #{attr.inspect} is nil in #{object.inspect}."
else
validate?(attr_validator, attr_value, value)
end
elsif value.is_a?(Array) and value.length == 2 and value.first.is_a?(Symbol)
evaluated = if value[1].is_a?(Proc)
attr_validator.evaluate(value[1])
else
value[1]
end
case value[0]
when :is
attr_value == evaluated
when :is_not
attr_value != evaluated
when :contains
begin
attr_value.include?(evaluated)
rescue NoMethodError => e
raise AuthorizationUsageError, "Operator contains requires a " +
"subclass of Enumerable as attribute value, got: #{attr_value.inspect} " +
"contains #{evaluated.inspect}: #{e}"
end
when :does_not_contain
begin
!attr_value.include?(evaluated)
rescue NoMethodError => e
raise AuthorizationUsageError, "Operator does_not_contain requires a " +
"subclass of Enumerable as attribute value, got: #{attr_value.inspect} " +
"does_not_contain #{evaluated.inspect}: #{e}"
end
when :intersects_with
begin
!(evaluated.to_set & attr_value.to_set).empty?
rescue NoMethodError => e
raise AuthorizationUsageError, "Operator intersects_with requires " +
"subclasses of Enumerable, got: #{attr_value.inspect} " +
"intersects_with #{evaluated.inspect}: #{e}"
end
when :is_in
begin
evaluated.include?(attr_value)
rescue NoMethodError => e
raise AuthorizationUsageError, "Operator is_in requires a " +
"subclass of Enumerable as value, got: #{attr_value.inspect} " +
"is_in #{evaluated.inspect}: #{e}"
end
when :is_not_in
begin
!evaluated.include?(attr_value)
rescue NoMethodError => e
raise AuthorizationUsageError, "Operator is_not_in requires a " +
"subclass of Enumerable as value, got: #{attr_value.inspect} " +
"is_not_in #{evaluated.inspect}: #{e}"
end
when :lt
attr_value && attr_value < evaluated
when :lte
attr_value && attr_value <= evaluated
when :gt
attr_value && attr_value > evaluated
when :gte
attr_value && attr_value >= evaluated
when :id_in_scope
evaluated.exists?(attr_value)
else
raise AuthorizationError, "Unknown operator #{value[0]}"
end
else
raise AuthorizationError, "Wrong conditions hash format"
end
end
end
|