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)

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

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)

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

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

.allowed_levelsObject


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

def allowed_levels
  restricted_levels = Gitlab::CurrentSettings.restricted_visibility_levels

  self.values - Array(restricted_levels)
end

.closest_allowed_level(target_level) ⇒ Object


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

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


106
107
108
109
110
111
112
113
# File 'lib/gitlab/visibility_level.rb', line 106

def level_name(level)
  level_name = N_('VisibilityLevel|Unknown')
  options.each do |name, lvl|
    level_name = name if lvl == level.to_i
  end

  s_(level_name)
end

.level_value(level) ⇒ Object


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

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


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

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)

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

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

.optionsObject


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

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

.public_visibility_restricted?Boolean

Returns:

  • (Boolean)

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

def public_visibility_restricted?
  restricted_level?(PUBLIC)
end

.restricted_level?(level) ⇒ Boolean

Returns:

  • (Boolean)

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

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


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

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

.string_optionsObject


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

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

.string_valuesObject


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

def string_values
  string_options.keys
end

.valid_level?(level) ⇒ Boolean

Returns:

  • (Boolean)

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

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

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)

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

def internal?
  visibility_level_value == INTERNAL
end

#private?Boolean

Returns:

  • (Boolean)

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

def private?
  visibility_level_value == PRIVATE
end

#public?Boolean

Returns:

  • (Boolean)

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

def public?
  visibility_level_value == PUBLIC
end

#visibilityObject


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

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

#visibility=(level) ⇒ Object


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

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

#visibility_attribute_present?(attributes) ⇒ Boolean

Returns:

  • (Boolean)

162
163
164
165
166
167
168
# File 'lib/gitlab/visibility_level.rb', line 162

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

  false
end

#visibility_level_attributesObject


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

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

#visibility_level_decreased?Boolean

Returns:

  • (Boolean)

126
127
128
129
130
131
132
# File 'lib/gitlab/visibility_level.rb', line 126

def visibility_level_decreased?
  return false unless visibility_level_previous_changes

  before, after = visibility_level_previous_changes

  before && after && after < before
end

#visibility_level_previous_changesObject


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

def visibility_level_previous_changes
  previous_changes[:visibility_level]
end

#visibility_level_valueObject


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

def visibility_level_value
  self[visibility_level_field]
end