Class: AuditEventService

Inherits:
Object
  • Object
show all
Includes:
AuditEventSaveType
Defined in:
app/services/audit_event_service.rb

Constant Summary

Constants included from AuditEventSaveType

AuditEventSaveType::SAVE_TYPES

Instance Method Summary collapse

Constructor Details

#initialize(author, entity, details = {}, save_type = :database_and_stream, created_at = DateTime.current) ⇒ AuditEventService

Instantiates a new service

Parameters:

  • author (User, token String)

    the entity who authors the change

  • entity (User, Project, Group)

    the scope which audit event belongs to This param is also used to determine the visibility of the audit event.

    • Project: events are visible at Project and Instance level

    • Group: events are visible at Group and Instance level

    • User: events are visible at Instance level

  • details (Hash) (defaults to: {})

    extra data of audit event

  • save_type (Symbol) (defaults to: :database_and_stream)

    the type to save the event Can be selected from the following, :database, :stream, :database_and_stream .



20
21
22
23
24
25
26
27
# File 'app/services/audit_event_service.rb', line 20

def initialize(author, entity, details = {}, save_type = :database_and_stream, created_at = DateTime.current)
  @author = build_author(author)
  @entity = entity
  @details = details
  @ip_address = resolve_ip_address(@author)
  @save_type = save_type
  @created_at = created_at
end

Instance Method Details

#for_authenticationAuditEventService

Builds the @details attribute for authentication

This uses the @author as the target object being audited

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/audit_event_service.rb', line 34

def for_authentication
  mark_as_authentication_event!

  @details = {
    with: @details[:with],
    target_id: @author.id,
    target_type: 'User',
    target_details: @author.name
  }

  self
end

#log_security_event_to_fileObject

Writes event to a file



57
58
59
# File 'app/services/audit_event_service.rb', line 57

def log_security_event_to_file
  file_logger.info(base_payload.merge(formatted_details))
end

#security_eventAuditEvent

Writes event to a file and creates an event record in DB

Returns:

  • (AuditEvent)

    persisted if saves and non-persisted if fails



50
51
52
53
54
# File 'app/services/audit_event_service.rb', line 50

def security_event
  log_security_event_to_file
  log_authentication_event_to_database
  log_security_event_to_database
end