|
1 | 1 | module Msf::DBManager::SessionEvent |
| 2 | + DEFAULT_ORDER = :desc |
| 3 | + DEFAULT_LIMIT = 100 |
| 4 | + DEFAULT_OFFSET = 0 |
2 | 5 |
|
| 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. |
3 | 21 | def session_events(opts) |
4 | 22 | ::ActiveRecord::Base.connection_pool.with_connection { |
5 | 23 | # If we have the ID, there is no point in creating a complex query. |
6 | 24 | if opts[:id] && !opts[:id].to_s.empty? |
7 | 25 | return Array.wrap(Mdm::SessionEvent.find(opts[:id])) |
8 | 26 | end |
9 | | - conditions = {} |
10 | 27 |
|
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 |
12 | 47 | } |
13 | 48 | end |
| 49 | + |
14 | 50 | # |
15 | 51 | # Record a session event in the database |
16 | 52 | # |
|
0 commit comments