Commit 8cc646fa authored by Reuben Pereira's avatar Reuben Pereira

Remove nodes_visible_to_user from ProjectParser and memoize readable_project_ids

parent a08b5144
module Banzai module Banzai
module ReferenceParser module ReferenceParser
class ProjectParser < BaseParser class ProjectParser < BaseParser
include Gitlab::Utils::StrongMemoize
self.reference_type = :project self.reference_type = :project
def references_relation def references_relation
Project Project
end end
def nodes_visible_to_user(user, nodes)
nodes_projects_hash = lazy { projects_for_nodes(nodes) }
project_attr = 'data-project'
readable_project_ids = projects_readable_by_user(nodes_projects_hash.values, user)
nodes.select do |node|
if node.has_attribute?(project_attr)
readable_project_ids.include?(nodes_projects_hash[node].try(:id))
else
true
end
end
end
private private
# Returns an Array of Project ids that can be read by the given user. # Returns an Array of Project ids that can be read by the given user.
# #
# projects - The projects to reduce down to those readable by the user. # projects - The projects to reduce down to those readable by the user.
# user - The User for which to check the projects # user - The User for which to check the projects
def projects_readable_by_user(projects, user) def readable_project_ids_for(projects, user)
strong_memoize(:readable_project_ids_for) do
Project.public_or_visible_to_user(user).where("projects.id IN (?)", projects.map(&:id)).pluck(:id) Project.public_or_visible_to_user(user).where("projects.id IN (?)", projects.map(&:id)).pluck(:id)
end end
end end
def can_read_reference?(user, ref_project, node)
readable_project_ids_for(@projects_for_nodes.values, user).include?(ref_project.try(:id))
end
end
end end
end end
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