Read about our upcoming Code of Conduct on this issue

groups_helper.rb 6.83 KB
Newer Older
1
2
# frozen_string_literal: true

3
module GroupsHelper
4
5
6
  def group_overview_nav_link_paths
    %w[
      groups#show
7
      groups#details
8
9
10
11
12
13
      groups#activity
      groups#subgroups
      analytics#show
    ]
  end

George Tsiolis's avatar
George Tsiolis committed
14
  def group_nav_link_paths
15
    %w[groups#projects groups#edit badges#index ci_cd#show ldap_group_links#index hooks#index audit_events#index pipeline_quota#index]
George Tsiolis's avatar
George Tsiolis committed
16
17
  end

18
19
20
21
22
23
24
25
  def group_sidebar_links
    @group_sidebar_links ||= get_group_sidebar_links
  end

  def group_sidebar_link?(link)
    group_sidebar_links.include?(link)
  end

Felipe Artur's avatar
Felipe Artur committed
26
27
28
29
  def can_change_group_visibility_level?(group)
    can?(current_user, :change_visibility_level, group)
  end

Michael Kozono's avatar
Michael Kozono committed
30
31
32
33
  def can_change_share_with_group_lock?(group)
    can?(current_user, :change_share_with_group_lock, group)
  end

34
  def can_disable_group_emails?(group)
35
    can?(current_user, :set_emails_disabled, group) && !group.parent&.emails_disabled?
36
37
  end

38
39
40
41
42
43
44
45
46
47
48
49
50
51
  def group_issues_count(state:)
    IssuesFinder
      .new(current_user, group_id: @group.id, state: state, non_archived: true, include_subgroups: true)
      .execute
      .count
  end

  def group_merge_requests_count(state:)
    MergeRequestsFinder
      .new(current_user, group_id: @group.id, state: state, non_archived: true, include_subgroups: true)
      .execute
      .count
  end

52
  def group_icon_url(group, options = {})
53
    if group.is_a?(String)
54
      group = Group.find_by_full_path(group)
55
56
    end

57
    group.try(:avatar_url) || ActionController::Base.helpers.image_path('no_group_avatar.png')
58
  end
59

60
  def group_title(group, name = nil, url = nil)
Sam Rose's avatar
Sam Rose committed
61
    @has_group_title = true
62
    full_title = []
63

64
    group.ancestors.reverse.each_with_index do |parent, index|
Phil Hughes's avatar
Phil Hughes committed
65
      if index > 0
66
        add_to_breadcrumb_dropdown(group_title_link(parent, hidable: false, show_avatar: true, for_dropdown: true), location: :before)
67
      else
68
        full_title << breadcrumb_list_item(group_title_link(parent, hidable: false))
69
70
71
      end
    end

72
    full_title << render("layouts/nav/breadcrumbs/collapsed_dropdown", location: :before, title: _("Show parent subgroups"))
73

74
75
    full_title << breadcrumb_list_item(group_title_link(group))
    full_title << ' &middot; '.html_safe + link_to(simple_sanitize(name), url, class: 'group-path breadcrumb-item-text js-breadcrumb-item-text') if name
76

77
    full_title.join.html_safe
78
  end
79

80
81
82
83
84
85
86
  def projects_lfs_status(group)
    lfs_status =
      if group.lfs_enabled?
        group.projects.select(&:lfs_enabled?).size
      else
        group.projects.reject(&:lfs_enabled?).size
      end
87

88
89
    size = group.projects.size

90
91
    if lfs_status == size
      'for all projects'
92
    else
93
      "for #{lfs_status} out of #{pluralize(size, 'project')}"
94
95
96
97
    end
  end

  def group_lfs_status(group)
98
99
100
    status = group.lfs_enabled? ? 'enabled' : 'disabled'

    content_tag(:span, class: "lfs-#{status}") do
101
      "#{status.humanize} #{projects_lfs_status(group)}"
102
    end
103
  end
104

105
  def remove_group_message(group)
106
    _("You are going to remove %{group_name}, this will also remove all of its subgroups and projects. Removed groups CANNOT be restored! Are you ABSOLUTELY sure?") %
107
108
109
      { group_name: group.name }
  end

110
111
112
113
114
115
116
117
118
119
120
121
  def share_with_group_lock_help_text(group)
    return default_help unless group.parent&.share_with_group_lock?

    if group.share_with_group_lock?
      if can?(current_user, :change_share_with_group_lock, group.parent)
        ancestor_locked_but_you_can_override(group)
      else
        ancestor_locked_so_ask_the_owner(group)
      end
    else
      ancestor_locked_and_has_been_overridden(group)
    end
Michael Kozono's avatar
Michael Kozono committed
122
123
  end

124
  def parent_group_options(current_group)
125
126
127
    exclude_groups = current_group.self_and_descendants.pluck_primary_key
    exclude_groups << current_group.parent_id if current_group.parent_id
    groups = GroupsFinder.new(current_user, min_access_level: Gitlab::Access::OWNER, exclude_group_ids: exclude_groups).execute.sort_by(&:human_name).map do |group|
128
129
130
131
132
133
      { id: group.id, text: group.human_name }
    end

    groups.to_json
  end

Phil Hughes's avatar
Phil Hughes committed
134
135
  private

136
137
138
  def get_group_sidebar_links
    links = [:overview, :group_members]

139
140
141
142
    resources = [:activity, :issues, :boards, :labels, :milestones,
                 :merge_requests]
    links += resources.select do |resource|
      can?(current_user, "read_group_#{resource}".to_sym, @group)
143
144
    end

145
    if can?(current_user, :read_cluster, @group)
146
147
148
      links << :kubernetes
    end

149
150
151
152
153
154
155
    if can?(current_user, :admin_group, @group)
      links << :settings
    end

    links
  end

156
157
  def group_title_link(group, hidable: false, show_avatar: false, for_dropdown: false)
    link_to(group_path(group), class: "group-path #{'breadcrumb-item-text' unless for_dropdown} js-breadcrumb-item-text #{'hidable' if hidable}") do
158
159
      icon = group_icon(group, class: "avatar-tile", width: 15, height: 15) if (group.try(:avatar_url) || show_avatar) && !Rails.env.test?
      [icon, simple_sanitize(group.name)].join.html_safe
Phil Hughes's avatar
Phil Hughes committed
160
161
    end
  end
Michael Kozono's avatar
Michael Kozono committed
162

163
  def ancestor_group(group)
164
    ancestor = oldest_consecutively_locked_ancestor(group)
165
166
167
168
169
    if can?(current_user, :read_group, ancestor)
      link_to ancestor.name, group_path(ancestor)
    else
      ancestor.name
    end
170
171
  end

172
  def remove_the_share_with_group_lock_from_ancestor(group)
173
    ancestor = oldest_consecutively_locked_ancestor(group)
174
    text = s_("GroupSettings|remove the share with group lock from %{ancestor_group_name}") % { ancestor_group_name: ancestor.name }
175
176
177
178
179
    if can?(current_user, :admin_group, ancestor)
      link_to text, edit_group_path(ancestor)
    else
      text
    end
180
181
  end

182
183
184
185
  def oldest_consecutively_locked_ancestor(group)
    group.ancestors.find do |group|
      !group.has_parent? || !group.parent.share_with_group_lock?
    end
Winnie Hellmann's avatar
Winnie Hellmann committed
186
187
  end

Michael Kozono's avatar
Michael Kozono committed
188
  def default_help
189
    s_("GroupSettings|This setting will be applied to all subgroups unless overridden by a group owner. Groups that already have access to the project will continue to have access unless removed manually.")
Michael Kozono's avatar
Michael Kozono committed
190
191
  end

192
  def ancestor_locked_but_you_can_override(group)
193
    s_("GroupSettings|This setting is applied on %{ancestor_group}. You can override the setting or %{remove_ancestor_share_with_group_lock}.").html_safe % { ancestor_group: ancestor_group(group), remove_ancestor_share_with_group_lock: remove_the_share_with_group_lock_from_ancestor(group) }
Michael Kozono's avatar
Michael Kozono committed
194
195
  end

196
  def ancestor_locked_so_ask_the_owner(group)
197
    s_("GroupSettings|This setting is applied on %{ancestor_group}. To share projects in this group with another group, ask the owner to override the setting or %{remove_ancestor_share_with_group_lock}.").html_safe % { ancestor_group: ancestor_group(group), remove_ancestor_share_with_group_lock: remove_the_share_with_group_lock_from_ancestor(group) }
Michael Kozono's avatar
Michael Kozono committed
198
199
  end

200
  def ancestor_locked_and_has_been_overridden(group)
Winnie Hellmann's avatar
Winnie Hellmann committed
201
    s_("GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup.").html_safe % { ancestor_group: ancestor_group(group) }
Michael Kozono's avatar
Michael Kozono committed
202
  end
203
end
204
205

GroupsHelper.prepend_if_ee('EE::GroupsHelper')