2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
class GroupMembersFinder < UnionFinder
|
2020-12-02 01:09:50 -05:00
|
|
|
RELATIONS = %i(direct inherited descendants).freeze
|
|
|
|
DEFAULT_RELATIONS = %i(direct inherited).freeze
|
|
|
|
|
2020-03-31 20:08:09 -04:00
|
|
|
include CreatedAtFilter
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
# Params can be any of the following:
|
|
|
|
# two_factor: string. 'enabled' or 'disabled' are returning different set of data, other values are not effective.
|
|
|
|
# sort: string
|
|
|
|
# search: string
|
2020-03-31 20:08:09 -04:00
|
|
|
# created_after: datetime
|
|
|
|
# created_before: datetime
|
2020-04-02 08:08:18 -04:00
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(group, user = nil, params: {})
|
2017-02-08 10:02:25 -05:00
|
|
|
@group = group
|
2020-01-13 10:07:53 -05:00
|
|
|
@user = user
|
2020-04-02 08:08:18 -04:00
|
|
|
@params = params
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|
|
|
|
|
2020-12-02 01:09:50 -05:00
|
|
|
def execute(include_relations: DEFAULT_RELATIONS)
|
2020-10-01 05:09:54 -04:00
|
|
|
group_members = group_members_list
|
2019-07-29 18:33:57 -04:00
|
|
|
relations = []
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
return filter_members(group_members) if include_relations == [:direct]
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2019-12-13 10:08:02 -05:00
|
|
|
relations << group_members if include_relations.include?(:direct)
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
if include_relations.include?(:inherited) && group.parent
|
2020-10-01 05:09:54 -04:00
|
|
|
parents_members = relation_group_members(group.ancestors)
|
2018-06-07 16:54:24 -04:00
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
relations << parents_members
|
2018-06-07 16:54:24 -04:00
|
|
|
end
|
|
|
|
|
2019-12-13 10:08:02 -05:00
|
|
|
if include_relations.include?(:descendants)
|
2020-10-01 05:09:54 -04:00
|
|
|
descendant_members = relation_group_members(group.descendants)
|
2018-06-07 16:54:24 -04:00
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
relations << descendant_members
|
2018-06-07 16:54:24 -04:00
|
|
|
end
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2020-01-16 10:08:41 -05:00
|
|
|
return GroupMember.none if relations.empty?
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
members = find_union(relations, GroupMember)
|
2020-03-31 20:08:09 -04:00
|
|
|
filter_members(members)
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|
2020-01-13 10:07:53 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-02 08:08:18 -04:00
|
|
|
attr_reader :user, :group
|
2020-01-13 10:07:53 -05:00
|
|
|
|
2020-03-31 20:08:09 -04:00
|
|
|
def filter_members(members)
|
2020-01-13 10:07:53 -05:00
|
|
|
members = members.search(params[:search]) if params[:search].present?
|
|
|
|
members = members.sort_by_attribute(params[:sort]) if params[:sort].present?
|
|
|
|
|
2020-07-17 17:09:23 -04:00
|
|
|
if params[:two_factor].present? && can_manage_members
|
2020-01-13 10:07:53 -05:00
|
|
|
members = members.filter_by_2fa(params[:two_factor])
|
|
|
|
end
|
|
|
|
|
2020-03-31 20:08:09 -04:00
|
|
|
by_created_at(members)
|
2020-01-13 10:07:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_manage_members
|
|
|
|
Ability.allowed?(user, :admin_group_member, group)
|
|
|
|
end
|
2020-10-01 05:09:54 -04:00
|
|
|
|
|
|
|
def group_members_list
|
|
|
|
group.members
|
|
|
|
end
|
|
|
|
|
|
|
|
def relation_group_members(relation)
|
|
|
|
all_group_members(relation).non_minimal_access
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def all_group_members(relation)
|
|
|
|
GroupMember.non_request
|
|
|
|
.where(source_id: relation.select(:id))
|
|
|
|
.where.not(user_id: group.users.select(:id))
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
GroupMembersFinder.prepend_if_ee('EE::GroupMembersFinder')
|