2013-11-06 10:13:21 -05:00
|
|
|
# Gitlab::VisibilityLevel module
|
|
|
|
#
|
|
|
|
# Define allowed public modes that can be used for
|
|
|
|
# GitLab projects to determine project public mode
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module VisibilityLevel
|
2015-03-01 10:06:46 -05:00
|
|
|
extend CurrentSettings
|
2016-03-17 18:42:46 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2016-03-18 08:28:16 -04:00
|
|
|
scope :public_only, -> { where(visibility_level: PUBLIC) }
|
|
|
|
scope :public_and_internal_only, -> { where(visibility_level: [PUBLIC, INTERNAL] ) }
|
2017-01-20 06:25:53 -05:00
|
|
|
scope :non_public_only, -> { where.not(visibility_level: PUBLIC) }
|
2016-03-20 16:03:53 -04:00
|
|
|
|
2017-02-06 21:32:34 -05:00
|
|
|
scope :public_to_user, -> (user) do
|
|
|
|
if user
|
|
|
|
if user.admin?
|
|
|
|
all
|
|
|
|
elsif !user.external?
|
|
|
|
public_and_internal_only
|
|
|
|
else
|
|
|
|
public_only
|
|
|
|
end
|
|
|
|
else
|
|
|
|
public_only
|
|
|
|
end
|
|
|
|
end
|
2016-03-17 18:42:46 -04:00
|
|
|
end
|
2015-03-01 10:06:46 -05:00
|
|
|
|
2014-07-16 14:12:46 -04:00
|
|
|
PRIVATE = 0 unless const_defined?(:PRIVATE)
|
|
|
|
INTERNAL = 10 unless const_defined?(:INTERNAL)
|
|
|
|
PUBLIC = 20 unless const_defined?(:PUBLIC)
|
2013-11-06 10:13:21 -05:00
|
|
|
|
|
|
|
class << self
|
2017-02-22 12:51:46 -05:00
|
|
|
delegate :values, to: :options
|
2013-11-06 10:13:21 -05:00
|
|
|
|
2017-02-16 05:06:40 -05:00
|
|
|
def string_values
|
|
|
|
string_options.keys
|
|
|
|
end
|
|
|
|
|
2013-11-06 10:13:21 -05:00
|
|
|
def options
|
|
|
|
{
|
2017-06-07 16:13:44 -04:00
|
|
|
N_('VisibilityLevel|Private') => PRIVATE,
|
|
|
|
N_('VisibilityLevel|Internal') => INTERNAL,
|
|
|
|
N_('VisibilityLevel|Public') => PUBLIC
|
2013-11-06 10:13:21 -05:00
|
|
|
}
|
|
|
|
end
|
2014-07-16 14:12:46 -04:00
|
|
|
|
2017-02-16 05:06:40 -05:00
|
|
|
def string_options
|
|
|
|
{
|
|
|
|
'private' => PRIVATE,
|
|
|
|
'internal' => INTERNAL,
|
|
|
|
'public' => PUBLIC
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-05-24 17:53:53 -04:00
|
|
|
def highest_allowed_level
|
|
|
|
restricted_levels = current_application_settings.restricted_visibility_levels
|
|
|
|
|
|
|
|
allowed_levels = self.values - restricted_levels
|
|
|
|
allowed_levels.max || PRIVATE
|
|
|
|
end
|
|
|
|
|
2013-11-06 10:13:21 -05:00
|
|
|
def allowed_for?(user, level)
|
2017-04-08 22:20:57 -04:00
|
|
|
user.admin? || allowed_level?(level.to_i)
|
2014-08-28 14:33:41 -04:00
|
|
|
end
|
|
|
|
|
2015-03-01 10:06:46 -05:00
|
|
|
# Return true if the specified level is allowed for the current user.
|
|
|
|
# Level should be a numeric value, e.g. `20`.
|
2014-08-28 14:33:41 -04:00
|
|
|
def allowed_level?(level)
|
2015-03-01 10:06:46 -05:00
|
|
|
valid_level?(level) && non_restricted_level?(level)
|
2014-08-28 14:33:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def non_restricted_level?(level)
|
2015-03-18 16:55:41 -04:00
|
|
|
restricted_levels = current_application_settings.restricted_visibility_levels
|
|
|
|
|
|
|
|
if restricted_levels.nil?
|
2015-03-18 14:38:58 -04:00
|
|
|
true
|
|
|
|
else
|
2015-03-18 16:55:41 -04:00
|
|
|
!restricted_levels.include?(level)
|
2015-03-18 14:38:58 -04:00
|
|
|
end
|
2015-03-01 10:06:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_level?(level)
|
2017-06-02 13:11:26 -04:00
|
|
|
options.value?(level)
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
2015-07-06 08:38:43 -04:00
|
|
|
|
2015-11-03 11:23:19 -05:00
|
|
|
def level_name(level)
|
|
|
|
level_name = 'Unknown'
|
|
|
|
options.each do |name, lvl|
|
2015-11-04 16:13:40 -05:00
|
|
|
level_name = name if lvl == level.to_i
|
2015-11-03 11:23:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
level_name
|
|
|
|
end
|
2017-02-16 05:06:40 -05:00
|
|
|
|
2017-03-01 15:23:00 -05:00
|
|
|
def level_value(level)
|
2017-03-20 17:05:53 -04:00
|
|
|
return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)
|
|
|
|
string_options[level] || PRIVATE
|
2017-03-01 15:23:00 -05:00
|
|
|
end
|
|
|
|
|
2017-02-16 05:06:40 -05:00
|
|
|
def string_level(level)
|
|
|
|
string_options.key(level)
|
|
|
|
end
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def private?
|
2017-03-01 15:23:00 -05:00
|
|
|
visibility_level_value == PRIVATE
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
2017-03-01 15:23:00 -05:00
|
|
|
visibility_level_value == INTERNAL
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def public?
|
2017-03-01 15:23:00 -05: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 10:13:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|