Class: Authorization::AuthorizationRule

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_authorization/authorization.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, privileges = [], contexts = nil, join_operator = :or, options = {}) ⇒ AuthorizationRule

Returns a new instance of AuthorizationRule.



489
490
491
492
493
494
495
496
497
498
# File 'lib/declarative_authorization/authorization.rb', line 489

def initialize(role, privileges = [], contexts = nil, join_operator = :or,
      options = {})
  @role = role
  @privileges = Set.new(privileges)
  @contexts = Set.new((contexts && !contexts.is_a?(Array) ? [contexts] : contexts))
  @join_operator = join_operator
  @attributes = []
  @source_file = options[:source_file]
  @source_line = options[:source_line]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def attributes
  @attributes
end

#contextsObject (readonly)

Returns the value of attribute contexts.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def contexts
  @contexts
end

#join_operatorObject (readonly)

Returns the value of attribute join_operator.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def join_operator
  @join_operator
end

#privilegesObject (readonly)

Returns the value of attribute privileges.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def privileges
  @privileges
end

#roleObject (readonly)

Returns the value of attribute role.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def role
  @role
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def source_file
  @source_file
end

#source_lineObject (readonly)

Returns the value of attribute source_line.



486
487
488
# File 'lib/declarative_authorization/authorization.rb', line 486

def source_line
  @source_line
end

Instance Method Details

#append_attribute(attribute) ⇒ Object



510
511
512
# File 'lib/declarative_authorization/authorization.rb', line 510

def append_attribute(attribute)
  @attributes << attribute
end

#append_privileges(privs) ⇒ Object



506
507
508
# File 'lib/declarative_authorization/authorization.rb', line 506

def append_privileges(privs)
  @privileges.merge(privs)
end

#initialize_copy(from) ⇒ Object



500
501
502
503
504
# File 'lib/declarative_authorization/authorization.rb', line 500

def initialize_copy(from)
  @privileges = @privileges.clone
  @contexts = @contexts.clone
  @attributes = @attributes.collect {|attribute| attribute.clone }
end

#matches?(roles, privs, context = nil) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
517
# File 'lib/declarative_authorization/authorization.rb', line 514

def matches?(roles, privs, context = nil)
  roles = Hash[[*roles].map { |r| [r, true] }] unless roles.is_a?(Hash)
  @contexts.include?(context) && roles.include?(@role) && privs.any? { |priv| @privileges.include?(priv) }
end

#obligations(attr_validator) ⇒ Object



530
531
532
533
534
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
# File 'lib/declarative_authorization/authorization.rb', line 530

def obligations(attr_validator)
  exceptions = []
  obligations = @attributes.collect do |attr|
    begin
      attr.obligation(attr_validator)
    rescue NotAuthorized => e
      exceptions << e
      nil
    end
  end

  if exceptions.length > 0 and (@join_operator == :and or exceptions.length == @attributes.length)
    raise NotAuthorized, "Missing authorization in collecting obligations: #{exceptions.map(&:to_s) * ", "}"
  end

  if @join_operator == :and and !obligations.empty?
    # cross product of OR'ed obligations in arrays
    arrayed_obligations = obligations.map {|obligation| obligation.is_a?(Hash) ? [obligation] : obligation}
    merged_obligations = arrayed_obligations.first
    arrayed_obligations[1..-1].each do |inner_obligations|
      previous_merged_obligations = merged_obligations
      merged_obligations = inner_obligations.collect do |inner_obligation|
        previous_merged_obligations.collect do |merged_obligation|
          merged_obligation.deep_merge(inner_obligation)
        end
      end.flatten
    end
    obligations = merged_obligations
  else
    obligations = obligations.flatten.compact
  end
  obligations.empty? ? [{}] : obligations
end

#to_long_sObject



564
565
566
# File 'lib/declarative_authorization/authorization.rb', line 564

def to_long_s
  attributes.collect {|attr| attr.to_long_s } * "; "
end

#validate?(attr_validator, skip_attribute = false) ⇒ Boolean

Returns:

  • (Boolean)


519
520
521
522
523
524
525
526
527
528
# File 'lib/declarative_authorization/authorization.rb', line 519

def validate?(attr_validator, skip_attribute = false)
  skip_attribute or @attributes.empty? or
    @attributes.send(@join_operator == :and ? :all? : :any?) do |attr|
      begin
        attr.validate?(attr_validator)
      rescue NilAttributeValueError => e
        nil # Bumping up against a nil attribute value flunks the rule.
      end
    end
end