visibility_level_helper.rb 6.12 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6
module VisibilityLevelHelper
  def visibility_level_color(level)
    case level
    when Gitlab::VisibilityLevel::PRIVATE
7
      'vs-private'
8
    when Gitlab::VisibilityLevel::INTERNAL
9
      'vs-internal'
10
    when Gitlab::VisibilityLevel::PUBLIC
11
      'vs-public'
12 13 14
    end
  end

Vinnie Okada's avatar
Vinnie Okada committed
15 16
  # Return the description for the +level+ argument.
  #
17 18 19
  # +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
20
  def visibility_level_description(level, form_model)
21 22
    case form_model
    when Project
Vinnie Okada's avatar
Vinnie Okada committed
23
      project_visibility_level_description(level)
24 25
    when Group
      group_visibility_level_description(level)
26 27
    when Snippet
      snippet_visibility_level_description(level, form_model)
Vinnie Okada's avatar
Vinnie Okada committed
28 29 30 31
    end
  end

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

42 43 44
  def group_visibility_level_description(level)
    case level
    when Gitlab::VisibilityLevel::PRIVATE
Douwe Maan's avatar
Douwe Maan committed
45
      "The group and its projects can only be viewed by members."
46
    when Gitlab::VisibilityLevel::INTERNAL
Douwe Maan's avatar
Douwe Maan committed
47
      "The group and any internal projects can be viewed by any logged in user."
48
    when Gitlab::VisibilityLevel::PUBLIC
Douwe Maan's avatar
Douwe Maan committed
49
      "The group and any public projects can be viewed without any authentication."
50 51 52
    end
  end

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

68 69
  def restricted_visibility_level_description(level)
    level_name = Gitlab::VisibilityLevel.level_name(level)
70
    "#{level_name.capitalize} visibility has been restricted by the administrator."
71 72 73 74 75 76 77 78 79 80 81
  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

82 83
  # 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.
84 85 86
  def disallowed_project_visibility_level_description(level, project)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []
87
    instructions = []
88 89

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

    unless project.visibility_level_allowed_by_group?(level)
94
      errors = visibility_level_errors_for_group(project.group, level_name)
95

96 97
      reasons << errors[:reason]
      instructions << errors[:instruction]
98 99 100
    end

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

104 105
  # 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.
106 107 108
  def disallowed_group_visibility_level_description(level, group)
    level_name = Gitlab::VisibilityLevel.level_name(level).downcase
    reasons = []
109
    instructions = []
110 111

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

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

    unless group.visibility_level_allowed_by_parent?(level)
120
      errors = visibility_level_errors_for_group(group.parent, level_name)
121

122 123
      reasons << errors[:reason]
      instructions << errors[:instruction]
124 125 126
    end

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

Douwe Maan's avatar
Douwe Maan committed
130
  def visibility_icon_description(form_model)
131
    if form_model.respond_to?(:visibility_level_allowed_as_fork?)
Douwe Maan's avatar
Douwe Maan committed
132
      project_visibility_icon_description(form_model.visibility_level)
133
    elsif form_model.respond_to?(:visibility_level_allowed_by_sub_groups?)
Douwe Maan's avatar
Douwe Maan committed
134 135 136 137 138 139
      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)}"
140 141
  end

Douwe Maan's avatar
Douwe Maan committed
142
  def project_visibility_icon_description(level)
143
    "#{visibility_level_label(level)} - #{project_visibility_level_description(level)}"
144 145
  end

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

152
  def restricted_visibility_levels(show_all = false)
153
    return [] if current_user.admin? && !show_all
154

155
    Gitlab::CurrentSettings.restricted_visibility_levels || []
156
  end
Vinnie Okada's avatar
Vinnie Okada committed
157

Douwe Maan's avatar
Douwe Maan committed
158 159
  delegate  :default_project_visibility,
            :default_group_visibility,
160
            to: :'Gitlab::CurrentSettings.current_application_settings'
161

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

165
    !form_model.visibility_level_allowed?(level)
Valery Sizov's avatar
Valery Sizov committed
166
  end
167 168 169 170

  private

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

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