summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/user_mentions/lib/gitlab/isolated_visibility_level.rb
blob: 0334ea1dd0843741bb4b76a07b502cd522bbcf44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    module UserMentions
      module Lib
        module Gitlab
          # Gitlab::IsolatedVisibilityLevel module
          #
          # Define allowed public modes that can be used for
          # GitLab projects to determine project public mode
          #
          module IsolatedVisibilityLevel
            extend ::ActiveSupport::Concern

            included do
              scope :public_to_user, -> (user = nil) do
                where(visibility_level: IsolatedVisibilityLevel.levels_for_user(user))
              end
            end

            PRIVATE  = 0 unless const_defined?(:PRIVATE)
            INTERNAL = 10 unless const_defined?(:INTERNAL)
            PUBLIC   = 20 unless const_defined?(:PUBLIC)

            class << self
              def levels_for_user(user = nil)
                return [PUBLIC] unless user

                if user.can_read_all_resources?
                  [PRIVATE, INTERNAL, PUBLIC]
                elsif user.external?
                  [PUBLIC]
                else
                  [INTERNAL, PUBLIC]
                end
              end
            end

            def private?
              visibility_level_value == PRIVATE
            end

            def internal?
              visibility_level_value == INTERNAL
            end

            def public?
              visibility_level_value == PUBLIC
            end

            def visibility_level_value
              self[visibility_level_field]
            end
          end
        end
      end
    end
  end
end