2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
module Banzai
|
|
|
|
module ReferenceParser
|
|
|
|
# Base class for reference parsing classes.
|
|
|
|
#
|
|
|
|
# Each parser should also specify its reference type by calling
|
|
|
|
# `self.reference_type = ...` in the body of the class. The value of this
|
|
|
|
# method should be a symbol such as `:issue` or `:merge_request`. For
|
|
|
|
# example:
|
|
|
|
#
|
|
|
|
# class IssueParser < BaseParser
|
|
|
|
# self.reference_type = :issue
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# The reference type is used to determine what nodes to pass to the
|
|
|
|
# `referenced_by` method.
|
|
|
|
#
|
|
|
|
# Parser classes should either implement the instance method
|
|
|
|
# `references_relation` or overwrite `referenced_by`. The
|
|
|
|
# `references_relation` method is supposed to return an
|
|
|
|
# ActiveRecord::Relation used as a base relation for retrieving the objects
|
|
|
|
# referenced in a set of HTML nodes.
|
|
|
|
#
|
|
|
|
# Each class can implement two additional methods:
|
|
|
|
#
|
|
|
|
# * `nodes_user_can_reference`: returns an Array of nodes the given user can
|
|
|
|
# refer to.
|
|
|
|
# * `nodes_visible_to_user`: returns an Array of nodes that are visible to
|
|
|
|
# the given user.
|
|
|
|
#
|
|
|
|
# You only need to overwrite these methods if you want to tweak who can see
|
|
|
|
# which references. For example, the IssueParser class defines its own
|
|
|
|
# `nodes_visible_to_user` method so it can ensure users can only see issues
|
|
|
|
# they have access to.
|
|
|
|
class BaseParser
|
|
|
|
class << self
|
2016-12-05 07:12:22 -05:00
|
|
|
attr_accessor :reference_type, :reference_options
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the attribute name containing the value for every object to be
|
|
|
|
# parsed by the current parser.
|
|
|
|
#
|
|
|
|
# For example, for a parser class that returns "Animal" objects this
|
|
|
|
# attribute would be "data-animal".
|
|
|
|
def self.data_attribute
|
|
|
|
@data_attribute ||= "data-#{reference_type.to_s.dasherize}"
|
|
|
|
end
|
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
# context - An instance of `Banzai::RenderContext`.
|
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_for_node(node)
|
|
|
|
context.project_for_node(node)
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all the nodes containing references that the user can refer to.
|
|
|
|
def nodes_user_can_reference(user, nodes)
|
|
|
|
nodes
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all the nodes that are visible to the given user.
|
|
|
|
def nodes_visible_to_user(user, nodes)
|
|
|
|
projects = lazy { projects_for_nodes(nodes) }
|
|
|
|
project_attr = 'data-project'
|
|
|
|
|
|
|
|
nodes.select do |node|
|
|
|
|
if node.has_attribute?(project_attr)
|
2017-06-08 12:56:39 -04:00
|
|
|
can_read_reference?(user, projects[node], node)
|
2016-05-26 07:16:43 -04:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an Array of objects referenced by any of the given HTML nodes.
|
|
|
|
def referenced_by(nodes)
|
|
|
|
ids = unique_attribute_values(nodes, self.class.data_attribute)
|
|
|
|
|
2016-09-27 11:13:20 -04:00
|
|
|
if ids.empty?
|
|
|
|
references_relation.none
|
|
|
|
else
|
|
|
|
references_relation.where(id: ids)
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the ActiveRecord::Relation to use for querying references in the
|
|
|
|
# DB.
|
|
|
|
def references_relation
|
|
|
|
raise NotImplementedError,
|
|
|
|
"#{self.class} does not implement #{__method__}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Hash containing attribute values per project ID.
|
|
|
|
#
|
|
|
|
# The returned Hash uses the following format:
|
|
|
|
#
|
|
|
|
# { project id => [value1, value2, ...] }
|
|
|
|
#
|
|
|
|
# nodes - An Array of HTML nodes to process.
|
|
|
|
# attribute - The name of the attribute (as a String) for which to gather
|
|
|
|
# values.
|
|
|
|
#
|
|
|
|
# Returns a Hash.
|
|
|
|
def gather_attributes_per_project(nodes, attribute)
|
|
|
|
per_project = Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
project_id = node.attr('data-project').to_i
|
|
|
|
id = node.attr(attribute)
|
|
|
|
|
|
|
|
per_project[project_id] << id if id
|
|
|
|
end
|
|
|
|
|
|
|
|
per_project
|
|
|
|
end
|
|
|
|
|
2016-08-24 13:11:48 -04:00
|
|
|
# Returns a Hash containing objects for an attribute grouped per the
|
|
|
|
# nodes that reference them.
|
2016-05-26 07:16:43 -04:00
|
|
|
#
|
|
|
|
# The returned Hash uses the following format:
|
|
|
|
#
|
2016-08-24 13:11:48 -04:00
|
|
|
# { node => row }
|
2016-05-26 07:16:43 -04:00
|
|
|
#
|
|
|
|
# nodes - An Array of HTML nodes to process.
|
|
|
|
#
|
|
|
|
# collection - The model or ActiveRecord relation to use for retrieving
|
|
|
|
# rows from the database.
|
|
|
|
#
|
|
|
|
# attribute - The name of the attribute containing the primary key values
|
|
|
|
# for every row.
|
|
|
|
#
|
|
|
|
# Returns a Hash.
|
|
|
|
def grouped_objects_for_nodes(nodes, collection, attribute)
|
|
|
|
return {} if nodes.empty?
|
|
|
|
|
|
|
|
ids = unique_attribute_values(nodes, attribute)
|
2016-08-24 13:11:48 -04:00
|
|
|
collection_objects = collection_objects_for_ids(collection, ids)
|
|
|
|
objects_by_id = collection_objects.index_by(&:id)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
2016-08-24 13:11:48 -04:00
|
|
|
nodes.each_with_object({}) do |node, hash|
|
|
|
|
if node.has_attribute?(attribute)
|
2017-04-15 09:04:15 -04:00
|
|
|
obj = objects_by_id[node.attr(attribute).to_i]
|
|
|
|
hash[node] = obj if obj
|
2016-08-24 13:11:48 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an Array containing all unique values of an attribute of the
|
|
|
|
# given nodes.
|
|
|
|
def unique_attribute_values(nodes, attribute)
|
|
|
|
values = Set.new
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
if node.has_attribute?(attribute)
|
|
|
|
values << node.attr(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
values.to_a
|
|
|
|
end
|
|
|
|
|
2016-06-30 05:51:07 -04:00
|
|
|
# Queries the collection for the objects with the given IDs.
|
|
|
|
#
|
|
|
|
# If the RequestStore module is enabled this method will only query any
|
|
|
|
# objects that have not yet been queried. For objects that have already
|
|
|
|
# been queried the object is returned from the cache.
|
|
|
|
def collection_objects_for_ids(collection, ids)
|
2018-09-20 18:40:15 -04:00
|
|
|
if Gitlab::SafeRequestStore.active?
|
2017-05-30 06:24:55 -04:00
|
|
|
ids = ids.map(&:to_i)
|
2016-06-30 05:51:07 -04:00
|
|
|
cache = collection_cache[collection_cache_key(collection)]
|
2017-05-30 06:24:55 -04:00
|
|
|
to_query = ids - cache.keys
|
2016-06-30 05:51:07 -04:00
|
|
|
|
|
|
|
unless to_query.empty?
|
|
|
|
collection.where(id: to_query).each { |row| cache[row.id] = row }
|
|
|
|
end
|
|
|
|
|
2020-01-02 10:07:40 -05:00
|
|
|
ids.uniq.map { |id| cache[id] }.compact
|
2016-06-30 05:51:07 -04:00
|
|
|
else
|
|
|
|
collection.where(id: ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the cache key to use for a collection.
|
|
|
|
def collection_cache_key(collection)
|
|
|
|
collection.respond_to?(:model) ? collection.model : collection
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
# Processes the list of HTML documents and returns an Array containing all
|
|
|
|
# the references.
|
|
|
|
def process(documents)
|
|
|
|
type = self.class.reference_type
|
2016-12-05 07:12:22 -05:00
|
|
|
reference_options = self.class.reference_options
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
nodes = documents.flat_map do |document|
|
2016-12-05 07:12:22 -05:00
|
|
|
Querying.css(document, "a[data-reference-type='#{type}'].gfm", reference_options).to_a
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
gather_references(nodes)
|
|
|
|
end
|
|
|
|
|
2020-01-30 16:08:47 -05:00
|
|
|
# Gathers the references for the given HTML nodes. Returns visible
|
|
|
|
# references and a list of nodes which are not visible to the user
|
2016-05-26 07:16:43 -04:00
|
|
|
def gather_references(nodes)
|
|
|
|
nodes = nodes_user_can_reference(current_user, nodes)
|
2020-01-30 16:08:47 -05:00
|
|
|
visible = nodes_visible_to_user(current_user, nodes)
|
|
|
|
not_visible = nodes - visible
|
2016-05-26 07:16:43 -04:00
|
|
|
|
2020-01-30 16:08:47 -05:00
|
|
|
{ visible: referenced_by(visible), not_visible: not_visible }
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Hash containing the projects for a given list of HTML nodes.
|
|
|
|
#
|
|
|
|
# The returned Hash uses the following format:
|
|
|
|
#
|
2016-08-24 13:11:48 -04:00
|
|
|
# { node => project }
|
2016-05-26 07:16:43 -04:00
|
|
|
#
|
|
|
|
def projects_for_nodes(nodes)
|
|
|
|
@projects_for_nodes ||=
|
2018-09-30 13:54:21 -04:00
|
|
|
grouped_objects_for_nodes(nodes, Project.includes(:project_feature), 'data-project')
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
2017-02-28 16:08:07 -05:00
|
|
|
def can?(user, permission, subject = :global)
|
2016-08-08 14:55:13 -04:00
|
|
|
Ability.allowed?(user, permission, subject)
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_projects_for_hash_keys(hash)
|
2016-06-30 05:51:07 -04:00
|
|
|
collection_objects_for_ids(Project, hash.keys)
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
attr_reader :context
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
context.current_user
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
|
2016-11-01 16:18:51 -04:00
|
|
|
# When a feature is disabled or visible only for
|
|
|
|
# team members we should not allow team members
|
|
|
|
# see reference comments.
|
|
|
|
# Override this method on subclasses
|
|
|
|
# to check if user can read resource
|
2017-06-08 12:56:39 -04:00
|
|
|
def can_read_reference?(user, ref_project, node)
|
2016-11-01 16:18:51 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
def lazy(&block)
|
|
|
|
Gitlab::Lazy.new(&block)
|
|
|
|
end
|
2016-06-30 05:51:07 -04:00
|
|
|
|
|
|
|
def collection_cache
|
2018-09-20 18:40:15 -04:00
|
|
|
Gitlab::SafeRequestStore[:banzai_collection_cache] ||= Hash.new do |hash, key|
|
2016-06-30 05:51:07 -04:00
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|