BigW Consortium Gitlab

group_descendants_finder.rb 5.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# GroupDescendantsFinder
#
# Used to find and filter all subgroups and projects of a passed parent group
# visible to a specified user.
#
# When passing a `filter` param, the search is performed over all nested levels
# of the `parent_group`. All ancestors for a search result are loaded
#
# Arguments:
#   current_user: The user for which the children should be visible
#   parent_group: The group to find children of
#   params:
#     Supports all params that the `ProjectsFinder` and `GroupProjectsFinder`
#     support.
#
#     filter: string - is aliased to `search` for consistency with the frontend
#     archived: string - `only` or `true`.
#                        `non_archived` is passed to the `ProjectFinder`s if none
#                        was given.
20
class GroupDescendantsFinder
21 22
  attr_reader :current_user, :parent_group, :params

23
  def initialize(current_user: nil, parent_group:, params: {})
24 25
    @current_user = current_user
    @parent_group = parent_group
26
    @params = params.reverse_merge(non_archived: params[:archived].blank?)
27 28 29
  end

  def execute
30
    # The children array might be extended with the ancestors of projects when
31
    # filtering. In that case, take the maximum so the array does not get limited
32
    # Otherwise, allow paginating through all results
33
    #
34 35 36 37 38
    all_required_elements = children
    all_required_elements |= ancestors_for_projects if params[:filter]
    total_count = [all_required_elements.size, paginator.total_count].max

    Kaminari.paginate_array(all_required_elements, total_count: total_count)
39 40
  end

41 42
  def has_children?
    projects.any? || subgroups.any?
43 44 45 46 47
  end

  private

  def children
48
    @children ||= paginator.paginate(params[:page])
49 50
  end

51
  def paginator
52 53
    @paginator ||= Gitlab::MultiCollectionPaginator.new(subgroups, projects,
                                                        per_page: params[:per_page])
54 55
  end

56
  def direct_child_groups
57 58 59 60 61
    GroupsFinder.new(current_user,
                     parent: parent_group,
                     all_available: true).execute
  end

62 63
  def all_visible_descendant_groups
    groups_table = Group.arel_table
64 65 66 67 68 69 70 71 72 73 74 75
    visible_to_user = groups_table[:visibility_level]
                      .in(Gitlab::VisibilityLevel.levels_for_user(current_user))
    if current_user
      authorized_groups = GroupsFinder.new(current_user,
                                           all_available: false)
                            .execute.as('authorized')
      authorized_to_user = groups_table.project(1).from(authorized_groups)
                             .where(authorized_groups[:id].eq(groups_table[:id]))
                             .exists
      visible_to_user = visible_to_user.or(authorized_to_user)
    end

76 77
    hierarchy_for_parent
      .descendants
78
      .where(visible_to_user)
79 80 81
  end

  def subgroups_matching_filter
82
    all_visible_descendant_groups
83 84 85
      .search(params[:filter])
  end

86 87 88 89 90 91 92 93 94 95
  # When filtering we want all to preload all the ancestors upto the specified
  # parent group.
  #
  # - root
  #   - subgroup
  #     - nested-group
  #       - project
  #
  # So when searching 'project', on the 'subgroup' page we want to preload
  # 'nested-group' but not 'subgroup' or 'root'
96 97
  def ancestors_for_groups(base_for_ancestors)
    Gitlab::GroupHierarchy.new(base_for_ancestors)
98
      .base_and_ancestors(upto: parent_group.id)
99 100
  end

101 102 103 104
  def ancestors_for_projects
    projects_to_load_ancestors_of = projects.where.not(namespace: parent_group)
    groups_to_load_ancestors_of = Group.where(id: projects_to_load_ancestors_of.select(:namespace_id))
    ancestors_for_groups(groups_to_load_ancestors_of)
105
      .with_selects_for_list(archived: params[:archived])
106 107
  end

108
  def subgroups
109 110
    return Group.none unless Group.supports_nested_groups?

111 112
    # When filtering subgroups, we want to find all matches withing the tree of
    # descendants to show to the user
113
    groups = if params[:filter]
114
               ancestors_for_groups(subgroups_matching_filter)
115
             else
116
               direct_child_groups
117
             end
118
    groups.with_selects_for_list(archived: params[:archived]).order_by(sort)
119 120
  end

121 122 123
  def direct_child_projects
    GroupProjectsFinder.new(group: parent_group, current_user: current_user, params: params)
      .execute
124 125
  end

126
  # Finds all projects nested under `parent_group` or any of its descendant
127
  # groups
128
  def projects_matching_filter
129 130 131 132 133 134
    projects_nested_in_group = Project.where(namespace_id: hierarchy_for_parent.base_and_descendants.select(:id))
    params_with_search = params.merge(search: params[:filter])

    ProjectsFinder.new(params: params_with_search,
                       current_user: current_user,
                       project_ids_relation: projects_nested_in_group).execute
135 136
  end

137
  def projects
138 139 140
    projects = if params[:filter]
                 projects_matching_filter
               else
141
                 direct_child_projects
142
               end
143
    projects.with_route.order_by(sort)
144 145 146 147 148 149
  end

  def sort
    params.fetch(:sort, 'id_asc')
  end

150 151
  def hierarchy_for_parent
    @hierarchy ||= Gitlab::GroupHierarchy.new(Group.where(id: parent_group.id))
152 153
  end
end