Commit 3680c78d authored by Mario de la Ossa's avatar Mario de la Ossa

Fix banzai iteration references when in private namespace

When parsing references inside Banzai, we do not get access to the
current user, and as such we cannot use Finders that check for
permissions
parent 19240517
---
title: Fixed iteration references when in a private group/project
merge_request: 39262
author:
type: fixed
...@@ -10,7 +10,7 @@ module EE ...@@ -10,7 +10,7 @@ module EE
def find_object(parent, id) def find_object(parent, id)
return unless valid_context?(parent) return unless valid_context?(parent)
find_iteration_with_finder(parent, id: id) find_iteration(parent, id: id)
end end
def valid_context?(parent) def valid_context?(parent)
...@@ -37,7 +37,7 @@ module EE ...@@ -37,7 +37,7 @@ module EE
iterations = {} iterations = {}
unescaped_html = unescape_html_entities(text).gsub(pattern) do |match| unescaped_html = unescape_html_entities(text).gsub(pattern) do |match|
iteration = find_iteration($~[:project], $~[:namespace], $~[:iteration_id], $~[:iteration_name]) iteration = parse_and_find_iteration($~[:project], $~[:namespace], $~[:iteration_id], $~[:iteration_name])
if iteration if iteration
iterations[iteration.id] = yield match, iteration.id, $~[:project], $~[:namespace], $~ iterations[iteration.id] = yield match, iteration.id, $~[:project], $~[:namespace], $~
...@@ -52,7 +52,7 @@ module EE ...@@ -52,7 +52,7 @@ module EE
escape_with_placeholders(unescaped_html, iterations) escape_with_placeholders(unescaped_html, iterations)
end end
def find_iteration(project_ref, namespace_ref, iteration_id, iteration_name) def parse_and_find_iteration(project_ref, namespace_ref, iteration_id, iteration_name)
project_path = full_project_path(namespace_ref, project_ref) project_path = full_project_path(namespace_ref, project_ref)
# Returns group if project is not found by path # Returns group if project is not found by path
...@@ -62,7 +62,7 @@ module EE ...@@ -62,7 +62,7 @@ module EE
iteration_params = iteration_params(iteration_id, iteration_name) iteration_params = iteration_params(iteration_id, iteration_name)
find_iteration_with_finder(parent, iteration_params) find_iteration(parent, iteration_params)
end end
def iteration_params(id, name) def iteration_params(id, name)
...@@ -73,23 +73,17 @@ module EE ...@@ -73,23 +73,17 @@ module EE
end end
end end
# rubocop:disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def find_iteration_with_finder(parent, params) def find_iteration(parent, params)
finder_params = iteration_finder_params(parent) ::Iteration.for_projects_and_groups(project_ids(parent), group_and_ancestors_ids(parent)).find_by(**params)
::IterationsFinder.new(context[:current_user], finder_params).find_by(params)
end end
# rubocop:enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def iteration_finder_params(parent)
{ order: nil, state: 'all' }.tap do |params|
params[:project_ids] = parent.id if project_context?(parent)
params[:group_ids] = self_and_ancestors_ids(parent) def project_ids(parent)
end parent.id if project_context?(parent)
end end
def self_and_ancestors_ids(parent) def group_and_ancestors_ids(parent)
if group_context?(parent) if group_context?(parent)
parent.self_and_ancestors.select(:id) parent.self_and_ancestors.select(:id)
elsif project_context?(parent) elsif project_context?(parent)
......
...@@ -357,6 +357,21 @@ RSpec.describe Banzai::Filter::IterationReferenceFilter do ...@@ -357,6 +357,21 @@ RSpec.describe Banzai::Filter::IterationReferenceFilter do
end end
end end
end end
context 'for private subgroups' do
let(:sub_group) { create(:group, :private, parent: group) }
let(:sub_group_iteration) { create(:iteration, title: 'sub_group_iteration', group: sub_group) }
it 'links to a valid reference of subgroup and group iterations' do
[group_iteration, sub_group_iteration].each do |iteration|
reference = "*iteration:#{iteration.title}"
result = reference_filter("See #{reference}", { project: nil, group: sub_group })
expect(result.css('a').first.attr('href')).to eq(urls.iteration_url(iteration))
end
end
end
end end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Gitlab module Gitlab
module MarkdownCache module MarkdownCache
# Increment this number every time the renderer changes its output # Increment this number every time the renderer changes its output
CACHE_COMMONMARK_VERSION = 24 CACHE_COMMONMARK_VERSION = 25
CACHE_COMMONMARK_VERSION_START = 10 CACHE_COMMONMARK_VERSION_START = 10
BaseError = Class.new(StandardError) BaseError = Class.new(StandardError)
......
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