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

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

13
  def group_nav_link_paths
14
    %w[groups#projects groups#edit badges#index ci_cd#show ldap_group_links#index hooks#index audit_events#index pipeline_quota#index]
15 16
  end

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

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

25 26 27 28
  def can_change_group_visibility_level?(group)
    can?(current_user, :change_visibility_level, group)
  end

29 30 31 32
  def can_change_share_with_group_lock?(group)
    can?(current_user, :change_share_with_group_lock, group)
  end

33 34 35 36 37 38 39 40 41 42 43 44 45 46
  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

47
  def group_icon_url(group, options = {})
48
    if group.is_a?(String)
49
      group = Group.find_by_full_path(group)
50 51
    end

52
    group.try(:avatar_url) || ActionController::Base.helpers.image_path('no_group_avatar.png')
53
  end
54

55
  def group_title(group, name = nil, url = nil)
Sam Rose's avatar
Sam Rose committed
56
    @has_group_title = true
57
    full_title = []
58

59
    group.ancestors.reverse.each_with_index do |parent, index|
60
      if index > 0
61
        add_to_breadcrumb_dropdown(group_title_link(parent, hidable: false, show_avatar: true, for_dropdown: true), location: :before)
62
      else
63
        full_title << breadcrumb_list_item(group_title_link(parent, hidable: false))
64 65
      end
    end
66

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

69 70
    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
71

72
    full_title.join.html_safe
73
  end
74

75 76 77 78 79 80 81
  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
82

83 84
    size = group.projects.size

85 86
    if lfs_status == size
      'for all projects'
87
    else
88
      "for #{lfs_status} out of #{pluralize(size, 'project')}"
89 90 91 92
    end
  end

  def group_lfs_status(group)
93 94 95
    status = group.lfs_enabled? ? 'enabled' : 'disabled'

    content_tag(:span, class: "lfs-#{status}") do
96
      "#{status.humanize} #{projects_lfs_status(group)}"
97
    end
98
  end
99

100
  def remove_group_message(group)
101
    _("You are going to remove %{group_name}. Removed groups CANNOT be restored! Are you ABSOLUTELY sure?") %
102 103 104
      { group_name: group.name }
  end

105 106 107 108 109 110 111 112 113 114 115 116 117 118
  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
  end

119 120 121 122 123 124 125 126 127 128 129 130 131
  def parent_group_options(current_group)
    groups = current_user.owned_groups.sort_by(&:human_name).map do |group|
      { id: group.id, text: group.human_name }
    end

    groups.delete_if { |group| group[:id] == current_group.id }
    groups.to_json
  end

  def supports_nested_groups?
    Group.supports_nested_groups?
  end

132 133
  private

134 135 136
  def get_group_sidebar_links
    links = [:overview, :group_members]

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

143
    if can?(current_user, :read_cluster, @group) && @group.group_clusters_enabled?
144 145 146
      links << :kubernetes
    end

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

    links
  end

154 155
  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
156 157
      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
158 159
    end
  end
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  def ancestor_group(group)
    ancestor = oldest_consecutively_locked_ancestor(group)
    if can?(current_user, :read_group, ancestor)
      link_to ancestor.name, group_path(ancestor)
    else
      ancestor.name
    end
  end

  def remove_the_share_with_group_lock_from_ancestor(group)
    ancestor = oldest_consecutively_locked_ancestor(group)
    text = s_("GroupSettings|remove the share with group lock from %{ancestor_group_name}") % { ancestor_group_name: ancestor.name }
    if can?(current_user, :admin_group, ancestor)
      link_to text, edit_group_path(ancestor)
    else
      text
    end
  end

  def oldest_consecutively_locked_ancestor(group)
    group.ancestors.find do |group|
      !group.has_parent? || !group.parent.share_with_group_lock?
    end
  end

  def default_help
187
    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.")
188 189 190 191 192 193 194 195 196 197 198 199 200
  end

  def ancestor_locked_but_you_can_override(group)
    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) }
  end

  def ancestor_locked_so_ask_the_owner(group)
    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) }
  end

  def ancestor_locked_and_has_been_overridden(group)
    s_("GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup.").html_safe % { ancestor_group: ancestor_group(group) }
  end
201
end
202 203

GroupsHelper.prepend(EE::GroupsHelper)