Class: Reeve::Authorization::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/reeve/authorization/registry.rb

Overview

Every guard_with declaration in the process, keyed by tool class.

Two lookups matter: by class, which the DSL and inheritance use, and by tool name, which is all the envelope has. Enumeration is what lets the compliance suite ask the question that matters — "is every tool this application exposes actually guarded?"

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



14
15
16
17
18
# File 'lib/reeve/authorization/registry.rb', line 14

def initialize
  @declarations = {}
  @name_index = nil # invalidated on every add; see #name_index
  @mutex = Mutex.new
end

Instance Method Details

#add(declaration) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/reeve/authorization/registry.rb', line 35

def add(declaration)
  @mutex.synchronize do
    @declarations[declaration.tool_class] = declaration
    @name_index = nil
    declaration
  end
end

#each(&block) ⇒ Object



60
61
62
# File 'lib/reeve/authorization/registry.rb', line 60

def each(&block)
  @declarations.values.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/reeve/authorization/registry.rb', line 68

def empty?
  @declarations.empty?
end

#for_class(tool_class) ⇒ Object

Walks the ancestry, so a subclass inherits its parent's guard and may override it simply by declaring its own.



45
46
47
48
49
50
51
52
53
# File 'lib/reeve/authorization/registry.rb', line 45

def for_class(tool_class)
  return nil unless tool_class.respond_to?(:ancestors)

  tool_class.ancestors.each do |ancestor|
    declaration = @declarations[ancestor]
    return declaration if declaration
  end
  nil
end

#guard_for(tool_name) ⇒ Object

The envelope's lookup. Returns nil for an unknown tool, which is a denial.



56
57
58
# File 'lib/reeve/authorization/registry.rb', line 56

def guard_for(tool_name)
  name_index[tool_name.to_s]
end

#register(tool_class:, policy:, action: nil, redacted_arguments: []) ⇒ Object

The DSL's guard_with. Declaring twice on one class is a mistake worth naming; add is the quiet path used by redact and by inheritance, which refine an existing declaration rather than compete with it.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reeve/authorization/registry.rb', line 23

def register(tool_class:, policy:, action: nil, redacted_arguments: [])
  warn_about_redeclaration_of(tool_class)

  declaration = Declaration.new(
    tool_class: tool_class,
    policy: policy,
    action: action || Reeve.config.default_action,
    redacted_arguments: redacted_arguments
  )
  add(declaration)
end

#remove(tool_class) ⇒ Object

Forgets one tool. Test suites build throwaway tools, and the compliance suite walks this registry — a fixture left behind fails a later, unrelated example.



74
75
76
77
78
79
# File 'lib/reeve/authorization/registry.rb', line 74

def remove(tool_class)
  @mutex.synchronize do
    @declarations.delete(tool_class)
    @name_index = nil
  end
end

#reset!Object



81
82
83
84
85
86
# File 'lib/reeve/authorization/registry.rb', line 81

def reset!
  @mutex.synchronize do
    @declarations = {}
    @name_index = nil
  end
end

#sizeObject



64
65
66
# File 'lib/reeve/authorization/registry.rb', line 64

def size
  @declarations.size
end