Module: Gitlab::VisibilityLevel

Extended by:
ActiveSupport::Concern
Included in:
Ci::Catalog::Resource, Namespace, Organizations::Organization, Project, Snippet
Defined in:
lib/gitlab/visibility_level.rb

Constant Summary collapse

PRIVATE =
0
INTERNAL =
10
PUBLIC =
20
LEVELS_FOR_ADMINS =
[PRIVATE, INTERNAL, PUBLIC].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allowed_for?(user, level) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/gitlab/visibility_level.rb', line 78

def allowed_for?(user, level)
  return true if user.can_admin_all_resources?

  allowed_level?(level.to_i)
end

.allowed_level?(level) ⇒ Boolean

Level should be a numeric value, e.g. 20 Return true if the specified level is allowed for the current user.

Returns:

  • (Boolean)


86
87
88
# File 'lib/gitlab/visibility_level.rb', line 86

def allowed_level?(level)
  valid_level?(level) && non_restricted_level?(level)
end

.allowed_levelsObject



65
66
67
68
69
# File 'lib/gitlab/visibility_level.rb', line 65

def allowed_levels
  restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels

  self.values - Array(restricted_levels)
end

.allowed_levels_for_user(user, subject) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gitlab/visibility_level.rb', line 90

def allowed_levels_for_user(user, subject)
  return [] if user.nil?

  visibility_levels = if user.can_admin_all_resources?
                        # admin can create groups even with restricted visibility levels
                        self.values
                      else
                        self.allowed_levels
                      end

  # visibility_level_allowed? is not supporting root-groups, so we have to create a dummy subgroup in the same
  # organization.
  subgroup = Group.new(parent_id: subject.id, organization_id: subject.organization_id)

  # return the allowed visibility levels for the subject
  visibility_levels.select { |level| subgroup.visibility_level_allowed?(level) }
end

.closest_allowed_level(target_level) ⇒ Object



71
72
73
74
75
76
# File 'lib/gitlab/visibility_level.rb', line 71

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
end

.level_name(level) ⇒ Object



130
131
132
# File 'lib/gitlab/visibility_level.rb', line 130

def level_name(level)
  options.key(level.to_i) || s_('VisibilityLevel|Unknown')
end

.level_value(level, fallback_value: PRIVATE) ⇒ Object



134
135
136
137
138
# File 'lib/gitlab/visibility_level.rb', line 134

def level_value(level, fallback_value: PRIVATE)
  return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)

  string_options[level] || fallback_value
end

.levels_for_user(user = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gitlab/visibility_level.rb', line 33

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

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

.non_restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/gitlab/visibility_level.rb', line 108

def non_restricted_level?(level)
  !restricted_level?(level)
end

.optionsObject



49
50
51
52
53
54
55
# File 'lib/gitlab/visibility_level.rb', line 49

def options
  {
    s_('VisibilityLevel|Private') => PRIVATE,
    s_('VisibilityLevel|Internal') => INTERNAL,
    s_('VisibilityLevel|Public') => PUBLIC
  }
end

.public_visibility_restricted?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/gitlab/visibility_level.rb', line 122

def public_visibility_restricted?
  restricted_level?(PUBLIC)
end

.restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
# File 'lib/gitlab/visibility_level.rb', line 112

def restricted_level?(level)
  restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels

  if restricted_levels.nil?
    false
  else
    restricted_levels.include?(level)
  end
end

.string_level(level) ⇒ Object



140
141
142
# File 'lib/gitlab/visibility_level.rb', line 140

def string_level(level)
  string_options.key(level)
end

.string_optionsObject



57
58
59
60
61
62
63
# File 'lib/gitlab/visibility_level.rb', line 57

def string_options
  {
    'private' => PRIVATE,
    'internal' => INTERNAL,
    'public' => PUBLIC
  }
end

.string_valuesObject



45
46
47
# File 'lib/gitlab/visibility_level.rb', line 45

def string_values
  string_options.keys
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/gitlab/visibility_level.rb', line 126

def valid_level?(level)
  options.value?(level)
end

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/gitlab/visibility_level.rb', line 153

def internal?
  visibility_level_value == INTERNAL
end

#private?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/gitlab/visibility_level.rb', line 149

def private?
  visibility_level_value == PRIVATE
end

#public?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/gitlab/visibility_level.rb', line 157

def public?
  visibility_level_value == PUBLIC
end

#visibilityObject



165
166
167
# File 'lib/gitlab/visibility_level.rb', line 165

def visibility
  Gitlab::VisibilityLevel.string_level(visibility_level_value)
end

#visibility=(level) ⇒ Object



169
170
171
# File 'lib/gitlab/visibility_level.rb', line 169

def visibility=(level)
  self[visibility_level_field] = Gitlab::VisibilityLevel.level_value(level)
end

#visibility_attribute_present?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
# File 'lib/gitlab/visibility_level.rb', line 173

def visibility_attribute_present?(attributes)
  visibility_level_attributes.each do |attr|
    return true if attributes[attr].present?
  end

  false
end

#visibility_attribute_value(attributes) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/gitlab/visibility_level.rb', line 181

def visibility_attribute_value(attributes)
  visibility_level_attributes.each do |attr|
    return attributes[attr] if attributes.has_key?(attr)
  end

  nil
end

#visibility_level_attributesObject



189
190
191
192
# File 'lib/gitlab/visibility_level.rb', line 189

def visibility_level_attributes
  [visibility_level_field, visibility_level_field.to_s,
    :visibility, 'visibility']
end

#visibility_level_previous_changesObject



145
146
147
# File 'lib/gitlab/visibility_level.rb', line 145

def visibility_level_previous_changes
  previous_changes[:visibility_level]
end

#visibility_level_valueObject



161
162
163
# File 'lib/gitlab/visibility_level.rb', line 161

def visibility_level_value
  self[visibility_level_field]
end