Module: Msf::Util::DBManager

Defined in:
lib/msf/util/db_manager.rb

Class Method Summary collapse

Class Method Details

.create_all_column_search_conditions(model, search, column_name_skip_list = nil) ⇒ Object

Creates search conditions to match the specified search string against all of the model’s columns.

Parameters:

  • model
    • An ActiveRecord model object

  • search
    • A string regex search

  • column_name_skip_list (defaults to: nil)
    • An array of strings containing column names to skip

Returns:

  • Arel::Nodes::Or object that represents a search of all of the model’s columns



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/msf/util/db_manager.rb', line 10

def self.create_all_column_search_conditions(model, search, column_name_skip_list=nil)
  search = "(?mi)#{search}"
  # remove skip columns
  columns = model.columns.reject { |column|
    column_name_skip_list && column_name_skip_list.include?(column.name)
  }

  condition_set = columns.map { |column|
    Arel::Nodes::Regexp.new(Arel::Nodes::NamedFunction.new("CAST", [model.arel_table[column.name].as("TEXT")]),
                            Arel::Nodes.build_quoted(search))
  }
  Arel::Nodes::Grouping.new(condition_set.reduce { |conditions, condition| conditions.or(condition).expr })
end

.process_opts_workspace(opts, framework, required = true) ⇒ Mdm::Workspace

Processes the workspace value in the opts hash from a request. This method throws an exception if :workspace was not present but required was true, deletes the workspace from the hash, and looks up the workspace object by name, which it returns.

Parameters:

  • opts (Hash)

    The opts hash passed in from the data request. Should contain :workspace if required is true.

  • framework (Msf::Framework)

    A framework object containing a valid database connection.

  • required (Bool) (defaults to: true)

    true if the :workspace key is required for this data operation. false if it is only optional.

Returns:

  • (Mdm::Workspace)

    The workspace object that was referenced by name in opts.

Raises:

  • (ArgumentError)

    opts must include a valid :workspace

  • (RuntimeError)

    couldn’t find workspace



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/msf/util/db_manager.rb', line 34

def self.process_opts_workspace(opts, framework, required = true)
  wspace = opts[:workspace]
  if required && (wspace.nil? || (wspace.kind_of?(String) && wspace.empty?))
    raise ArgumentError.new("opts must include a valid :workspace")
  end

  case wspace
  when Hash
    workspace_name = wspace[:name]
  when String
    workspace_name = wspace
  when Mdm::Workspace
    workspace_name = wspace.name
  else
    workspace_name = nil
  end

  wspace = framework.db.find_workspace(workspace_name) unless workspace_name.nil?
  raise "Couldn't find workspace #{workspace_name}" if wspace.nil? && required

  wspace
end