BigW Consortium Gitlab

visibility_level.rb 1.73 KB
Newer Older
1 2 3 4 5 6 7
# Gitlab::VisibilityLevel module
#
# Define allowed public modes that can be used for
# GitLab projects to determine project public mode
#
module Gitlab
  module VisibilityLevel
8 9
    extend CurrentSettings

10 11 12
    PRIVATE  = 0 unless const_defined?(:PRIVATE)
    INTERNAL = 10 unless const_defined?(:INTERNAL)
    PUBLIC   = 20 unless const_defined?(:PUBLIC)
13 14 15 16 17 18 19 20 21 22 23 24 25

    class << self
      def values
        options.values
      end

      def options
        {
          'Private'  => PRIVATE,
          'Internal' => INTERNAL,
          'Public'   => PUBLIC
        }
      end
26

27
      def allowed_for?(user, level)
28
        user.is_admin? || allowed_level?(level.to_i)
29 30
      end

31 32
      # Return true if the specified level is allowed for the current user.
      # Level should be a numeric value, e.g. `20`.
33
      def allowed_level?(level)
34
        valid_level?(level) && non_restricted_level?(level)
35 36 37
      end

      def non_restricted_level?(level)
38 39 40
        restricted_levels = current_application_settings.restricted_visibility_levels

        if restricted_levels.nil?
41 42
          true
        else
43
          !restricted_levels.include?(level)
44
        end
45 46 47 48
      end

      def valid_level?(level)
        options.has_value?(level)
49
      end
50 51 52 53

      def allowed_fork_levels(origin_level)
        [PRIVATE, INTERNAL, PUBLIC].select{ |level| level <= origin_level }
      end
54 55 56 57

      def level_name(level)
        level_name = 'Unknown'
        options.each do |name, lvl|
58
          level_name = name if lvl == level.to_i
59 60 61 62
        end

        level_name
      end
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    end

    def private?
      visibility_level_field == PRIVATE
    end

    def internal?
      visibility_level_field == INTERNAL
    end

    def public?
      visibility_level_field == PUBLIC
    end
  end
end