Class: Dradis::Plugins::CSV::MappingForm

Inherits:
Object
  • Object
show all
Defined in:
lib/dradis/plugins/csv/mapping_form.rb

Overview

Converts the column mapper's form structure into persisted Mapping records, one per entity (issue/evidence), so a CSV format only needs to be mapped once: #save is the forward direction (form -> records), run the first time a format is mapped. Later uploads of a recognized format read straight from those records instead (see Importer#mapped_fields), rather than reconstructing the form.

Constant Summary collapse

ENTITY_TYPES =

Mapper column types that produce fields for each entity: identifier columns are stored with the issue mapping, node columns with evidence.

{
  issue: %w[issue identifier],
  evidence: %w[evidence node]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination:, headers:, rtp_fields:) ⇒ MappingForm

rtp_fields is { issue: [...field names...], evidence: [...] }: the destination fields actually defined on the RTP, used to gate #save (see below).



21
22
23
24
25
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 21

def initialize(destination:, headers:, rtp_fields:)
  @destination = destination
  @headers = headers
  @rtp_fields = rtp_fields
end

Instance Attribute Details

#column_mappingsObject (readonly)

Returns the value of attribute column_mappings.



16
17
18
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 16

def column_mappings
  @column_mappings
end

#destinationObject (readonly)

Returns the value of attribute destination.



16
17
18
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 16

def destination
  @destination
end

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 16

def headers
  @headers
end

#rtp_fieldsObject (readonly)

Returns the value of attribute rtp_fields.



16
17
18
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 16

def rtp_fields
  @rtp_fields
end

Instance Method Details

#save(column_mappings:) ⇒ Object

Persists one Mapping per entity that has assigned columns, so a newly-mapped CSV format is recognized automatically on future uploads (see Importer#import). Only ever called for formats with no existing mapping: Importer#import and UploadController#new short-circuit before the mapper is reached once a format already has a saved mapping.

column_mappings is the mapper form submission, keyed by column index:

{
'0' => { 'type' => 'node' },
'1' => { 'type' => 'issue', 'field' => 'Title' },
'2' => { 'type' => 'identifier' },
'3' => { 'type' => 'evidence', 'field' => 'Port' }
}

An entity with no RTP fields defined has nowhere valid to point a mapping, so it's skipped entirely (including its identifier/node column, if any) rather than saving a mapping with a bogus destination.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dradis/plugins/csv/mapping_form.rb', line 44

def save(column_mappings:)
  @column_mappings = column_mappings

  return if destination.blank?

  ::Mapping.transaction do
    ENTITY_TYPES.keys.each do |entity|
      next if rtp_fields[entity].blank?

      fields = fields_for(entity)
      next if fields.empty?

      source = Dradis::Plugins::CSV.mapping_source(headers: headers, entity: entity)
      mapping = ::Mapping.create!(component: component, source: source, destination: destination)
      mapping.mapping_fields.create!(fields)
    end
  end
end