Class: Shamu::Security::PolicyRefinement

Inherits:
Object
  • Object
show all
Defined in:
lib/shamu/security/policy_refinement.rb

Overview

Defines how an ActiveRecord::Relation is refined for an ActiveRecordPolicy.

Instance Method Summary collapse

Constructor Details

#initialize(actions, model_class, block) ⇒ PolicyRefinement

Returns a new instance of PolicyRefinement.



8
9
10
11
12
# File 'lib/shamu/security/policy_refinement.rb', line 8

def initialize( actions, model_class, block )
  @actions     = actions
  @model_class = model_class
  @block       = block
end

Instance Method Details

#apply(relation, additional_context) ⇒ ActiveRecord::Relation

Apply the refinement to the relation.

Parameters:

  • relation (ActiveRecord::Relation)

    to refine

Returns:

  • (ActiveRecord::Relation)


34
35
36
# File 'lib/shamu/security/policy_refinement.rb', line 34

def apply( relation, additional_context )
  ( block && block.call( relation, additional_context ) ) || relation
end

#match?(action, relation, additional_context) ⇒ Boolean

Determines if the refinement matches the request action permission on the given relation.

Parameters:

  • action (Symbol)

    to be performed on entities projected from the relation.

  • relation (ActiveRecord::Relation)

    to refine.

  • additional (Object)

    context offered to Shamu::Security::Policy#permit?.

Returns:

  • (Boolean)

    true if the rule is a match.



23
24
25
26
27
28
# File 'lib/shamu/security/policy_refinement.rb', line 23

def match?( action, relation, additional_context )
  return false unless actions.include? action
  return false unless model_class_match?( relation )

  true
end