Module: Gitlab::VisibilityLevel

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

Methods included from CurrentSettings

current_application_settings, fake_application_settings

Class Method Details

.allowed_for?(user, level) ⇒ Boolean


35
36
37
# File 'lib/gitlab/visibility_level.rb', line 35

def allowed_for?(user, level)
  user.is_admin? || allowed_level?(level.to_i)
end

.allowed_level?(level) ⇒ Boolean

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


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

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

.level_name(level) ⇒ Object


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

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

  level_name
end

.non_restricted_level?(level) ⇒ Boolean


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

def non_restricted_level?(level)
  restricted_levels = current_application_settings.restricted_visibility_levels

  if restricted_levels.nil?
    true
  else
    !restricted_levels.include?(level)
  end
end

.optionsObject


27
28
29
30
31
32
33
# File 'lib/gitlab/visibility_level.rb', line 27

def options
  {
    'Private'  => PRIVATE,
    'Internal' => INTERNAL,
    'Public'   => PUBLIC
  }
end

.valid_level?(level) ⇒ Boolean


55
56
57
# File 'lib/gitlab/visibility_level.rb', line 55

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

.valuesObject


23
24
25
# File 'lib/gitlab/visibility_level.rb', line 23

def values
  options.values
end

Instance Method Details

#internal?Boolean


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

def internal?
  visibility_level_field == INTERNAL
end

#private?Boolean


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

def private?
  visibility_level_field == PRIVATE
end

#public?Boolean


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

def public?
  visibility_level_field == PUBLIC
end