debian-mirror-gitlab/app/services/search_service.rb

134 lines
3.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class SearchService
include Gitlab::Allowable
2020-03-13 15:44:24 +05:30
SEARCH_TERM_LIMIT = 64
SEARCH_CHAR_LIMIT = 4096
2020-05-24 23:13:21 +05:30
DEFAULT_PER_PAGE = Gitlab::SearchResults::DEFAULT_PER_PAGE
MAX_PER_PAGE = 200
2017-08-17 22:00:37 +05:30
def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def project
return @project if defined?(@project)
@project =
if params[:project_id].present?
the_project = Project.find_by(id: params[:project_id])
2017-09-10 17:25:29 +05:30
can?(current_user, :read_project, the_project) ? the_project : nil
2017-08-17 22:00:37 +05:30
else
nil
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def group
return @group if defined?(@group)
@group =
if params[:group_id].present?
the_group = Group.find_by(id: params[:group_id])
can?(current_user, :read_group, the_group) ? the_group : nil
else
nil
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def show_snippets?
return @show_snippets if defined?(@show_snippets)
@show_snippets = params[:snippets] == 'true'
end
2020-03-13 15:44:24 +05:30
def valid_query_length?
params[:search].length <= SEARCH_CHAR_LIMIT
end
def valid_terms_count?
params[:search].split.count { |word| word.length >= 3 } <= SEARCH_TERM_LIMIT
end
2017-08-17 22:00:37 +05:30
delegate :scope, to: :search_service
def search_results
@search_results ||= search_service.execute
end
2020-06-23 00:09:42 +05:30
def search_objects(preload_method = nil)
@search_objects ||= redact_unauthorized_results(search_results.objects(scope, page: params[:page], per_page: per_page, preload_method: preload_method))
2020-04-08 14:13:33 +05:30
end
2017-08-17 22:00:37 +05:30
private
2020-05-24 23:13:21 +05:30
def per_page
per_page_param = params[:per_page].to_i
2020-10-24 23:57:45 +05:30
return DEFAULT_PER_PAGE unless per_page_param > 0
2020-05-24 23:13:21 +05:30
[MAX_PER_PAGE, per_page_param].min
end
2020-04-08 14:13:33 +05:30
def visible_result?(object)
return true unless object.respond_to?(:to_ability_name) && DeclarativePolicy.has_policy?(object)
Ability.allowed?(current_user, :"read_#{object.to_ability_name}", object)
end
2020-04-22 19:07:51 +05:30
def redact_unauthorized_results(results_collection)
2020-06-23 00:09:42 +05:30
redacted_results = results_collection.reject { |object| visible_result?(object) }
if redacted_results.any?
redacted_log = redacted_results.each_with_object({}) do |object, memo|
memo[object.id] = { ability: :"read_#{object.to_ability_name}", id: object.id, class_name: object.class.name }
end
log_redacted_search_results(redacted_log.values)
2020-04-08 14:13:33 +05:30
2020-06-23 00:09:42 +05:30
return results_collection.id_not_in(redacted_log.keys) if results_collection.is_a?(ActiveRecord::Relation)
2020-04-08 14:13:33 +05:30
end
2020-06-23 00:09:42 +05:30
return results_collection if results_collection.is_a?(ActiveRecord::Relation)
2020-04-08 14:13:33 +05:30
2020-06-23 00:09:42 +05:30
permitted_results = results_collection - redacted_results
2020-04-08 14:13:33 +05:30
Kaminari.paginate_array(
permitted_results,
2020-04-22 19:07:51 +05:30
total_count: results_collection.total_count,
limit: results_collection.limit_value,
offset: results_collection.offset_value
2020-04-08 14:13:33 +05:30
)
end
def log_redacted_search_results(filtered_results)
logger.error(message: "redacted_search_results", filtered: filtered_results, current_user_id: current_user&.id, query: params[:search])
end
def logger
@logger ||= ::Gitlab::RedactedSearchResultsLogger.build
end
2017-08-17 22:00:37 +05:30
def search_service
@search_service ||=
if project
Search::ProjectService.new(project, current_user, params)
elsif show_snippets?
Search::SnippetService.new(current_user, params)
elsif group
Search::GroupService.new(current_user, group, params)
else
Search::GlobalService.new(current_user, params)
end
end
attr_reader :current_user, :params
end
2019-12-04 20:38:33 +05:30
SearchService.prepend_if_ee('EE::SearchService')