debian-mirror-gitlab/app/finders/groups_finder.rb

99 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
# 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-11-18 11:00:15 +05:30
# min_access_level: integer
2018-03-17 18:26:18 +05:30
#
# 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-06-02 11:05:42 +05:30
class GroupsFinder < UnionFinder
2018-03-17 18:26:18 +05:30
include CustomAttributesFilter
2017-08-17 22:00:37 +05:30
def initialize(current_user = nil, params = {})
@current_user = current_user
@params = params
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
def execute
2017-09-10 17:25:29 +05:30
items = all_groups.map do |item|
2018-03-17 18:26:18 +05:30
item = by_parent(item)
item = by_custom_attributes(item)
item
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
find_union(items, Group).with_route.order_id_desc
2016-06-02 11:05:42 +05:30
end
private
2017-08-17 22:00:37 +05:30
attr_reader :current_user, :params
def all_groups
2018-03-17 18:26:18 +05:30
return [owned_groups] if params[:owned]
2018-11-18 11:00:15 +05:30
return [groups_with_min_access_level] if min_access_level?
2018-10-15 14:42:47 +05:30
return [Group.all] if current_user&.full_private_access? && all_available?
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
groups = []
2019-02-15 15:39:39 +05:30
groups << Gitlab::ObjectHierarchy.new(groups_for_ancestors, groups_for_descendants).all_objects if current_user
2018-03-17 18:26:18 +05:30
groups << Group.unscoped.public_to_user(current_user) if include_public_groups?
groups << Group.none if groups.empty?
2016-06-02 11:05:42 +05:30
groups
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def groups_for_ancestors
current_user.authorized_groups
end
def groups_for_descendants
current_user.groups
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def groups_with_min_access_level
groups = current_user
.groups
.where('members.access_level >= ?', params[:min_access_level])
2019-02-15 15:39:39 +05:30
Gitlab::ObjectHierarchy
2018-11-18 11:00:15 +05:30
.new(groups)
.base_and_descendants
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def by_parent(groups)
return groups unless params[:parent]
groups.where(parent: params[:parent])
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def owned_groups
current_user&.owned_groups || Group.none
end
def include_public_groups?
2018-10-15 14:42:47 +05:30
current_user.nil? || all_available?
end
def all_available?
params.fetch(:all_available, true)
2018-03-17 18:26:18 +05:30
end
2018-11-18 11:00:15 +05:30
def min_access_level?
current_user && params[:min_access_level].present?
end
2016-06-02 11:05:42 +05:30
end