Commit a49c38c6 authored by Nick Thomas's avatar Nick Thomas

Allow admins to perform global searches with Elasticsearch

parent f2cdab59
...@@ -180,22 +180,19 @@ module Elastic ...@@ -180,22 +180,19 @@ module Elastic
def project_ids_query(current_user, project_ids, public_and_internal_projects, feature = nil) def project_ids_query(current_user, project_ids, public_and_internal_projects, feature = nil)
conditions = [] conditions = []
limit_private_projects = {}
private_project_condition = { if project_ids != :any
bool: { limit_private_projects[:filter] = { terms: { id: project_ids } }
filter: { end
terms: { id: project_ids }
}
}
}
if feature if feature
private_project_condition[:bool][:must_not] = { limit_private_projects[:must_not] = {
term: { "#{feature}_access_level" => ProjectFeature::DISABLED } term: { "#{feature}_access_level" => ProjectFeature::DISABLED }
} }
end end
conditions << private_project_condition conditions << { bool: limit_private_projects } unless limit_private_projects.empty?
if public_and_internal_projects if public_and_internal_projects
conditions << if feature conditions << if feature
......
...@@ -53,7 +53,7 @@ module Elastic ...@@ -53,7 +53,7 @@ module Elastic
end end
def self.confidentiality_filter(query_hash, current_user) def self.confidentiality_filter(query_hash, current_user)
return query_hash if current_user && current_user.admin? return query_hash if current_user && current_user.admin_or_auditor?
filter = if current_user filter = if current_user
{ {
......
...@@ -17,14 +17,23 @@ module Search ...@@ -17,14 +17,23 @@ module Search
end end
if current_application_settings.elasticsearch_search? if current_application_settings.elasticsearch_search?
unless group projects_spec =
projects = current_user ? current_user.authorized_projects : Project.none if group
end projects.pluck(:id)
else
if current_user && current_user.admin_or_auditor?
:any
elsif current_user
current_user.authorized_projects.pluck(:id)
else
[]
end
end
Gitlab::Elastic::SearchResults.new( Gitlab::Elastic::SearchResults.new(
current_user, current_user,
params[:search], params[:search],
projects.pluck(:id), projects_spec,
!group # Ignore public projects outside of the group if provided !group # Ignore public projects outside of the group if provided
) )
else else
......
---
title: Allow admins to perform global searches with Elasticsearch
merge_request: 1578
author:
...@@ -165,7 +165,12 @@ module Gitlab ...@@ -165,7 +165,12 @@ module Gitlab
end end
def repository_filter def repository_filter
conditions = [{ terms: { id: non_guest_project_ids } }] conditions =
if non_guest_project_ids == :any
[{ exists: { field: "id" } }]
else
[{ terms: { id: non_guest_project_ids } }]
end
if public_and_internal_projects if public_and_internal_projects
conditions << { conditions << {
...@@ -213,7 +218,11 @@ module Gitlab ...@@ -213,7 +218,11 @@ module Gitlab
end end
def non_guest_project_ids def non_guest_project_ids
@non_guest_project_ids ||= limit_project_ids - guest_project_ids if limit_project_ids == :any
:any
else
@non_guest_project_ids ||= limit_project_ids - guest_project_ids
end
end end
def default_scope def default_scope
......
...@@ -2,6 +2,8 @@ require 'spec_helper' ...@@ -2,6 +2,8 @@ require 'spec_helper'
describe 'GlobalSearch' do describe 'GlobalSearch' do
let(:features) { %i(issues merge_requests repository builds) } let(:features) { %i(issues merge_requests repository builds) }
let(:admin) { create :user, admin: true }
let(:auditor) {create :user, auditor: true }
let(:non_member) { create :user } let(:non_member) { create :user }
let(:member) { create :user } let(:member) { create :user }
let(:guest) { create :user } let(:guest) { create :user }
...@@ -27,6 +29,8 @@ describe 'GlobalSearch' do ...@@ -27,6 +29,8 @@ describe 'GlobalSearch' do
it "does not find items if features are disabled" do it "does not find items if features are disabled" do
create_items(project, feature_settings(:disabled)) create_items(project, feature_settings(:disabled))
expect_no_items_to_be_found(admin)
expect_no_items_to_be_found(auditor)
expect_no_items_to_be_found(member) expect_no_items_to_be_found(member)
expect_no_items_to_be_found(guest) expect_no_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
...@@ -36,6 +40,8 @@ describe 'GlobalSearch' do ...@@ -36,6 +40,8 @@ describe 'GlobalSearch' do
it "shows items to member only if features are enabled" do it "shows items to member only if features are enabled" do
create_items(project, feature_settings(:enabled)) create_items(project, feature_settings(:enabled))
expect_items_to_be_found(admin)
expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_non_code_items_to_be_found(guest) expect_non_code_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
...@@ -50,6 +56,8 @@ describe 'GlobalSearch' do ...@@ -50,6 +56,8 @@ describe 'GlobalSearch' do
it "does not find items if features are disabled" do it "does not find items if features are disabled" do
create_items(project, feature_settings(:disabled)) create_items(project, feature_settings(:disabled))
expect_no_items_to_be_found(admin)
expect_no_items_to_be_found(auditor)
expect_no_items_to_be_found(member) expect_no_items_to_be_found(member)
expect_no_items_to_be_found(guest) expect_no_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
...@@ -59,6 +67,8 @@ describe 'GlobalSearch' do ...@@ -59,6 +67,8 @@ describe 'GlobalSearch' do
it "shows items to member only if features are enabled" do it "shows items to member only if features are enabled" do
create_items(project, feature_settings(:enabled)) create_items(project, feature_settings(:enabled))
expect_items_to_be_found(admin)
expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_items_to_be_found(guest) expect_items_to_be_found(guest)
expect_items_to_be_found(non_member) expect_items_to_be_found(non_member)
...@@ -68,6 +78,8 @@ describe 'GlobalSearch' do ...@@ -68,6 +78,8 @@ describe 'GlobalSearch' do
it "shows items to member only if features are private" do it "shows items to member only if features are private" do
create_items(project, feature_settings(:private)) create_items(project, feature_settings(:private))
expect_items_to_be_found(admin)
expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_non_code_items_to_be_found(guest) expect_non_code_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
...@@ -82,6 +94,8 @@ describe 'GlobalSearch' do ...@@ -82,6 +94,8 @@ describe 'GlobalSearch' do
it "does not find items if features are disabled" do it "does not find items if features are disabled" do
create_items(project, feature_settings(:disabled)) create_items(project, feature_settings(:disabled))
expect_no_items_to_be_found(admin)
expect_no_items_to_be_found(auditor)
expect_no_items_to_be_found(member) expect_no_items_to_be_found(member)
expect_no_items_to_be_found(guest) expect_no_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
...@@ -91,6 +105,8 @@ describe 'GlobalSearch' do ...@@ -91,6 +105,8 @@ describe 'GlobalSearch' do
it "finds items if features are enabled" do it "finds items if features are enabled" do
create_items(project, feature_settings(:enabled)) create_items(project, feature_settings(:enabled))
expect_items_to_be_found(admin)
expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_items_to_be_found(guest) expect_items_to_be_found(guest)
expect_items_to_be_found(non_member) expect_items_to_be_found(non_member)
...@@ -100,6 +116,8 @@ describe 'GlobalSearch' do ...@@ -100,6 +116,8 @@ describe 'GlobalSearch' do
it "shows items to member only if features are private" do it "shows items to member only if features are private" do
create_items(project, feature_settings(:private)) create_items(project, feature_settings(:private))
expect_items_to_be_found(admin)
expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_non_code_items_to_be_found(guest) expect_non_code_items_to_be_found(guest)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment