visibility_level_helper.rb 6.0 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
  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

80 81
  # Note: these messages closely mirror the form validation strings found in the project
  # model and any changes or additons to these may also need to be made there.
82 83 84
  def disallowed_project_visibility_level_description(level, project)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []
85
    instructions = ''
86 87

    unless project.visibility_level_allowed_as_fork?(level)
88
      reasons << "the fork source project has lower visibility"
89 90 91
    end

    unless project.visibility_level_allowed_by_group?(level)
R
Rubén Dávila 已提交
92
      errors = visibility_level_errors_for_group(project.group, level_name)
93

R
Rubén Dávila 已提交
94 95
      reasons << errors[:reason]
      instructions << errors[:instruction]
96 97 98
    end

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

102 103
  # Note: these messages closely mirror the form validation strings found in the group
  # model and any changes or additons to these may also need to be made there.
104 105 106
  def disallowed_group_visibility_level_description(level, group)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []
107
    instructions = ''
108 109

    unless group.visibility_level_allowed_by_projects?(level)
110
      reasons << "it contains projects with higher visibility"
111 112 113
    end

    unless group.visibility_level_allowed_by_sub_groups?(level)
114
      reasons << "it contains sub-groups with higher visibility"
115 116 117
    end

    unless group.visibility_level_allowed_by_parent?(level)
R
Rubén Dávila 已提交
118
      errors = visibility_level_errors_for_group(group.parent, level_name)
119

R
Rubén Dávila 已提交
120 121
      reasons << errors[:reason]
      instructions << errors[:instruction]
122 123 124
    end

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

D
Douwe Maan 已提交
128 129 130 131 132 133 134 135 136 137 138
  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 已提交
139 140
  end

D
Douwe Maan 已提交
141 142
  def project_visibility_icon_description(level)
    "#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
D
Douwe Maan 已提交
143 144
  end

145
  def visibility_level_label(level)
146 147 148
    # The visibility level can be:
    # 'VisibilityLevel|Private', 'VisibilityLevel|Internal', 'VisibilityLevel|Public'
    s_(Project.visibility_levels.key(level))
149
  end
150

151
  def restricted_visibility_levels(show_all = false)
B
blackst0ne 已提交
152
    return [] if current_user.admin? && !show_all
153
    current_application_settings.restricted_visibility_levels || []
154
  end
V
Vinnie Okada 已提交
155

D
Douwe Maan 已提交
156 157 158
  delegate  :default_project_visibility,
            :default_group_visibility,
            to: :current_application_settings
159

160
  def disallowed_visibility_level?(form_model, level)
161 162
    return false unless form_model.respond_to?(:visibility_level_allowed?)
    !form_model.visibility_level_allowed?(level)
V
Valery Sizov 已提交
163
  end
R
Rubén Dávila 已提交
164 165 166 167

  private

  def visibility_level_errors_for_group(group, level_name)
M
Mike Greiling 已提交
168
    group_name = link_to group.name, group_path(group)
R
Rubén Dávila 已提交
169 170
    change_visiblity = link_to 'change the visibility', edit_group_path(group)

M
Mike Greiling 已提交
171
    { reason: "the visibility of #{group_name} is #{group.visibility}",
R
Rubén Dávila 已提交
172 173
      instruction: " To make this group #{level_name}, you must first #{change_visiblity} of the parent group." }
  end
174
end