Commit acb4a90e authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '340775-disable-anonymous-project-searching' into 'master'

Disable anonymous searching for ProjectsFinder with the disable_anonymous_search FF

See merge request gitlab-org/gitlab!70288
parents 203b5077 47ef25a1
......@@ -26,6 +26,7 @@ class Explore::ProjectsController < Explore::ApplicationController
feature_category :projects
def index
show_alert_if_search_is_disabled
@projects = load_projects
respond_to do |format|
......@@ -120,6 +121,12 @@ class Explore::ProjectsController < Explore::ApplicationController
end
end
end
def show_alert_if_search_is_disabled
return if current_user || params[:name].blank? && params[:search].blank? || !html_request? || Feature.disabled?(:disable_anonymous_search, type: :ops)
flash[:notice] = _('You must sign in to search for specific projects.')
end
end
Explore::ProjectsController.prepend_mod_with('Explore::ProjectsController')
......@@ -193,6 +193,7 @@ class ProjectsFinder < UnionFinder
def by_search(items)
params[:search] ||= params[:name]
return items if Feature.enabled?(:disable_anonymous_search, type: :ops) && current_user.nil?
return items.none if params[:search].present? && params[:minimum_search_length].present? && params[:search].length < params[:minimum_search_length].to_i
items.optionally_search(params[:search], include_namespace: params[:search_namespaces].present?)
......
......@@ -38758,6 +38758,9 @@ msgstr ""
msgid "You must provide your current password in order to change it."
msgstr ""
msgid "You must sign in to search for specific projects."
msgstr ""
msgid "You must sign in to search for specific terms."
msgstr ""
......
......@@ -200,6 +200,24 @@ RSpec.describe Explore::ProjectsController do
let(:sorting_param) { 'created_asc' }
end
end
describe 'GET #index' do
let(:controller_action) { :index }
let(:params_with_name) { { name: 'some project' } }
context 'when disable_anonymous_search is enabled' do
before do
stub_feature_flags(disable_anonymous_search: true)
end
it 'does not show a flash message' do
sign_in(create(:user))
get controller_action, params: params_with_name
expect(flash[:notice]).to be_nil
end
end
end
end
context 'when user is not signed in' do
......@@ -229,5 +247,50 @@ RSpec.describe Explore::ProjectsController do
expect(response).to redirect_to new_user_session_path
end
end
describe 'GET #index' do
let(:controller_action) { :index }
let(:params_with_name) { { name: 'some project' } }
context 'when disable_anonymous_search is enabled' do
before do
stub_feature_flags(disable_anonymous_search: true)
end
it 'shows a flash message' do
get controller_action, params: params_with_name
expect(flash[:notice]).to eq('You must sign in to search for specific projects.')
end
context 'when search param is not given' do
it 'does not show a flash message' do
get controller_action
expect(flash[:notice]).to be_nil
end
end
context 'when format is not HTML' do
it 'does not show a flash message' do
get controller_action, params: params_with_name.merge(format: :atom)
expect(flash[:notice]).to be_nil
end
end
end
context 'when disable_anonymous_search is disabled' do
before do
stub_feature_flags(disable_anonymous_search: false)
end
it 'does not show a flash message' do
get controller_action, params: params_with_name
expect(flash[:notice]).to be_nil
end
end
end
end
end
......@@ -190,6 +190,32 @@ RSpec.describe ProjectsFinder do
it { is_expected.to eq([public_project]) }
end
context 'with anonymous user' do
let(:public_project_2) { create(:project, :public, group: group, name: 'E', path: 'E') }
let(:current_user) { nil }
let(:params) { { search: 'C' } }
context 'with disable_anonymous_search feature flag enabled' do
before do
stub_feature_flags(disable_anonymous_search: true)
end
it 'does not perform search' do
is_expected.to eq([public_project_2, public_project])
end
end
context 'with disable_anonymous_search feature flag disabled' do
before do
stub_feature_flags(disable_anonymous_search: false)
end
it 'finds one public project' do
is_expected.to eq([public_project])
end
end
end
describe 'filter by name for backward compatibility' do
let(:params) { { name: 'C' } }
......
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