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
|
|
|
|
|
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
|
|
|
|
def values
|
|
|
|
options.values
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
{
|
|
|
|
'Private' => PRIVATE,
|
|
|
|
'Internal' => INTERNAL,
|
|
|
|
'Public' => PUBLIC
|
|
|
|
}
|
|
|
|
end
|
2014-07-16 14:12:46 -04:00
|
|
|
|
2013-11-06 10:13:21 -05:00
|
|
|
def allowed_for?(user, level)
|
2015-03-01 10:06:46 -05:00
|
|
|
user.is_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)
|
|
|
|
options.has_value?(level)
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
2015-07-06 08:38:43 -04:00
|
|
|
|
|
|
|
def allowed_fork_levels(origin_level)
|
|
|
|
[PRIVATE, INTERNAL, PUBLIC].select{ |level| level <= origin_level }
|
|
|
|
end
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def private?
|
|
|
|
visibility_level_field == PRIVATE
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
|
|
|
visibility_level_field == INTERNAL
|
|
|
|
end
|
|
|
|
|
|
|
|
def public?
|
|
|
|
visibility_level_field == PUBLIC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|