Module: Legion::API::Routes::IdentityAudit

Defined in:
lib/legion/api/identity_audit.rb

Defined Under Namespace

Modules: IdentityAuditHelpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/api/identity_audit.rb', line 7

def self.registered(app)
  app.helpers IdentityAuditHelpers

  app.get '/api/identity/audit' do
    halt 503, json_error('unavailable', 'audit records not available') unless defined?(Legion::Data::Model::AuditRecord)

    dataset = Legion::Data::Model::AuditRecord.where(entity_type: 'identity')

    principal = params[:principal]
    dataset = dataset.where(Sequel.lit("metadata->>'principal' = ?", principal)) if principal

    since = params[:since]
    if since
      duration = parse_since_duration(since)
      dataset = dataset.where { created_at >= Time.now - duration } if duration
    end

    records = dataset.order(Sequel.desc(:created_at)).limit(100).all
    json_collection(records.map do |r|
      { id: r.id, action: r.action, entity_type: r.entity_type, metadata: r., created_at: r.created_at }
    end)
  end
end