Class: Dradis::Plugins::Projects::Upload::Template::Importer

Inherits:
Upload::Importer
  • Object
show all
Defined in:
lib/dradis/plugins/projects/upload/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lookup_tableObject

Returns the value of attribute lookup_table.



13
14
15
# File 'lib/dradis/plugins/projects/upload/template.rb', line 13

def lookup_table
  @lookup_table
end

#template_versionObject

Returns the value of attribute template_version.



13
14
15
# File 'lib/dradis/plugins/projects/upload/template.rb', line 13

def template_version
  @template_version
end

Class Method Details

.templatesObject



15
16
17
# File 'lib/dradis/plugins/projects/upload/template.rb', line 15

def self.templates
  { }
end

Instance Method Details

#import(params = {}) ⇒ Object

The import method is invoked by the framework to process a template file that has just been uploaded using the ‘Import from file…’ dialog.

This module will take the XMl export file created with the ProjectExport module and dump the contents into the current database.

Since we cannot ensure that the original node and category IDs as specified in the XML are free in this database, we need to keep a few lookup tables to maintain the original structure of Nodes and the Notes pointing to the right nodes and categories.

This method also returns the Node lookup table so callers can understand what changes to the original IDs have been applied. This is mainly for the benefit of the ProjectPackageUpload module that would use the translation table to re-associate the attachments in the project archive with the new node IDs in the current project.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dradis/plugins/projects/upload/template.rb', line 35

def import(params={})
  # load the template
  logger.info { "Loading template file from: #{params[:file]}" }
  template = Nokogiri::XML(File.read(params[:file]))
  logger.info { "Done." }

  unless template.errors.empty?
    logger.error { "Invalid project template format." }
    return false
  end


  if template.xpath('/dradis-template').empty?
    error = "The uploaded file doesn't look like a Dradis project template (/dradis-template)."
    logger.fatal{ error }
    content_service.create_note text: error
    return false
  end

  # :options contains all the options we've received from the framework.
  #
  # See:
  #   Dradis::Plugins::Upload::Importer#initialize
  Rails.application.config.dradis.projects.template_uploader.new(options)
    .parse(template)
end

#parse(template) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dradis/plugins/projects/upload/template.rb', line 62

def parse(template)
  @template_version = template.root[:version].try(:to_i) || 1
  logger.info { "Parsing Dradis template version #{template_version.inspect}" }

  parse_categories(template)
  parse_nodes(template)
  parse_issues(template)
  parse_methodologies(template)
  parse_report_content(template)
  parse_tags(template)
  finalize(template)
  # FIXME: returning this is gross
  lookup_table
rescue Exception => e
  logger.fatal { e.message }
  logger.fatal { e.backtrace } if Rails.env.development?
  return false
end