Commit 08a17d15 authored by Douwe Maan's avatar Douwe Maan

Merge branch '30917-wiki-is-not-searchable-with-guest-permissions' into 'master'

Resolve "Wiki is not searchable with Guest permissions"

Closes #30917

See merge request !11613
parents fd2993b5 be9ffbaf
...@@ -85,6 +85,12 @@ module ProjectsHelper ...@@ -85,6 +85,12 @@ module ProjectsHelper
@nav_tabs ||= get_project_nav_tabs(@project, current_user) @nav_tabs ||= get_project_nav_tabs(@project, current_user)
end end
def project_search_tabs?(tab)
abilities = Array(search_tab_ability_map[tab])
abilities.any? { |ability| can?(current_user, ability, @project) }
end
def project_nav_tab?(name) def project_nav_tab?(name)
project_nav_tabs.include? name project_nav_tabs.include? name
end end
...@@ -204,7 +210,17 @@ module ProjectsHelper ...@@ -204,7 +210,17 @@ module ProjectsHelper
nav_tabs << :container_registry nav_tabs << :container_registry
end end
tab_ability_map = { tab_ability_map.each do |tab, ability|
if can?(current_user, ability, project)
nav_tabs << tab
end
end
nav_tabs.flatten
end
def tab_ability_map
{
environments: :read_environment, environments: :read_environment,
milestones: :read_milestone, milestones: :read_milestone,
pipelines: :read_pipeline, pipelines: :read_pipeline,
...@@ -216,14 +232,15 @@ module ProjectsHelper ...@@ -216,14 +232,15 @@ module ProjectsHelper
team: :read_project_member, team: :read_project_member,
wiki: :read_wiki wiki: :read_wiki
} }
end
tab_ability_map.each do |tab, ability| def search_tab_ability_map
if can?(current_user, ability, project) @search_tab_ability_map ||= tab_ability_map.merge(
nav_tabs << tab blobs: :download_code,
end commits: :download_code,
end merge_requests: :read_merge_request,
notes: [:read_merge_request, :download_code, :read_issue, :read_project_snippet]
nav_tabs.flatten )
end end
def project_lfs_status(project) def project_lfs_status(project)
......
...@@ -12,7 +12,7 @@ class SearchService ...@@ -12,7 +12,7 @@ class SearchService
@project = @project =
if params[:project_id].present? if params[:project_id].present?
the_project = Project.find_by(id: params[:project_id]) the_project = Project.find_by(id: params[:project_id])
can?(current_user, :download_code, the_project) ? the_project : nil can?(current_user, :read_project, the_project) ? the_project : nil
else else
nil nil
end end
......
...@@ -3,41 +3,48 @@ ...@@ -3,41 +3,48 @@
.fade-right= icon('angle-right') .fade-right= icon('angle-right')
%ul.nav-links.search-filter.scrolling-tabs %ul.nav-links.search-filter.scrolling-tabs
- if @project - if @project
%li{ class: active_when(@scope == 'blobs') } - if project_search_tabs?(:blobs)
= link_to search_filter_path(scope: 'blobs') do %li{ class: active_when(@scope == 'blobs') }
Code = link_to search_filter_path(scope: 'blobs') do
%span.badge Code
= @search_results.blobs_count %span.badge
%li{ class: active_when(@scope == 'issues') } = @search_results.blobs_count
= link_to search_filter_path(scope: 'issues') do - if project_search_tabs?(:issues)
Issues %li{ class: active_when(@scope == 'issues') }
%span.badge = link_to search_filter_path(scope: 'issues') do
= @search_results.issues_count Issues
%li{ class: active_when(@scope == 'merge_requests') } %span.badge
= link_to search_filter_path(scope: 'merge_requests') do = @search_results.issues_count
Merge requests - if project_search_tabs?(:merge_requests)
%span.badge %li{ class: active_when(@scope == 'merge_requests') }
= @search_results.merge_requests_count = link_to search_filter_path(scope: 'merge_requests') do
%li{ class: active_when(@scope == 'milestones') } Merge requests
= link_to search_filter_path(scope: 'milestones') do %span.badge
Milestones = @search_results.merge_requests_count
%span.badge - if project_search_tabs?(:milestones)
= @search_results.milestones_count %li{ class: active_when(@scope == 'milestones') }
%li{ class: active_when(@scope == 'notes') } = link_to search_filter_path(scope: 'milestones') do
= link_to search_filter_path(scope: 'notes') do Milestones
Comments %span.badge
%span.badge = @search_results.milestones_count
= @search_results.notes_count - if project_search_tabs?(:notes)
%li{ class: active_when(@scope == 'wiki_blobs') } %li{ class: active_when(@scope == 'notes') }
= link_to search_filter_path(scope: 'wiki_blobs') do = link_to search_filter_path(scope: 'notes') do
Wiki Comments
%span.badge %span.badge
= @search_results.wiki_blobs_count = @search_results.notes_count
%li{ class: active_when(@scope == 'commits') } - if project_search_tabs?(:wiki)
= link_to search_filter_path(scope: 'commits') do %li{ class: active_when(@scope == 'wiki_blobs') }
Commits = link_to search_filter_path(scope: 'wiki_blobs') do
%span.badge Wiki
= @search_results.commits_count %span.badge
= @search_results.wiki_blobs_count
- if project_search_tabs?(:commits)
%li{ class: active_when(@scope == 'commits') }
= link_to search_filter_path(scope: 'commits') do
Commits
%span.badge
= @search_results.commits_count
- elsif @show_snippets - elsif @show_snippets
%li{ class: active_when(@scope == 'snippet_blobs') } %li{ class: active_when(@scope == 'snippet_blobs') }
......
---
title: 'Fix: Wiki is not searchable with Guest permissions'
merge_request:
author:
...@@ -123,8 +123,8 @@ describe Gitlab::ProjectSearchResults, lib: true do ...@@ -123,8 +123,8 @@ describe Gitlab::ProjectSearchResults, lib: true do
context 'when wiki is internal' do context 'when wiki is internal' do
let(:project) { create(:project, :public, :wiki_private) } let(:project) { create(:project, :public, :wiki_private) }
it 'finds wiki blobs for members' do it 'finds wiki blobs for guest' do
project.add_reporter(user) project.add_guest(user)
is_expected.not_to be_empty is_expected.not_to be_empty
end end
......
...@@ -26,6 +26,15 @@ describe SearchService, services: true do ...@@ -26,6 +26,15 @@ describe SearchService, services: true do
expect(project).to eq accessible_project expect(project).to eq accessible_project
end end
it 'returns the project for guests' do
search_project = create :empty_project
search_project.add_guest(user)
project = SearchService.new(user, project_id: search_project.id).project
expect(project).to eq search_project
end
end end
context 'when the project is not accessible' do context 'when the project is not accessible' do
......
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