Module: CommandTower::AdminScope::ApplyAuditScoping

Defined in:
lib/command_tower/admin_scope/apply_audit_scoping.rb

Class Method Summary collapse

Class Method Details

.call(relation:, scope_context:, principal:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/command_tower/admin_scope/apply_audit_scoping.rb', line 8

def call(relation:, scope_context:, principal:)
  return relation if scope_context.nil?

  registration = CommandTower.config.admin_scope.fetch(scope_context.tool_id)
  global_event_names = global_visible_event_names
  affected_user_ids = registration.affected_users_in_scope.call(
    scope_value: scope_context.scope_value,
    principal:,
    tool_id: scope_context.tool_id
  )

  host_relation = registration.narrow_audit.call(
    relation: relation.where(scope_class: CommandTower::Audit::Event::SCOPE_CLASSES[:host]),
    scope_value: scope_context.scope_value,
    principal:,
    tool_id: scope_context.tool_id
  )

  global_relation = relation.where(
    scope_class: CommandTower::Audit::Event::SCOPE_CLASSES[:global],
    action: global_event_names,
    affected_user_id: affected_user_ids
  )

  relation.where(id: host_relation.select(:id))
    .or(relation.where(id: global_relation.select(:id)))
    .where.not(scope_class: CommandTower::Audit::Event::SCOPE_CLASSES[:legacy])
end