debian-mirror-gitlab/lib/gitlab/visibility_level.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

174 lines
4.3 KiB
Ruby
Raw Normal View History

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