2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
# GroupsFinder
|
|
|
|
#
|
|
|
|
# Used to filter Groups by a set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user is requesting groups
|
|
|
|
# params:
|
|
|
|
# owned: boolean
|
|
|
|
# parent: Group
|
|
|
|
# all_available: boolean (defaults to true)
|
2018-07-08 15:41:55 -04:00
|
|
|
# min_access_level: integer
|
2021-07-21 08:09:35 -04:00
|
|
|
# search: string
|
2019-04-02 02:49:11 -04:00
|
|
|
# exclude_group_ids: array of integers
|
2020-09-30 02:09:47 -04:00
|
|
|
# include_parent_descendants: boolean (defaults to false) - includes descendant groups when
|
|
|
|
# filtering by parent. The parent param must be present.
|
2022-09-15 23:13:19 -04:00
|
|
|
# include_ancestors: boolean (defaults to true)
|
2017-08-24 06:33:06 -04:00
|
|
|
#
|
|
|
|
# Users with full private access can see all groups. The `owned` and `parent`
|
|
|
|
# params can be used to restrict the groups that are returned.
|
|
|
|
#
|
|
|
|
# Anonymous users will never return any `owned` groups. They will return all
|
|
|
|
# public groups instead, even if `all_available` is set to false.
|
2016-03-20 16:03:53 -04:00
|
|
|
class GroupsFinder < UnionFinder
|
2017-09-18 11:07:38 -04:00
|
|
|
include CustomAttributesFilter
|
|
|
|
|
2017-05-03 19:51:25 -04:00
|
|
|
def initialize(current_user = nil, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
2016-03-01 10:22:29 -05:00
|
|
|
|
2017-05-03 19:51:25 -04:00
|
|
|
def execute
|
2017-06-14 15:37:29 -04:00
|
|
|
items = all_groups.map do |item|
|
2017-09-18 11:07:38 -04:00
|
|
|
item = by_parent(item)
|
|
|
|
item = by_custom_attributes(item)
|
2019-04-02 02:49:11 -04:00
|
|
|
item = exclude_group_ids(item)
|
2021-07-21 08:09:35 -04:00
|
|
|
item = by_search(item)
|
2017-09-18 11:07:38 -04:00
|
|
|
|
|
|
|
item
|
2017-06-14 15:37:29 -04:00
|
|
|
end
|
2017-09-18 11:07:38 -04:00
|
|
|
|
2017-06-14 15:37:29 -04:00
|
|
|
find_union(items, Group).with_route.order_id_desc
|
2016-03-01 10:22:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-03 19:51:25 -04:00
|
|
|
attr_reader :current_user, :params
|
|
|
|
|
|
|
|
def all_groups
|
2017-08-24 06:33:06 -04:00
|
|
|
return [owned_groups] if params[:owned]
|
2018-07-08 15:41:55 -04:00
|
|
|
return [groups_with_min_access_level] if min_access_level?
|
2019-12-17 04:07:48 -05:00
|
|
|
return [Group.all] if current_user&.can_read_all_resources? && all_available?
|
2016-03-17 18:42:46 -04:00
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
groups = []
|
2022-09-15 23:13:19 -04:00
|
|
|
groups = get_groups_for_user if current_user
|
2021-12-07 19:15:00 -05:00
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
groups << Group.unscoped.public_to_user(current_user) if include_public_groups?
|
|
|
|
groups << Group.none if groups.empty?
|
2016-03-20 16:03:53 -04:00
|
|
|
groups
|
2016-03-17 18:42:46 -04:00
|
|
|
end
|
2017-05-03 19:51:25 -04:00
|
|
|
|
2017-06-14 15:37:29 -04:00
|
|
|
def groups_for_ancestors
|
|
|
|
current_user.authorized_groups
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups_for_descendants
|
|
|
|
current_user.groups
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-08 15:41:55 -04:00
|
|
|
def groups_with_min_access_level
|
|
|
|
groups = current_user
|
|
|
|
.groups
|
|
|
|
.where('members.access_level >= ?', params[:min_access_level])
|
|
|
|
|
2022-05-06 11:09:03 -04:00
|
|
|
if Feature.enabled?(:use_traversal_ids_groups_finder, current_user)
|
2021-12-07 19:15:00 -05:00
|
|
|
groups.self_and_descendants
|
|
|
|
else
|
|
|
|
Gitlab::ObjectHierarchy
|
|
|
|
.new(groups)
|
|
|
|
.base_and_descendants
|
|
|
|
end
|
2018-07-08 15:41:55 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-08 15:41:55 -04:00
|
|
|
|
2019-04-02 02:49:11 -04:00
|
|
|
def exclude_group_ids(groups)
|
|
|
|
return groups unless params[:exclude_group_ids]
|
|
|
|
|
|
|
|
groups.id_not_in(params[:exclude_group_ids])
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-03 19:51:25 -04:00
|
|
|
def by_parent(groups)
|
|
|
|
return groups unless params[:parent]
|
|
|
|
|
2020-09-30 02:09:47 -04:00
|
|
|
if include_parent_descendants?
|
|
|
|
groups.id_in(params[:parent].descendants)
|
|
|
|
else
|
|
|
|
groups.where(parent: params[:parent])
|
|
|
|
end
|
2017-05-03 19:51:25 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-24 06:33:06 -04:00
|
|
|
|
2021-07-21 08:09:35 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_search(groups)
|
|
|
|
return groups unless params[:search].present?
|
|
|
|
|
2021-07-26 14:09:51 -04:00
|
|
|
groups.search(params[:search], include_parents: params[:parent].blank?)
|
2021-07-21 08:09:35 -04:00
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
def owned_groups
|
2017-09-19 07:44:02 -04:00
|
|
|
current_user&.owned_groups || Group.none
|
2017-08-24 06:33:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_public_groups?
|
2018-05-01 05:24:21 -04:00
|
|
|
current_user.nil? || all_available?
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_available?
|
|
|
|
params.fetch(:all_available, true)
|
2017-08-24 06:33:06 -04:00
|
|
|
end
|
2018-07-08 15:41:55 -04:00
|
|
|
|
2020-09-30 02:09:47 -04:00
|
|
|
def include_parent_descendants?
|
|
|
|
params.fetch(:include_parent_descendants, false)
|
|
|
|
end
|
|
|
|
|
2018-07-08 15:41:55 -04:00
|
|
|
def min_access_level?
|
|
|
|
current_user && params[:min_access_level].present?
|
|
|
|
end
|
2022-09-15 23:13:19 -04:00
|
|
|
|
|
|
|
def include_ancestors?
|
|
|
|
params.fetch(:include_ancestors, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_groups_for_user
|
|
|
|
groups = []
|
|
|
|
|
|
|
|
if Feature.enabled?(:use_traversal_ids_groups_finder, current_user)
|
|
|
|
groups << if include_ancestors?
|
|
|
|
current_user.authorized_groups.self_and_ancestors
|
|
|
|
else
|
|
|
|
current_user.authorized_groups
|
|
|
|
end
|
|
|
|
|
|
|
|
groups << current_user.groups.self_and_descendants
|
|
|
|
elsif include_ancestors?
|
|
|
|
groups << Gitlab::ObjectHierarchy.new(groups_for_ancestors, groups_for_descendants).all_objects
|
|
|
|
else
|
|
|
|
groups << current_user.authorized_groups
|
|
|
|
groups << Gitlab::ObjectHierarchy.new(groups_for_descendants).base_and_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
groups
|
|
|
|
end
|
2016-03-01 10:22:29 -05:00
|
|
|
end
|