Module: Gitlab::VisibilityLevel

Extended by:
ActiveSupport::Concern
Included in:
Namespace, 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)

75
76
77
# File 'lib/gitlab/visibility_level.rb', line 75

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)

81
82
83
# File 'lib/gitlab/visibility_level.rb', line 81

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

.allowed_levelsObject


62
63
64
65
66
# File 'lib/gitlab/visibility_level.rb', line 62

def allowed_levels
  restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels

  self.values - Array(restricted_levels)
end

.closest_allowed_level(target_level) ⇒ Object


68
69
70
71
72
73
# File 'lib/gitlab/visibility_level.rb', line 68

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


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

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

.level_value(level) ⇒ Object


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

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

  string_options[level] || PRIVATE
end

.levels_for_user(user = nil) ⇒ Object


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

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)

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

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

.optionsObject


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

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

.public_visibility_restricted?Boolean

Returns:

  • (Boolean)

99
100
101
# File 'lib/gitlab/visibility_level.rb', line 99

def public_visibility_restricted?
  restricted_level?(PUBLIC)
end

.restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)

89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/visibility_level.rb', line 89

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


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

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

.string_optionsObject


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

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

.string_valuesObject


42
43
44
# File 'lib/gitlab/visibility_level.rb', line 42

def string_values
  string_options.keys
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)

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

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

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)

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

def internal?
  visibility_level_value == INTERNAL
end

#private?Boolean

Returns:

  • (Boolean)

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

def private?
  visibility_level_value == PRIVATE
end

#public?Boolean

Returns:

  • (Boolean)

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

def public?
  visibility_level_value == PUBLIC
end

#visibilityObject


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

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

#visibility=(level) ⇒ Object


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

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

#visibility_attribute_present?(attributes) ⇒ Boolean

Returns:

  • (Boolean)

150
151
152
153
154
155
156
# File 'lib/gitlab/visibility_level.rb', line 150

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


158
159
160
161
162
163
164
# File 'lib/gitlab/visibility_level.rb', line 158

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


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

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

#visibility_level_previous_changesObject


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

def visibility_level_previous_changes
  previous_changes[:visibility_level]
end

#visibility_level_valueObject


138
139
140
# File 'lib/gitlab/visibility_level.rb', line 138

def visibility_level_value
  self[visibility_level_field]
end