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

Vinnie Okada's avatar
Vinnie Okada committed
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.
Vinnie Okada's avatar
Vinnie Okada committed
18
  def visibility_level_description(level, form_model)
19 20
    case form_model
    when Project
Vinnie Okada's avatar
Vinnie Okada committed
21
      project_visibility_level_description(level)
Felipe's avatar
Felipe committed
22 23
    when Group
      group_visibility_level_description(level)
24 25
    when Snippet
      snippet_visibility_level_description(level, form_model)
Vinnie Okada's avatar
Vinnie Okada committed
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.")
37 38 39
    end
  end

Felipe's avatar
Felipe committed
40 41 42
  def group_visibility_level_description(level)
    case level
    when Gitlab::VisibilityLevel::PRIVATE
Douwe Maan's avatar
Douwe Maan committed
43
      "The group and its projects can only be viewed by members."
Felipe's avatar
Felipe committed
44
    when Gitlab::VisibilityLevel::INTERNAL
Douwe Maan's avatar
Douwe Maan committed
45
      "The group and any internal projects can be viewed by any logged in user."
Felipe's avatar
Felipe committed
46
    when Gitlab::VisibilityLevel::PUBLIC
Douwe Maan's avatar
Douwe Maan committed
47
      "The group and any public projects can be viewed without any authentication."
Felipe's avatar
Felipe committed
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)
92
      errors = visibility_level_errors_for_group(project.group, level_name)
93

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)
118
      errors = visibility_level_errors_for_group(group.parent, level_name)
119

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

Douwe Maan's avatar
Douwe Maan committed
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)}"
139 140
  end

Douwe Maan's avatar
Douwe Maan committed
141 142
  def project_visibility_icon_description(level)
    "#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
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)
152
    return [] if current_user.admin? && !show_all
153

154
    current_application_settings.restricted_visibility_levels || []
155
  end
Vinnie Okada's avatar
Vinnie Okada committed
156

Douwe Maan's avatar
Douwe Maan committed
157 158 159
  delegate  :default_project_visibility,
            :default_group_visibility,
            to: :current_application_settings
160

161
  def disallowed_visibility_level?(form_model, level)
162
    return false unless form_model.respond_to?(:visibility_level_allowed?)
163

164
    !form_model.visibility_level_allowed?(level)
Valeriy's avatar
Valeriy committed
165
  end
166 167 168 169

  private

  def visibility_level_errors_for_group(group, level_name)
170
    group_name = link_to group.name, group_path(group)
171 172
    change_visiblity = link_to 'change the visibility', edit_group_path(group)

173
    { reason: "the visibility of #{group_name} is #{group.visibility}",
174 175
      instruction: " To make this group #{level_name}, you must first #{change_visiblity} of the parent group." }
  end
176
end