2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
class GroupMembersFinder < UnionFinder
|
2021-02-22 17:27:13 +05:30
|
|
|
RELATIONS = %i(direct inherited descendants).freeze
|
|
|
|
DEFAULT_RELATIONS = %i(direct inherited).freeze
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
include CreatedAtFilter
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
# 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-04-22 19:07:51 +05:30
|
|
|
# created_after: datetime
|
|
|
|
# created_before: datetime
|
|
|
|
attr_reader :params
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def initialize(group, user = nil, params: {})
|
2017-08-17 22:00:37 +05:30
|
|
|
@group = group
|
2020-03-13 15:44:24 +05:30
|
|
|
@user = user
|
2020-04-22 19:07:51 +05:30
|
|
|
@params = params
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def execute(include_relations: DEFAULT_RELATIONS)
|
2021-01-03 14:25:43 +05:30
|
|
|
group_members = group_members_list
|
2019-10-12 21:52:04 +05:30
|
|
|
relations = []
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
return filter_members(group_members) if include_relations == [:direct]
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
relations << group_members if include_relations.include?(:direct)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if include_relations.include?(:inherited) && group.parent
|
2021-01-03 14:25:43 +05:30
|
|
|
parents_members = relation_group_members(group.ancestors)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
relations << parents_members
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
if include_relations.include?(:descendants)
|
2021-01-03 14:25:43 +05:30
|
|
|
descendant_members = relation_group_members(group.descendants)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
relations << descendant_members
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
return GroupMember.none if relations.empty?
|
|
|
|
|
|
|
|
members = find_union(relations, GroupMember)
|
2020-04-22 19:07:51 +05:30
|
|
|
filter_members(members)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :group
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def filter_members(members)
|
2020-03-13 15:44:24 +05:30
|
|
|
members = members.search(params[:search]) if params[:search].present?
|
|
|
|
members = members.sort_by_attribute(params[:sort]) if params[:sort].present?
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
if params[:two_factor].present? && can_manage_members
|
2020-03-13 15:44:24 +05:30
|
|
|
members = members.filter_by_2fa(params[:two_factor])
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
by_created_at(members)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def can_manage_members
|
|
|
|
Ability.allowed?(user, :admin_group_member, group)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
GroupMembersFinder.prepend_if_ee('EE::GroupMembersFinder')
|