BigW Consortium Gitlab

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

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

15 16
      scope :public_to_user, -> (user = nil) do
        where(visibility_level: VisibilityLevel.levels_for_user(user))
17
      end
Felipe Artur committed
18
    end
19

20 21 22
    PRIVATE  = 0 unless const_defined?(:PRIVATE)
    INTERNAL = 10 unless const_defined?(:INTERNAL)
    PUBLIC   = 20 unless const_defined?(:PUBLIC)
23 24

    class << self
Douwe Maan committed
25
      delegate :values, to: :options
26

27 28 29
      def levels_for_user(user = nil)
        return [PUBLIC] unless user

30
        if user.full_private_access?
31 32 33 34 35 36 37 38
          [PRIVATE, INTERNAL, PUBLIC]
        elsif user.external?
          [PUBLIC]
        else
          [INTERNAL, PUBLIC]
        end
      end

39 40 41 42
      def string_values
        string_options.keys
      end

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

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

59
      def allowed_levels
60
        restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels
61

62
        self.values - Array(restricted_levels)
63 64 65 66 67 68 69
      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
70 71
      end

72
      def allowed_for?(user, level)
73
        user.admin? || allowed_level?(level.to_i)
74 75
      end

76 77
      # Return true if the specified level is allowed for the current user.
      # Level should be a numeric value, e.g. `20`.
78
      def allowed_level?(level)
79
        valid_level?(level) && non_restricted_level?(level)
80 81 82
      end

      def non_restricted_level?(level)
83
        restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels
84 85

        if restricted_levels.nil?
86 87
          true
        else
88
          !restricted_levels.include?(level)
89
        end
90 91 92
      end

      def valid_level?(level)
93
        options.value?(level)
94
      end
95

96
      def level_name(level)
97
        level_name = N_('VisibilityLevel|Unknown')
98
        options.each do |name, lvl|
99
          level_name = name if lvl == level.to_i
100 101
        end

102
        s_(level_name)
103
      end
104

105
      def level_value(level)
106
        return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)
107

108
        string_options[level] || PRIVATE
109 110
      end

111 112 113
      def string_level(level)
        string_options.key(level)
      end
114 115 116
    end

    def private?
117
      visibility_level_value == PRIVATE
118 119 120
    end

    def internal?
121
      visibility_level_value == INTERNAL
122 123 124
    end

    def public?
125 126 127 128 129 130 131 132 133 134 135 136 137
      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)
138 139 140
    end
  end
end