visibility_level_helper.rb 5.1 KB
Newer Older
1 2 3 4
module VisibilityLevelHelper
  def visibility_level_color(level)
    case level
    when Gitlab::VisibilityLevel::PRIVATE
5
      'vs-private'
6
    when Gitlab::VisibilityLevel::INTERNAL
7
      'vs-internal'
8
    when Gitlab::VisibilityLevel::PUBLIC
9
      'vs-public'
10 11 12
    end
  end

V
Vinnie Okada 已提交
13 14
  # Return the description for the +level+ argument.
  #
15 16 17
  # +level+       One of the Gitlab::VisibilityLevel constants
  # +form_model+  Either a model object (Project, Snippet, etc.) or the name of
  #               a Project or Snippet class.
V
Vinnie Okada 已提交
18
  def visibility_level_description(level, form_model)
19 20
    case form_model
    when Project
V
Vinnie Okada 已提交
21
      project_visibility_level_description(level)
F
Felipe Artur 已提交
22 23
    when Group
      group_visibility_level_description(level)
24 25
    when Snippet
      snippet_visibility_level_description(level, form_model)
V
Vinnie Okada 已提交
26 27 28 29
    end
  end

  def project_visibility_level_description(level)
30 31
    case level
    when Gitlab::VisibilityLevel::PRIVATE
32
      _("Project access must be granted explicitly to each user.")
33
    when Gitlab::VisibilityLevel::INTERNAL
34
      _("The project can be accessed by any logged in user.")
35
    when Gitlab::VisibilityLevel::PUBLIC
36
      _("The project can be accessed without any authentication.")
V
Valery Sizov 已提交
37 38 39
    end
  end

F
Felipe Artur 已提交
40 41 42
  def group_visibility_level_description(level)
    case level
    when Gitlab::VisibilityLevel::PRIVATE
D
Douwe Maan 已提交
43
      "The group and its projects can only be viewed by members."
F
Felipe Artur 已提交
44
    when Gitlab::VisibilityLevel::INTERNAL
D
Douwe Maan 已提交
45
      "The group and any internal projects can be viewed by any logged in user."
F
Felipe Artur 已提交
46
    when Gitlab::VisibilityLevel::PUBLIC
D
Douwe Maan 已提交
47
      "The group and any public projects can be viewed without any authentication."
F
Felipe Artur 已提交
48 49 50
    end
  end

51
  def snippet_visibility_level_description(level, snippet = nil)
52 53
    case level
    when Gitlab::VisibilityLevel::PRIVATE
54 55 56 57 58
      if snippet.is_a? ProjectSnippet
        "The snippet is visible only to project members."
      else
        "The snippet is visible only to me."
      end
59
    when Gitlab::VisibilityLevel::INTERNAL
60
      "The snippet is visible to any logged in user."
61
    when Gitlab::VisibilityLevel::PUBLIC
62
      "The snippet can be accessed without any authentication."
63 64 65
    end
  end

66 67
  def restricted_visibility_level_description(level)
    level_name = Gitlab::VisibilityLevel.level_name(level)
68
    "#{level_name.capitalize} visibility has been restricted by the administrator."
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  end

  def disallowed_visibility_level_description(level, form_model)
    case form_model
    when Project
      disallowed_project_visibility_level_description(level, form_model)
    when Group
      disallowed_group_visibility_level_description(level, form_model)
    end
  end

  def disallowed_project_visibility_level_description(level, project)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []

    unless project.visibility_level_allowed_as_fork?(level)
85
      reasons << project.visibility_error_for(:fork, level: level_name)
86 87 88
    end

    unless project.visibility_level_allowed_by_group?(level)
89
      reasons << project.visibility_error_for(:group, level: level_name, group_level: project.group.visibility)
90 91 92 93 94 95 96 97 98 99 100
    end

    reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
    "This project cannot be #{level_name}#{reasons}."
  end

  def disallowed_group_visibility_level_description(level, group)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []

    unless group.visibility_level_allowed_by_projects?(level)
101
      reasons << group.visibility_error_for(:projects, level: level_name)
102 103 104
    end

    unless group.visibility_level_allowed_by_sub_groups?(level)
105
      reasons << group.visibility_error_for(:sub_groups, level: level_name)
106 107 108
    end

    unless group.visibility_level_allowed_by_parent?(level)
109
      reasons << group.visibility_error_for(:parent, level: level_name, parent_level: group.parent.visibility)
110 111 112 113 114 115
    end

    reasons = reasons.any? ? ' because ' + reasons.to_sentence : ''
    "This group cannot be #{level_name}#{reasons}."
  end

D
Douwe Maan 已提交
116 117 118 119 120 121 122 123 124 125 126
  def visibility_icon_description(form_model)
    case form_model
    when Project
      project_visibility_icon_description(form_model.visibility_level)
    when Group
      group_visibility_icon_description(form_model.visibility_level)
    end
  end

  def group_visibility_icon_description(level)
    "#{visibility_level_label(level)} - #{group_visibility_level_description(level)}"
D
Douwe Maan 已提交
127 128
  end

D
Douwe Maan 已提交
129 130
  def project_visibility_icon_description(level)
    "#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
D
Douwe Maan 已提交
131 132
  end

133
  def visibility_level_label(level)
134 135 136
    # The visibility level can be:
    # 'VisibilityLevel|Private', 'VisibilityLevel|Internal', 'VisibilityLevel|Public'
    s_(Project.visibility_levels.key(level))
137
  end
138

139
  def restricted_visibility_levels(show_all = false)
B
blackst0ne 已提交
140
    return [] if current_user.admin? && !show_all
141
    current_application_settings.restricted_visibility_levels || []
142
  end
V
Vinnie Okada 已提交
143

D
Douwe Maan 已提交
144 145 146
  delegate  :default_project_visibility,
            :default_group_visibility,
            to: :current_application_settings
147

148
  def disallowed_visibility_level?(form_model, level)
149 150
    return false unless form_model.respond_to?(:visibility_level_allowed?)
    !form_model.visibility_level_allowed?(level)
V
Valery Sizov 已提交
151
  end
152
end