2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
# 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)
|
|
|
|
# 2. The global allowed visibility settings, set by the admin
|
|
|
|
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-06-23 00:09:42 +05:30
|
|
|
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
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def visibility_level_options(form_model)
|
|
|
|
available_visibility_levels(form_model).map do |level|
|
|
|
|
{
|
|
|
|
level: level,
|
|
|
|
label: visibility_level_label(level),
|
|
|
|
description: visibility_level_description(level, form_model)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def snippets_selected_visibility_level(visibility_levels, selected)
|
|
|
|
visibility_levels.find { |level| level == selected } || visibility_levels.min
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
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
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def project_visibility_level_description(level)
|
|
|
|
case level
|
|
|
|
when Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
_("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.")
|
|
|
|
when Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
_("The project can be accessed by any logged in user except external users.")
|
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
_("The project can be accessed without any authentication.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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 except external users.")
|
|
|
|
when Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
_("The group and any public projects can be viewed without any authentication.")
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def project_visibility_icon_description(level)
|
|
|
|
"#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_visibility_icon_description(level)
|
|
|
|
"#{visibility_level_label(level)} - #{group_visibility_level_description(level)}"
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|