Class: Authorization::AuthorizationRule
- Inherits:
-
Object
- Object
- Authorization::AuthorizationRule
- Defined in:
- lib/declarative_authorization/authorization.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
-
#join_operator ⇒ Object
readonly
Returns the value of attribute join_operator.
-
#privileges ⇒ Object
readonly
Returns the value of attribute privileges.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
-
#source_line ⇒ Object
readonly
Returns the value of attribute source_line.
Instance Method Summary collapse
- #append_attribute(attribute) ⇒ Object
- #append_privileges(privs) ⇒ Object
-
#initialize(role, privileges = [], contexts = nil, join_operator = :or, options = {}) ⇒ AuthorizationRule
constructor
A new instance of AuthorizationRule.
- #initialize_copy(from) ⇒ Object
- #matches?(roles, privs, context = nil) ⇒ Boolean
- #obligations(attr_validator) ⇒ Object
- #to_long_s ⇒ Object
- #validate?(attr_validator, skip_attribute = false) ⇒ Boolean
Constructor Details
#initialize(role, privileges = [], contexts = nil, join_operator = :or, options = {}) ⇒ AuthorizationRule
Returns a new instance of AuthorizationRule.
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/declarative_authorization/authorization.rb', line 334 def initialize (role, privileges = [], contexts = nil, join_operator = :or, = {}) @role = role @privileges = Set.new(privileges) @contexts = Set.new((contexts && !contexts.is_a?(Array) ? [contexts] : contexts)) @join_operator = join_operator @attributes = [] @source_file = [:source_file] @source_line = [:source_line] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def attributes @attributes end |
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def contexts @contexts end |
#join_operator ⇒ Object (readonly)
Returns the value of attribute join_operator.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def join_operator @join_operator end |
#privileges ⇒ Object (readonly)
Returns the value of attribute privileges.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def privileges @privileges end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def role @role end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def source_file @source_file end |
#source_line ⇒ Object (readonly)
Returns the value of attribute source_line.
331 332 333 |
# File 'lib/declarative_authorization/authorization.rb', line 331 def source_line @source_line end |
Instance Method Details
#append_attribute(attribute) ⇒ Object
355 356 357 |
# File 'lib/declarative_authorization/authorization.rb', line 355 def append_attribute (attribute) @attributes << attribute end |
#append_privileges(privs) ⇒ Object
351 352 353 |
# File 'lib/declarative_authorization/authorization.rb', line 351 def append_privileges (privs) @privileges.merge(privs) end |
#initialize_copy(from) ⇒ Object
345 346 347 348 349 |
# File 'lib/declarative_authorization/authorization.rb', line 345 def initialize_copy (from) @privileges = @privileges.clone @contexts = @contexts.clone @attributes = @attributes.collect {|attribute| attribute.clone } end |
#matches?(roles, privs, context = nil) ⇒ Boolean
359 360 361 362 363 |
# File 'lib/declarative_authorization/authorization.rb', line 359 def matches? (roles, privs, context = nil) roles = [roles] unless roles.is_a?(Array) @contexts.include?(context) and roles.include?(@role) and not (@privileges & privs).empty? end |
#obligations(attr_validator) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/declarative_authorization/authorization.rb', line 376 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_s ⇒ Object
410 411 412 |
# File 'lib/declarative_authorization/authorization.rb', line 410 def to_long_s attributes.collect {|attr| attr.to_long_s } * "; " end |
#validate?(attr_validator, skip_attribute = false) ⇒ Boolean
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/declarative_authorization/authorization.rb', line 365 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 |