2018-08-18 11:19:57 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
module VisibilityLevelHelper
|
|
|
|
def visibility_level_color(level)
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
2014-06-03 11:49:50 +00:00
|
|
|
'vs-private'
|
2013-11-06 15:13:21 +00:00
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
2014-06-03 11:49:50 +00:00
|
|
|
'vs-internal'
|
2013-11-06 15:13:21 +00:00
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
2014-06-03 11:49:50 +00:00
|
|
|
'vs-public'
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 05:01:52 +00:00
|
|
|
# Return the description for the +level+ argument.
|
|
|
|
#
|
2015-12-07 12:37:01 +00:00
|
|
|
# +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-04-26 05:01:52 +00:00
|
|
|
def visibility_level_description(level, form_model)
|
2015-12-07 12:37:01 +00:00
|
|
|
case form_model
|
|
|
|
when Project
|
2015-04-26 05:01:52 +00:00
|
|
|
project_visibility_level_description(level)
|
2016-03-01 15:22:29 +00:00
|
|
|
when Group
|
|
|
|
group_visibility_level_description(level)
|
2015-04-26 05:01:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_visibility_level_description(level)
|
2015-12-02 13:08:59 +00:00
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
2020-02-27 15:09:24 +00:00
|
|
|
_("Project access must be granted explicitly to each user. If this project is part of a group, access will be granted to members of the group.")
|
2015-12-02 13:08:59 +00:00
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
2020-10-30 15:08:59 +00:00
|
|
|
_("The project can be accessed by any logged in user except external users.")
|
2015-12-02 13:08:59 +00:00
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
2017-06-07 20:13:44 +00:00
|
|
|
_("The project can be accessed without any authentication.")
|
2014-10-08 13:44:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-01 15:22:29 +00:00
|
|
|
def group_visibility_level_description(level)
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
2019-04-04 17:05:25 +00:00
|
|
|
_("The group and its projects can only be viewed by members.")
|
2016-03-01 15:22:29 +00:00
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
2020-10-30 15:08:59 +00:00
|
|
|
_("The group and any internal projects can be viewed by any logged in user except external users.")
|
2016-03-01 15:22:29 +00:00
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
2019-04-04 17:05:25 +00:00
|
|
|
_("The group and any public projects can be viewed without any authentication.")
|
2016-03-01 15:22:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-30 19:40:09 +00:00
|
|
|
# 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.
|
2017-08-25 20:01:56 +00:00
|
|
|
def disallowed_project_visibility_level_description(level, project)
|
|
|
|
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
|
|
|
|
reasons = []
|
2018-08-18 11:19:57 +00:00
|
|
|
instructions = []
|
2017-08-25 20:01:56 +00:00
|
|
|
|
|
|
|
unless project.visibility_level_allowed_as_fork?(level)
|
2017-08-30 19:37:08 +00:00
|
|
|
reasons << "the fork source project has lower visibility"
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless project.visibility_level_allowed_by_group?(level)
|
2017-08-30 23:38:06 +00:00
|
|
|
errors = visibility_level_errors_for_group(project.group, level_name)
|
2017-08-30 20:08:30 +00:00
|
|
|
|
2017-08-30 23:38:06 +00:00
|
|
|
reasons << errors[:reason]
|
|
|
|
instructions << errors[:instruction]
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
|
2018-08-18 11:19:57 +00:00
|
|
|
"This project cannot be #{level_name}#{reasons}.#{instructions.join}".html_safe
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
2017-08-30 19:40:09 +00:00
|
|
|
# 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.
|
2017-08-25 20:01:56 +00:00
|
|
|
def disallowed_group_visibility_level_description(level, group)
|
|
|
|
level_name = Gitlab::VisibilityLevel.level_name(level).downcase
|
|
|
|
reasons = []
|
2018-08-18 11:19:57 +00:00
|
|
|
instructions = []
|
2017-08-25 20:01:56 +00:00
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_projects?(level)
|
2017-08-30 19:37:08 +00:00
|
|
|
reasons << "it contains projects with higher visibility"
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_sub_groups?(level)
|
2017-08-30 19:37:08 +00:00
|
|
|
reasons << "it contains sub-groups with higher visibility"
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless group.visibility_level_allowed_by_parent?(level)
|
2017-08-30 23:38:06 +00:00
|
|
|
errors = visibility_level_errors_for_group(group.parent, level_name)
|
2017-08-30 21:01:57 +00:00
|
|
|
|
2017-08-30 23:38:06 +00:00
|
|
|
reasons << errors[:reason]
|
|
|
|
instructions << errors[:instruction]
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
|
2018-08-18 11:19:57 +00:00
|
|
|
"This group cannot be #{level_name}#{reasons}.#{instructions.join}".html_safe
|
2017-08-25 20:01:56 +00:00
|
|
|
end
|
|
|
|
|
2016-03-21 23:09:20 +00:00
|
|
|
def visibility_icon_description(form_model)
|
2018-07-23 09:39:03 +00:00
|
|
|
if form_model.respond_to?(:visibility_level_allowed_as_fork?)
|
2016-03-21 23:09:20 +00:00
|
|
|
project_visibility_icon_description(form_model.visibility_level)
|
2018-07-23 09:39:03 +00:00
|
|
|
elsif form_model.respond_to?(:visibility_level_allowed_by_sub_groups?)
|
2016-03-21 23:09:20 +00:00
|
|
|
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)}"
|
2016-03-20 20:03:53 +00:00
|
|
|
end
|
|
|
|
|
2016-03-21 23:09:20 +00:00
|
|
|
def project_visibility_icon_description(level)
|
2018-12-07 14:11:42 +00:00
|
|
|
"#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
|
2016-03-20 20:03:53 +00:00
|
|
|
end
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
def visibility_level_label(level)
|
2017-06-07 20:13:44 +00:00
|
|
|
# The visibility level can be:
|
|
|
|
# 'VisibilityLevel|Private', 'VisibilityLevel|Internal', 'VisibilityLevel|Public'
|
|
|
|
s_(Project.visibility_levels.key(level))
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
2013-11-27 11:13:33 +00:00
|
|
|
|
2015-03-01 15:06:46 +00:00
|
|
|
def restricted_visibility_levels(show_all = false)
|
2017-04-09 02:20:57 +00:00
|
|
|
return [] if current_user.admin? && !show_all
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.restricted_visibility_levels || []
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
2015-04-26 05:01:52 +00:00
|
|
|
|
2017-02-22 17:51:46 +00:00
|
|
|
delegate :default_project_visibility,
|
|
|
|
:default_group_visibility,
|
2018-02-02 18:39:55 +00:00
|
|
|
to: :'Gitlab::CurrentSettings.current_application_settings'
|
2016-03-09 00:01:33 +00:00
|
|
|
|
2017-08-24 22:24:29 +00:00
|
|
|
def disallowed_visibility_level?(form_model, level)
|
2017-08-24 22:19:49 +00:00
|
|
|
return false unless form_model.respond_to?(:visibility_level_allowed?)
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2017-08-24 22:19:49 +00:00
|
|
|
!form_model.visibility_level_allowed?(level)
|
2015-07-06 12:38:43 +00:00
|
|
|
end
|
2017-08-30 23:38:06 +00:00
|
|
|
|
2019-05-26 05:42:42 +00:00
|
|
|
# Visibility level can be restricted in two ways:
|
|
|
|
#
|
|
|
|
# 1. The group permissions (e.g. a subgroup is private, which requires
|
|
|
|
# all projects to be private)
|
2019-06-06 07:35:24 +00:00
|
|
|
# 2. The global allowed visibility settings, set by the admin
|
2019-05-26 05:42:42 +00:00
|
|
|
def selected_visibility_level(form_model, requested_level)
|
|
|
|
requested_level =
|
|
|
|
if requested_level.present?
|
|
|
|
requested_level.to_i
|
|
|
|
else
|
|
|
|
default_project_visibility
|
|
|
|
end
|
|
|
|
|
|
|
|
[requested_level, max_allowed_visibility_level(form_model)].min
|
|
|
|
end
|
|
|
|
|
2020-05-26 21:07:45 +00:00
|
|
|
def available_visibility_levels(form_model)
|
|
|
|
Gitlab::VisibilityLevel.values.reject do |level|
|
|
|
|
disallowed_visibility_level?(form_model, level) ||
|
|
|
|
restricted_visibility_levels.include?(level)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def snippets_selected_visibility_level(visibility_levels, selected)
|
|
|
|
visibility_levels.find { |level| level == selected } || visibility_levels.min
|
|
|
|
end
|
|
|
|
|
2019-06-28 21:38:26 +00:00
|
|
|
def multiple_visibility_levels_restricted?
|
|
|
|
restricted_visibility_levels.many? # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_visibility_levels_restricted?
|
|
|
|
Gitlab::VisibilityLevel.values == restricted_visibility_levels
|
|
|
|
end
|
|
|
|
|
2017-08-30 23:38:06 +00:00
|
|
|
private
|
|
|
|
|
2019-05-26 05:42:42 +00:00
|
|
|
def max_allowed_visibility_level(form_model)
|
|
|
|
# First obtain the maximum visibility for the project or group
|
|
|
|
current_level = max_allowed_visibility_level_by_model(form_model)
|
|
|
|
|
|
|
|
# Now limit this by the global setting
|
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(current_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_allowed_visibility_level_by_model(form_model)
|
|
|
|
current_level = Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
|
|
|
|
Gitlab::VisibilityLevel.values.sort.each do |value|
|
|
|
|
if disallowed_visibility_level?(form_model, value)
|
|
|
|
break
|
|
|
|
else
|
|
|
|
current_level = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
current_level
|
|
|
|
end
|
|
|
|
|
2017-08-30 23:38:06 +00:00
|
|
|
def visibility_level_errors_for_group(group, level_name)
|
2017-08-31 00:33:24 +00:00
|
|
|
group_name = link_to group.name, group_path(group)
|
2019-10-29 15:07:20 +00:00
|
|
|
change_visibility = link_to 'change the visibility', edit_group_path(group)
|
2017-08-30 23:38:06 +00:00
|
|
|
|
2017-08-31 00:33:24 +00:00
|
|
|
{ reason: "the visibility of #{group_name} is #{group.visibility}",
|
2019-10-29 15:07:20 +00:00
|
|
|
instruction: " To make this group #{level_name}, you must first #{change_visibility} of the parent group." }
|
2017-08-30 23:38:06 +00:00
|
|
|
end
|
2013-11-27 11:13:33 +00:00
|
|
|
end
|