2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-11 04:17:24 -04:00
|
|
|
# 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.
|
2017-09-19 07:11:09 -04:00
|
|
|
class GroupDescendantsFinder
|
2017-09-04 10:23:55 -04:00
|
|
|
attr_reader :current_user, :parent_group, :params
|
|
|
|
|
2017-09-19 05:15:57 -04:00
|
|
|
def initialize(current_user: nil, parent_group:, params: {})
|
2017-09-04 10:23:55 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@parent_group = parent_group
|
2017-10-11 04:17:24 -04:00
|
|
|
@params = params.reverse_merge(non_archived: params[:archived].blank?)
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-01-20 08:35:48 -05:00
|
|
|
# The children array might be extended with the ancestors of projects and
|
|
|
|
# subgroups when filtering. In that case, take the maximum so the array does
|
|
|
|
# not get limited otherwise, allow paginating through all results.
|
2017-09-26 15:31:32 -04:00
|
|
|
#
|
2017-10-04 16:26:51 -04:00
|
|
|
all_required_elements = children
|
2018-01-20 08:35:48 -05:00
|
|
|
if params[:filter]
|
|
|
|
all_required_elements |= ancestors_of_filtered_subgroups
|
|
|
|
all_required_elements |= ancestors_of_filtered_projects
|
|
|
|
end
|
|
|
|
|
2017-10-04 16:26:51 -04:00
|
|
|
total_count = [all_required_elements.size, paginator.total_count].max
|
|
|
|
|
|
|
|
Kaminari.paginate_array(all_required_elements, total_count: total_count)
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
2017-10-10 08:11:55 -04:00
|
|
|
def has_children?
|
|
|
|
projects.any? || subgroups.any?
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def children
|
2017-10-04 16:26:51 -04:00
|
|
|
@children ||= paginator.paginate(params[:page])
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 16:26:51 -04:00
|
|
|
def paginator
|
2018-01-20 08:35:48 -05:00
|
|
|
@paginator ||= Gitlab::MultiCollectionPaginator.new(
|
|
|
|
subgroups,
|
|
|
|
projects.with_route,
|
|
|
|
per_page: params[:per_page]
|
|
|
|
)
|
2017-10-02 06:54:12 -04:00
|
|
|
end
|
|
|
|
|
2017-09-19 07:11:09 -04:00
|
|
|
def direct_child_groups
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/Finder
|
2017-09-06 09:28:07 -04:00
|
|
|
GroupsFinder.new(current_user,
|
|
|
|
parent: parent_group,
|
|
|
|
all_available: true).execute
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/Finder
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-26 14:06:08 -04:00
|
|
|
def all_visible_descendant_groups
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/Finder
|
2017-09-26 14:06:08 -04:00
|
|
|
groups_table = Group.arel_table
|
2017-10-10 07:33:02 -04:00
|
|
|
visible_to_user = groups_table[:visibility_level]
|
|
|
|
.in(Gitlab::VisibilityLevel.levels_for_user(current_user))
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-10-10 07:33:02 -04:00
|
|
|
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
|
|
|
|
|
2017-10-04 16:26:51 -04:00
|
|
|
hierarchy_for_parent
|
|
|
|
.descendants
|
2017-10-10 07:33:02 -04:00
|
|
|
.where(visible_to_user)
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/Finder
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-06 09:28:07 -04:00
|
|
|
|
|
|
|
def subgroups_matching_filter
|
2017-09-26 14:06:08 -04:00
|
|
|
all_visible_descendant_groups
|
2017-09-26 05:22:52 -04:00
|
|
|
.search(params[:filter])
|
|
|
|
end
|
|
|
|
|
2017-10-02 01:55:44 -04:00
|
|
|
# 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'
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-20 08:35:48 -05:00
|
|
|
def ancestors_of_groups(base_for_ancestors)
|
|
|
|
group_ids = base_for_ancestors.except(:select, :sort).select(:id)
|
2018-12-18 07:15:51 -05:00
|
|
|
Gitlab::ObjectHierarchy.new(Group.where(id: group_ids))
|
2017-10-04 16:26:51 -04:00
|
|
|
.base_and_ancestors(upto: parent_group.id)
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-20 08:35:48 -05:00
|
|
|
def ancestors_of_filtered_projects
|
2017-10-04 16:26:51 -04:00
|
|
|
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))
|
2018-01-20 08:35:48 -05:00
|
|
|
ancestors_of_groups(groups_to_load_ancestors_of)
|
|
|
|
.with_selects_for_list(archived: params[:archived])
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-20 08:35:48 -05:00
|
|
|
|
|
|
|
def ancestors_of_filtered_subgroups
|
|
|
|
ancestors_of_groups(subgroups)
|
2017-10-11 11:05:39 -04:00
|
|
|
.with_selects_for_list(archived: params[:archived])
|
2017-10-02 08:22:17 -04:00
|
|
|
end
|
|
|
|
|
2017-09-04 14:01:58 -04:00
|
|
|
def subgroups
|
2019-02-25 05:42:31 -05:00
|
|
|
# When filtering subgroups, we want to find all matches within the tree of
|
2017-09-19 07:11:09 -04:00
|
|
|
# descendants to show to the user
|
2017-09-06 09:28:07 -04:00
|
|
|
groups = if params[:filter]
|
2018-01-20 08:35:48 -05:00
|
|
|
subgroups_matching_filter
|
2017-09-06 09:28:07 -04:00
|
|
|
else
|
2017-09-19 07:11:09 -04:00
|
|
|
direct_child_groups
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-10-11 11:05:39 -04:00
|
|
|
groups.with_selects_for_list(archived: params[:archived]).order_by(sort)
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/Finder
|
2017-10-10 11:05:02 -04:00
|
|
|
def direct_child_projects
|
2018-09-07 02:09:13 -04:00
|
|
|
GroupProjectsFinder.new(group: parent_group, current_user: current_user, params: params, options: { only_owned: true })
|
2018-04-17 05:49:52 -04:00
|
|
|
.execute
|
2017-09-26 05:22:52 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/Finder
|
2017-09-26 05:22:52 -04:00
|
|
|
|
2017-10-10 09:11:14 -04:00
|
|
|
# Finds all projects nested under `parent_group` or any of its descendant
|
2017-10-04 16:26:51 -04:00
|
|
|
# groups
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-06 09:28:07 -04:00
|
|
|
def projects_matching_filter
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/Finder
|
2017-10-11 04:17:24 -04:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/Finder
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-06 09:28:07 -04:00
|
|
|
|
2017-09-04 10:23:55 -04:00
|
|
|
def projects
|
2017-09-06 09:28:07 -04:00
|
|
|
projects = if params[:filter]
|
|
|
|
projects_matching_filter
|
|
|
|
else
|
2017-09-19 05:46:07 -04:00
|
|
|
direct_child_projects
|
2017-09-06 09:28:07 -04:00
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-10-04 16:26:51 -04:00
|
|
|
projects.with_route.order_by(sort)
|
2017-09-26 15:31:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def sort
|
2018-11-14 08:46:31 -05:00
|
|
|
params.fetch(:sort, 'created_desc')
|
2017-09-26 15:31:32 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-10-04 16:26:51 -04:00
|
|
|
def hierarchy_for_parent
|
2018-12-18 07:15:51 -05:00
|
|
|
@hierarchy ||= Gitlab::ObjectHierarchy.new(Group.where(id: parent_group.id))
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-04 10:23:55 -04:00
|
|
|
end
|