debian-mirror-gitlab/app/controllers/search_controller.rb

165 lines
4.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class SearchController < ApplicationController
2018-03-27 19:54:05 +05:30
include ControllerWithCrossProjectAccessCheck
2014-09-02 18:07:02 +05:30
include SearchHelper
2018-03-17 18:26:18 +05:30
include RendersCommits
2020-11-24 15:15:51 +05:30
include RedisTracking
2014-09-02 18:07:02 +05:30
2020-06-23 00:09:42 +05:30
SCOPE_PRELOAD_METHOD = {
2020-10-24 23:57:45 +05:30
projects: :with_web_entity_associations,
2021-01-03 14:25:43 +05:30
issues: :with_web_entity_associations,
2021-01-29 00:20:46 +05:30
merge_requests: :with_web_entity_associations,
2021-01-03 14:25:43 +05:30
epics: :with_web_entity_associations
2020-06-23 00:09:42 +05:30
}.freeze
2020-11-24 15:15:51 +05:30
track_redis_hll_event :show, name: 'i_search_total', feature: :search_track_unique_users, feature_default_enabled: true
2019-09-30 21:07:59 +05:30
around_action :allow_gitaly_ref_name_caching
2020-11-24 15:15:51 +05:30
before_action :block_anonymous_global_searches
2018-03-27 19:54:05 +05:30
skip_before_action :authenticate_user!
requires_cross_project_access if: -> do
search_term_present = params[:search].present? || params[:term].present?
search_term_present && !params[:project_id].present?
end
2015-09-11 14:41:01 +05:30
layout 'search'
2021-01-03 14:25:43 +05:30
feature_category :global_search
2014-09-02 18:07:02 +05:30
def show
2017-08-17 22:00:37 +05:30
@project = search_service.project
@group = search_service.group
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
return if params[:search].blank?
2016-09-29 09:46:39 +05:30
2020-03-13 15:44:24 +05:30
return unless search_term_valid?
2021-01-29 00:20:46 +05:30
return if check_single_commit_result?
2016-06-02 11:05:42 +05:30
@search_term = params[:search]
2021-01-29 00:20:46 +05:30
@sort = params[:sort] || default_sort
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
@scope = search_service.scope
@show_snippets = search_service.show_snippets?
@search_results = search_service.search_results
2020-06-23 00:09:42 +05:30
@search_objects = search_service.search_objects(preload_method)
2021-01-03 14:25:43 +05:30
@search_highlight = search_service.search_highlight
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
render_commits if @scope == 'commits'
2019-07-07 11:18:12 +05:30
eager_load_user_status if @scope == 'users'
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
increment_search_counters
2014-09-02 18:07:02 +05:30
end
2019-10-12 21:52:04 +05:30
def count
params.require([:search, :scope])
scope = search_service.scope
count = search_service.search_results.formatted_count(scope)
render json: { count: count }
end
2020-07-28 23:09:34 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def autocomplete
term = params[:term]
if params[:project_id].present?
@project = Project.find_by(id: params[:project_id])
@project = nil unless can?(current_user, :read_project, @project)
end
@ref = params[:project_ref] if params[:project_ref].present?
render json: search_autocomplete_opts(term).to_json
end
# rubocop: enable CodeReuse/ActiveRecord
2020-06-23 00:09:42 +05:30
private
2014-09-02 18:07:02 +05:30
2020-06-23 00:09:42 +05:30
def preload_method
SCOPE_PRELOAD_METHOD[@scope.to_sym]
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
# overridden in EE
def default_sort
'created_desc'
end
2020-03-13 15:44:24 +05:30
def search_term_valid?
unless search_service.valid_query_length?
flash[:alert] = t('errors.messages.search_chars_too_long', count: SearchService::SEARCH_CHAR_LIMIT)
return false
end
unless search_service.valid_terms_count?
flash[:alert] = t('errors.messages.search_terms_too_long', count: SearchService::SEARCH_TERM_LIMIT)
return false
end
true
end
2018-03-17 18:26:18 +05:30
def render_commits
@search_objects = prepare_commits_for_rendering(@search_objects)
end
2019-07-07 11:18:12 +05:30
def eager_load_user_status
@search_objects = @search_objects.eager_load(:status) # rubocop:disable CodeReuse/ActiveRecord
end
2021-01-29 00:20:46 +05:30
def check_single_commit_result?
return false if params[:force_search_results]
return false unless @project.present?
# download_code project policy grants user the read_commit ability
return false unless Ability.allowed?(current_user, :download_code, @project)
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
query = params[:search].strip.downcase
return false unless Commit.valid_hash?(query)
commit = @project.commit_by(oid: query)
return false unless commit.present?
link = search_path(safe_params.merge(force_search_results: true))
flash[:notice] = html_escape(_("You have been redirected to the only result; see the %{a_start}search results%{a_end} instead.")) % { a_start: "<a href=\"#{link}\"><u>".html_safe, a_end: '</u></a>'.html_safe }
redirect_to project_commit_path(@project, commit)
true
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
def increment_search_counters
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
2019-10-12 21:52:04 +05:30
return if params[:nav_source] != 'navbar'
2020-06-23 00:09:42 +05:30
Gitlab::UsageDataCounters::SearchCounter.count(:navbar_searches)
2019-10-12 21:52:04 +05:30
end
2020-10-24 23:57:45 +05:30
def append_info_to_payload(payload)
super
# Merging to :metadata will ensure these are logged as top level keys
2020-11-24 15:15:51 +05:30
payload[:metadata] ||= {}
2020-10-24 23:57:45 +05:30
payload[:metadata]['meta.search.group_id'] = params[:group_id]
payload[:metadata]['meta.search.project_id'] = params[:project_id]
payload[:metadata]['meta.search.scope'] = params[:scope]
2021-01-29 00:20:46 +05:30
payload[:metadata]['meta.search.filters.confidential'] = params[:confidential]
payload[:metadata]['meta.search.filters.state'] = params[:state]
payload[:metadata]['meta.search.force_search_results'] = params[:force_search_results]
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
def block_anonymous_global_searches
return if params[:project_id].present? || params[:group_id].present?
return if current_user
return unless ::Feature.enabled?(:block_anonymous_global_searches)
store_location_for(:user, request.fullpath)
redirect_to new_user_session_path, alert: _('You must be logged in to search across all of GitLab')
end
2014-09-02 18:07:02 +05:30
end
2020-11-24 15:15:51 +05:30
SearchController.prepend_if_ee('EE::SearchController')