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)
|
|
|
|
#
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
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-05-01 05:24:21 -04:00
|
|
|
return [Group.all] if current_user&.full_private_access? && all_available?
|
2016-03-17 18:42:46 -04:00
|
|
|
|
2017-08-24 06:33:06 -04:00
|
|
|
groups = []
|
|
|
|
groups << Gitlab::GroupHierarchy.new(groups_for_ancestors, groups_for_descendants).all_groups if current_user
|
|
|
|
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
|
|
|
|
|
2017-05-03 19:51:25 -04:00
|
|
|
def by_parent(groups)
|
|
|
|
return groups unless params[:parent]
|
|
|
|
|
|
|
|
groups.where(parent: params[:parent])
|
|
|
|
end
|
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
|
2016-03-01 10:22:29 -05:00
|
|
|
end
|