2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
class GroupMembersFinder < UnionFinder
|
2017-02-08 10:02:25 -05:00
|
|
|
def initialize(group)
|
|
|
|
@group = group
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-07 16:54:24 -04:00
|
|
|
def execute(include_descendants: false)
|
2017-02-08 10:02:25 -05:00
|
|
|
group_members = @group.members
|
2019-07-29 18:33:57 -04:00
|
|
|
relations = []
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
return group_members unless @group.parent || include_descendants
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
relations << group_members
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
if @group.parent
|
|
|
|
parents_members = GroupMember.non_request
|
|
|
|
.where(source_id: @group.ancestors.select(:id))
|
|
|
|
.where.not(user_id: @group.users.select(:id))
|
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
relations << parents_members
|
2018-06-07 16:54:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if include_descendants
|
|
|
|
descendant_members = GroupMember.non_request
|
|
|
|
.where(source_id: @group.descendants.select(:id))
|
|
|
|
.where.not(user_id: @group.users.select(:id))
|
|
|
|
|
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
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
find_union(relations, GroupMember)
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# 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')
|