2014-09-02 18:07:02 +05:30
|
|
|
module VisibilityLevelHelper
|
|
|
|
def visibility_level_color(level)
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
'vs-private'
|
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
'vs-internal'
|
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
'vs-public'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
# Return the description for the +level+ argument.
|
|
|
|
#
|
2015-12-23 02:04:40 +05:30
|
|
|
# +level+ One of the Gitlab::VisibilityLevel constants
|
|
|
|
# +form_model+ Either a model object (Project, Snippet, etc.) or the name of
|
|
|
|
# a Project or Snippet class.
|
2015-09-11 14:41:01 +05:30
|
|
|
def visibility_level_description(level, form_model)
|
2015-12-23 02:04:40 +05:30
|
|
|
case form_model
|
|
|
|
when Project
|
2015-09-11 14:41:01 +05:30
|
|
|
project_visibility_level_description(level)
|
2016-06-02 11:05:42 +05:30
|
|
|
when Group
|
|
|
|
group_visibility_level_description(level)
|
2015-12-23 02:04:40 +05:30
|
|
|
when Snippet
|
|
|
|
snippet_visibility_level_description(level, form_model)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_visibility_level_description(level)
|
2015-12-23 02:04:40 +05:30
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
2017-09-10 17:25:29 +05:30
|
|
|
_("Project access must be granted explicitly to each user.")
|
2015-12-23 02:04:40 +05:30
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
2017-09-10 17:25:29 +05:30
|
|
|
_("The project can be accessed by any logged in user.")
|
2015-12-23 02:04:40 +05:30
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
2017-09-10 17:25:29 +05:30
|
|
|
_("The project can be accessed without any authentication.")
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def group_visibility_level_description(level)
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
"The group and its projects can only be viewed by members."
|
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
"The group and any internal projects can be viewed by any logged in user."
|
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
"The group and any public projects can be viewed without any authentication."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def snippet_visibility_level_description(level, snippet = nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
2015-12-23 02:04:40 +05:30
|
|
|
if snippet.is_a? ProjectSnippet
|
|
|
|
"The snippet is visible only to project members."
|
|
|
|
else
|
|
|
|
"The snippet is visible only to me."
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
2015-12-23 02:04:40 +05:30
|
|
|
"The snippet is visible to any logged in user."
|
2014-09-02 18:07:02 +05:30
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
2015-12-23 02:04:40 +05:30
|
|
|
"The snippet can be accessed without any authentication."
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def restricted_visibility_level_description(level)
|
|
|
|
level_name = Gitlab::VisibilityLevel.level_name(level)
|
|
|
|
"#{level_name.capitalize} visibility has been restricted by the administrator."
|
|
|
|
end
|
|
|
|
|
|
|
|
def disallowed_visibility_level_description(level, form_model)
|
|
|
|
case form_model
|
|
|
|
when Project
|
|
|
|
disallowed_project_visibility_level_description(level, form_model)
|
|
|
|
when Group
|
|
|
|
disallowed_group_visibility_level_description(level, form_model)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Note: these messages closely mirror the form validation strings found in the project
|
|
|
|
# model and any changes or additons to these may also need to be made there.
|
|
|
|
def disallowed_project_visibility_level_description(level, project)
|
|
|
|
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
|
|
|
|
reasons = []
|
|
|
|
instructions = ''
|
|
|
|
|
|
|
|
unless project.visibility_level_allowed_as_fork?(level)
|
|
|
|
reasons << "the fork source project has lower visibility"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless project.visibility_level_allowed_by_group?(level)
|
|
|
|
errors = visibility_level_errors_for_group(project.group, level_name)
|
|
|
|
|
|
|
|
reasons << errors[:reason]
|
|
|
|
instructions << errors[:instruction]
|
|
|
|
end
|
|
|
|
|
|
|
|
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
|
|
|
|
"This project cannot be #{level_name}#{reasons}.#{instructions}".html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
# Note: these messages closely mirror the form validation strings found in the group
|
|
|
|
# model and any changes or additons to these may also need to be made there.
|
|
|
|
def disallowed_group_visibility_level_description(level, group)
|
|
|
|
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
|
|
|
|
reasons = []
|
|
|
|
instructions = ''
|
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_projects?(level)
|
|
|
|
reasons << "it contains projects with higher visibility"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_sub_groups?(level)
|
|
|
|
reasons << "it contains sub-groups with higher visibility"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_parent?(level)
|
|
|
|
errors = visibility_level_errors_for_group(group.parent, level_name)
|
|
|
|
|
|
|
|
reasons << errors[:reason]
|
|
|
|
instructions << errors[:instruction]
|
|
|
|
end
|
|
|
|
|
|
|
|
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
|
|
|
|
"This group cannot be #{level_name}#{reasons}.#{instructions}".html_safe
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def visibility_icon_description(form_model)
|
2018-11-18 11:00:15 +05:30
|
|
|
if form_model.respond_to?(:visibility_level_allowed_as_fork?)
|
2016-06-02 11:05:42 +05:30
|
|
|
project_visibility_icon_description(form_model.visibility_level)
|
2018-11-18 11:00:15 +05:30
|
|
|
elsif form_model.respond_to?(:visibility_level_allowed_by_sub_groups?)
|
2016-06-02 11:05:42 +05:30
|
|
|
group_visibility_icon_description(form_model.visibility_level)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_visibility_icon_description(level)
|
|
|
|
"#{visibility_level_label(level)} - #{group_visibility_level_description(level)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_visibility_icon_description(level)
|
|
|
|
"#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def visibility_level_label(level)
|
2017-09-10 17:25:29 +05:30
|
|
|
# The visibility level can be:
|
|
|
|
# 'VisibilityLevel|Private', 'VisibilityLevel|Internal', 'VisibilityLevel|Public'
|
|
|
|
s_(Project.visibility_levels.key(level))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def restricted_visibility_levels(show_all = false)
|
2017-08-17 22:00:37 +05:30
|
|
|
return [] if current_user.admin? && !show_all
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
Gitlab::CurrentSettings.restricted_visibility_levels || []
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
delegate :default_project_visibility,
|
|
|
|
:default_group_visibility,
|
2018-03-17 18:26:18 +05:30
|
|
|
to: :'Gitlab::CurrentSettings.current_application_settings'
|
|
|
|
|
|
|
|
def disallowed_visibility_level?(form_model, level)
|
|
|
|
return false unless form_model.respond_to?(:visibility_level_allowed?)
|
|
|
|
|
|
|
|
!form_model.visibility_level_allowed?(level)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def visibility_level_errors_for_group(group, level_name)
|
|
|
|
group_name = link_to group.name, group_path(group)
|
|
|
|
change_visiblity = link_to 'change the visibility', edit_group_path(group)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
{ reason: "the visibility of #{group_name} is #{group.visibility}",
|
|
|
|
instruction: " To make this group #{level_name}, you must first #{change_visiblity} of the parent group." }
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|