2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
# Gitlab::VisibilityLevel module
|
|
|
|
#
|
|
|
|
# Define allowed public modes that can be used for
|
|
|
|
# GitLab projects to determine project public mode
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module VisibilityLevel
|
2016-03-17 22:42:46 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2016-03-18 12:28:16 +00:00
|
|
|
scope :public_only, -> { where(visibility_level: PUBLIC) }
|
|
|
|
scope :public_and_internal_only, -> { where(visibility_level: [PUBLIC, INTERNAL] ) }
|
2017-01-20 11:25:53 +00:00
|
|
|
scope :non_public_only, -> { where.not(visibility_level: PUBLIC) }
|
2016-03-20 20:03:53 +00:00
|
|
|
|
2017-06-13 14:44:55 +00:00
|
|
|
scope :public_to_user, -> (user = nil) do
|
|
|
|
where(visibility_level: VisibilityLevel.levels_for_user(user))
|
2017-02-07 02:32:34 +00:00
|
|
|
end
|
2016-03-17 22:42:46 +00:00
|
|
|
end
|
2015-03-01 15:06:46 +00:00
|
|
|
|
2014-07-16 18:12:46 +00:00
|
|
|
PRIVATE = 0 unless const_defined?(:PRIVATE)
|
|
|
|
INTERNAL = 10 unless const_defined?(:INTERNAL)
|
|
|
|
PUBLIC = 20 unless const_defined?(:PUBLIC)
|
2013-11-06 15:13:21 +00:00
|
|
|
|
|
|
|
class << self
|
2017-02-22 17:51:46 +00:00
|
|
|
delegate :values, to: :options
|
2013-11-06 15:13:21 +00:00
|
|
|
|
2017-06-13 14:44:55 +00:00
|
|
|
def levels_for_user(user = nil)
|
|
|
|
return [PUBLIC] unless user
|
|
|
|
|
2019-12-17 09:07:48 +00:00
|
|
|
if user.can_read_all_resources?
|
2017-06-13 14:44:55 +00:00
|
|
|
[PRIVATE, INTERNAL, PUBLIC]
|
|
|
|
elsif user.external?
|
|
|
|
[PUBLIC]
|
|
|
|
else
|
|
|
|
[INTERNAL, PUBLIC]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-16 10:06:40 +00:00
|
|
|
def string_values
|
|
|
|
string_options.keys
|
|
|
|
end
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
def options
|
|
|
|
{
|
2021-03-05 12:08:55 +00:00
|
|
|
s_('VisibilityLevel|Private') => PRIVATE,
|
|
|
|
s_('VisibilityLevel|Internal') => INTERNAL,
|
|
|
|
s_('VisibilityLevel|Public') => PUBLIC
|
2013-11-06 15:13:21 +00:00
|
|
|
}
|
|
|
|
end
|
2014-07-16 18:12:46 +00:00
|
|
|
|
2017-02-16 10:06:40 +00:00
|
|
|
def string_options
|
|
|
|
{
|
|
|
|
'private' => PRIVATE,
|
|
|
|
'internal' => INTERNAL,
|
|
|
|
'public' => PUBLIC
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-12-20 15:19:54 +00:00
|
|
|
def allowed_levels
|
2018-02-02 18:39:55 +00:00
|
|
|
restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels
|
2016-05-24 21:53:53 +00:00
|
|
|
|
2018-02-02 10:23:05 +00:00
|
|
|
self.values - Array(restricted_levels)
|
2017-12-20 15:19:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def closest_allowed_level(target_level)
|
|
|
|
highest_allowed_level = allowed_levels.select { |level| level <= target_level }.max
|
|
|
|
|
|
|
|
# If all levels are restricted, fall back to PRIVATE
|
|
|
|
highest_allowed_level || PRIVATE
|
2016-05-24 21:53:53 +00:00
|
|
|
end
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
def allowed_for?(user, level)
|
2017-04-09 02:20:57 +00:00
|
|
|
user.admin? || allowed_level?(level.to_i)
|
2014-08-28 18:33:41 +00:00
|
|
|
end
|
|
|
|
|
2019-04-12 05:00:50 +00:00
|
|
|
# Level should be a numeric value, e.g. `20`
|
2015-03-01 15:06:46 +00:00
|
|
|
# Return true if the specified level is allowed for the current user.
|
2014-08-28 18:33:41 +00:00
|
|
|
def allowed_level?(level)
|
2015-03-01 15:06:46 +00:00
|
|
|
valid_level?(level) && non_restricted_level?(level)
|
2014-08-28 18:33:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def non_restricted_level?(level)
|
2020-04-27 18:09:41 +00:00
|
|
|
!restricted_level?(level)
|
|
|
|
end
|
|
|
|
|
|
|
|
def restricted_level?(level)
|
2018-02-02 18:39:55 +00:00
|
|
|
restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels
|
2015-03-18 20:55:41 +00:00
|
|
|
|
|
|
|
if restricted_levels.nil?
|
2020-04-27 18:09:41 +00:00
|
|
|
false
|
2015-03-18 18:38:58 +00:00
|
|
|
else
|
2020-04-27 18:09:41 +00:00
|
|
|
restricted_levels.include?(level)
|
2015-03-18 18:38:58 +00:00
|
|
|
end
|
2015-03-01 15:06:46 +00:00
|
|
|
end
|
|
|
|
|
2020-04-27 18:09:41 +00:00
|
|
|
def public_visibility_restricted?
|
|
|
|
restricted_level?(PUBLIC)
|
|
|
|
end
|
|
|
|
|
2015-03-01 15:06:46 +00:00
|
|
|
def valid_level?(level)
|
2017-06-02 17:11:26 +00:00
|
|
|
options.value?(level)
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
2015-07-06 12:38:43 +00:00
|
|
|
|
2015-11-03 16:23:19 +00:00
|
|
|
def level_name(level)
|
2021-03-05 12:08:55 +00:00
|
|
|
options.key(level.to_i) || s_('VisibilityLevel|Unknown')
|
2015-11-03 16:23:19 +00:00
|
|
|
end
|
2017-02-16 10:06:40 +00:00
|
|
|
|
2017-03-01 20:23:00 +00:00
|
|
|
def level_value(level)
|
2017-03-20 21:05:53 +00:00
|
|
|
return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2017-03-20 21:05:53 +00:00
|
|
|
string_options[level] || PRIVATE
|
2017-03-01 20:23:00 +00:00
|
|
|
end
|
|
|
|
|
2017-02-16 10:06:40 +00:00
|
|
|
def string_level(level)
|
|
|
|
string_options.key(level)
|
|
|
|
end
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
|
2019-11-28 15:06:57 +00:00
|
|
|
def visibility_level_previous_changes
|
|
|
|
previous_changes[:visibility_level]
|
|
|
|
end
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
def private?
|
2017-03-01 20:23:00 +00:00
|
|
|
visibility_level_value == PRIVATE
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
2017-03-01 20:23:00 +00:00
|
|
|
visibility_level_value == INTERNAL
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def public?
|
2017-03-01 20:23:00 +00:00
|
|
|
visibility_level_value == PUBLIC
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility_level_value
|
|
|
|
self[visibility_level_field]
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility
|
|
|
|
Gitlab::VisibilityLevel.string_level(visibility_level_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility=(level)
|
|
|
|
self[visibility_level_field] = Gitlab::VisibilityLevel.level_value(level)
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
2019-06-12 23:03:46 +00:00
|
|
|
|
|
|
|
def visibility_attribute_present?(attributes)
|
|
|
|
visibility_level_attributes.each do |attr|
|
|
|
|
return true if attributes[attr].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility_level_attributes
|
|
|
|
[visibility_level_field, visibility_level_field.to_s,
|
|
|
|
:visibility, 'visibility']
|
|
|
|
end
|
2013-11-06 15:13:21 +00:00
|
|
|
end
|
|
|
|
end
|