Skip to content

Commit d677eb1

Browse files
committed
Enhance session_events query
1 parent d117e6a commit d677eb1

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

lib/msf/core/db_manager/session_event.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
module Msf::DBManager::SessionEvent
2+
DEFAULT_ORDER = :desc
3+
DEFAULT_LIMIT = 100
4+
DEFAULT_OFFSET = 0
25

6+
# Retrieves session events that are stored in the database.
7+
#
8+
# @param opts [Hash] Hash containing query key-value pairs based on the session events model.
9+
# @option opts :id [Integer] A specific session event ID. If specified, all other options are ignored.
10+
#
11+
# Additional query options:
12+
# @option opts :order [Symbol|String] The session event created_at sort order.
13+
# Valid values: :asc, :desc, 'asc' or 'desc'. Default: :desc
14+
# @option opts :limit [Integer] The maximum number of session events that will be retrieved from the query.
15+
# Default: 100
16+
# @option opts :offset [Integer] The number of session events the query will begin reading from the start
17+
# of the set. Default: 0
18+
# @option opts :search_term [String] Search regular expression used to filter results.
19+
# All fields are converted to strings and results are returned if the pattern is matched.
20+
# @return [Array<Mdm::SessionEvent>] session events that are matched.
321
def session_events(opts)
422
::ActiveRecord::Base.connection_pool.with_connection {
523
# If we have the ID, there is no point in creating a complex query.
624
if opts[:id] && !opts[:id].to_s.empty?
725
return Array.wrap(Mdm::SessionEvent.find(opts[:id]))
826
end
9-
conditions = {}
1027

11-
Mdm::SessionEvent.all
28+
# Passing workspace keys to the search will cause exceptions, so remove them if they were accidentally included.
29+
Msf::Util::DBManager.delete_opts_workspace(opts)
30+
31+
order = opts.delete(:order)
32+
order = order.nil? ? DEFAULT_ORDER : order.to_sym
33+
34+
limit = opts.delete(:limit) || DEFAULT_LIMIT
35+
offset = opts.delete(:offset) || DEFAULT_OFFSET
36+
37+
search_term = opts.delete(:search_term)
38+
results = Mdm::SessionEvent.where(opts).order(created_at: order).offset(offset).limit(limit)
39+
40+
if search_term && !search_term.empty?
41+
re_search_term = /#{search_term}/mi
42+
results = results.select { |event|
43+
event.attribute_names.any? { |a| event[a.intern].to_s.match(re_search_term) }
44+
}
45+
end
46+
results
1247
}
1348
end
49+
1450
#
1551
# Record a session event in the database
1652
#

0 commit comments

Comments
 (0)