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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allowed_for?(user, level) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/gitlab/visibility_level.rb', line 77

def allowed_for?(user, level)
  user.admin? || 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)


83
84
85
# File 'lib/gitlab/visibility_level.rb', line 83

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

.allowed_levelsObject



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

def allowed_levels
  restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels

  self.values - Array(restricted_levels)
end

.allowed_levels_for_user(user, subject) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gitlab/visibility_level.rb', line 87

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 sub-group.
  subgroup = Group.new(parent_id: subject.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



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

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



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

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

.level_value(level, fallback_value: PRIVATE) ⇒ Object



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

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



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

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

.non_restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/gitlab/visibility_level.rb', line 104

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

.optionsObject



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

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

.public_visibility_restricted?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/gitlab/visibility_level.rb', line 118

def public_visibility_restricted?
  restricted_level?(PUBLIC)
end

.restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
# File 'lib/gitlab/visibility_level.rb', line 108

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



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

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

.string_optionsObject



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

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

.string_valuesObject



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

def string_values
  string_options.keys
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


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

def internal?
  visibility_level_value == INTERNAL
end

#private?Boolean

Returns:

  • (Boolean)


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

def private?
  visibility_level_value == PRIVATE
end

#public?Boolean

Returns:

  • (Boolean)


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

def public?
  visibility_level_value == PUBLIC
end

#visibilityObject



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

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

#visibility=(level) ⇒ Object



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

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

#visibility_attribute_present?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
# File 'lib/gitlab/visibility_level.rb', line 169

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



177
178
179
180
181
182
183
# File 'lib/gitlab/visibility_level.rb', line 177

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



185
186
187
188
# File 'lib/gitlab/visibility_level.rb', line 185

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

#visibility_level_previous_changesObject



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

def visibility_level_previous_changes
  previous_changes[:visibility_level]
end

#visibility_level_valueObject



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

def visibility_level_value
  self[visibility_level_field]
end