BigW Consortium Gitlab

visibility_level.rb 3.17 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
    extend CurrentSettings
Felipe Artur committed
9 10 11
    extend ActiveSupport::Concern

    included do
12 13
      scope :public_only,               -> { where(visibility_level: PUBLIC) }
      scope :public_and_internal_only,  -> { where(visibility_level: [PUBLIC, INTERNAL] ) }
14
      scope :non_public_only,           -> { where.not(visibility_level: PUBLIC) }
15

16 17 18 19 20 21 22 23 24 25 26 27 28
      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
Felipe Artur committed
29
    end
30

31 32 33
    PRIVATE  = 0 unless const_defined?(:PRIVATE)
    INTERNAL = 10 unless const_defined?(:INTERNAL)
    PUBLIC   = 20 unless const_defined?(:PUBLIC)
34 35

    class << self
Douwe Maan committed
36
      delegate :values, to: :options
37

38 39 40 41
      def string_values
        string_options.keys
      end

42 43
      def options
        {
44 45 46
          N_('VisibilityLevel|Private')  => PRIVATE,
          N_('VisibilityLevel|Internal') => INTERNAL,
          N_('VisibilityLevel|Public')   => PUBLIC
47 48
        }
      end
49

50 51 52 53 54 55 56 57
      def string_options
        {
          'private'  => PRIVATE,
          'internal' => INTERNAL,
          'public'   => PUBLIC
        }
      end

58 59 60 61 62 63 64
      def highest_allowed_level
        restricted_levels = current_application_settings.restricted_visibility_levels

        allowed_levels = self.values - restricted_levels
        allowed_levels.max || PRIVATE
      end

65
      def allowed_for?(user, level)
66
        user.admin? || allowed_level?(level.to_i)
67 68
      end

69 70
      # Return true if the specified level is allowed for the current user.
      # Level should be a numeric value, e.g. `20`.
71
      def allowed_level?(level)
72
        valid_level?(level) && non_restricted_level?(level)
73 74 75
      end

      def non_restricted_level?(level)
76 77 78
        restricted_levels = current_application_settings.restricted_visibility_levels

        if restricted_levels.nil?
79 80
          true
        else
81
          !restricted_levels.include?(level)
82
        end
83 84 85
      end

      def valid_level?(level)
86
        options.value?(level)
87
      end
88

89 90 91
      def level_name(level)
        level_name = 'Unknown'
        options.each do |name, lvl|
92
          level_name = name if lvl == level.to_i
93 94 95 96
        end

        level_name
      end
97

98
      def level_value(level)
99 100
        return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)
        string_options[level] || PRIVATE
101 102
      end

103 104 105
      def string_level(level)
        string_options.key(level)
      end
106 107 108
    end

    def private?
109
      visibility_level_value == PRIVATE
110 111 112
    end

    def internal?
113
      visibility_level_value == INTERNAL
114 115 116
    end

    def public?
117 118 119 120 121 122 123 124 125 126 127 128 129
      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)
130 131 132
    end
  end
end