CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/util/db_manager.rb
Views: 11779
1
module Msf
2
module Util
3
module DBManager
4
# Creates search conditions to match the specified search string against all of the model's columns.
5
#
6
# @param model - An ActiveRecord model object
7
# @param search - A string regex search
8
# @param column_name_skip_list - An array of strings containing column names to skip
9
# @return Arel::Nodes::Or object that represents a search of all of the model's columns
10
def self.create_all_column_search_conditions(model, search, column_name_skip_list=nil)
11
search = "(?mi)#{search}"
12
# remove skip columns
13
columns = model.columns.reject { |column|
14
column_name_skip_list && column_name_skip_list.include?(column.name)
15
}
16
17
condition_set = columns.map { |column|
18
Arel::Nodes::Regexp.new(Arel::Nodes::NamedFunction.new("CAST", [model.arel_table[column.name].as("TEXT")]),
19
Arel::Nodes.build_quoted(search))
20
}
21
Arel::Nodes::Grouping.new(condition_set.reduce { |conditions, condition| conditions.or(condition).expr })
22
end
23
24
# Processes the workspace value in the opts hash from a request. This method throws an exception if
25
# :workspace was not present but required was true, deletes the workspace from the hash, and
26
# looks up the workspace object by name, which it returns.
27
#
28
# @param [Hash] opts The opts hash passed in from the data request. Should contain :workspace if required is true.
29
# @param [Msf::Framework] framework A framework object containing a valid database connection.
30
# @param [Bool] required true if the :workspace key is required for this data operation. false if it is only optional.
31
# @raise [ArgumentError] opts must include a valid :workspace
32
# @raise [RuntimeError] couldn't find workspace
33
# @return [Mdm::Workspace] The workspace object that was referenced by name in opts.
34
def self.process_opts_workspace(opts, framework, required = true)
35
wspace = opts[:workspace]
36
if required && (wspace.nil? || (wspace.kind_of?(String) && wspace.empty?))
37
raise ArgumentError.new("opts must include a valid :workspace")
38
end
39
40
case wspace
41
when Hash
42
workspace_name = wspace[:name]
43
when String
44
workspace_name = wspace
45
when Mdm::Workspace
46
workspace_name = wspace.name
47
else
48
workspace_name = nil
49
end
50
51
wspace = framework.db.find_workspace(workspace_name) unless workspace_name.nil?
52
raise "Couldn't find workspace #{workspace_name}" if wspace.nil? && required
53
54
wspace
55
end
56
57
end
58
end
59
end
60
61