2015-04-03 00:46:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
2015-04-14 22:34:01 +00:00
|
|
|
# Common methods for ReferenceFilters that support an optional cross-project
|
|
|
|
# reference.
|
2015-04-03 00:46:43 +00:00
|
|
|
module CrossProjectReference
|
|
|
|
# Given a cross-project reference string, get the Project record
|
|
|
|
#
|
2015-04-14 20:04:37 +00:00
|
|
|
# Defaults to value of `context[:project]` if:
|
2015-04-14 22:34:01 +00:00
|
|
|
# * No reference is given OR
|
2015-04-16 18:06:05 +00:00
|
|
|
# * Reference given doesn't exist
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# ref - String reference.
|
|
|
|
#
|
2015-04-16 18:06:05 +00:00
|
|
|
# Returns a Project, or nil if the reference can't be accessed
|
2015-04-03 00:46:43 +00:00
|
|
|
def project_from_ref(ref)
|
2015-04-17 10:34:51 +00:00
|
|
|
return context[:project] unless ref
|
|
|
|
|
|
|
|
other = Project.find_with_namespace(ref)
|
|
|
|
return nil unless other && user_can_reference_project?(other)
|
2015-04-23 16:58:55 +00:00
|
|
|
|
2015-04-17 10:34:51 +00:00
|
|
|
other
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2015-04-14 20:04:37 +00:00
|
|
|
|
|
|
|
def user_can_reference_project?(project, user = context[:current_user])
|
2015-04-16 17:05:49 +00:00
|
|
|
Ability.abilities.allowed?(user, :read_project, project)
|
2015-04-14 20:04:37 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|