2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-29 18:33:57 -04:00
|
|
|
class GroupMembersFinder < UnionFinder
|
2021-12-20 10:12:25 -05:00
|
|
|
RELATIONS = %i(direct inherited descendants shared_from_groups).freeze
|
2020-12-02 01:09:50 -05:00
|
|
|
DEFAULT_RELATIONS = %i(direct inherited).freeze
|
2021-08-12 11:09:58 -04:00
|
|
|
INVALID_RELATION_TYPE_ERROR_MSG = "is not a valid relation type. Valid relation types are #{RELATIONS.join(', ')}."
|
2021-04-28 11:09:35 -04:00
|
|
|
|
|
|
|
RELATIONS_DESCRIPTIONS = {
|
|
|
|
direct: 'Members in the group itself',
|
|
|
|
inherited: "Members in the group's ancestor groups",
|
2021-12-20 10:12:25 -05:00
|
|
|
descendants: "Members in the group's subgroups",
|
|
|
|
shared_from_groups: "Invited group's members"
|
2021-04-28 11:09:35 -04:00
|
|
|
}.freeze
|
2020-12-02 01:09:50 -05:00
|
|
|
|
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)
|
2021-03-30 11:11:08 -04:00
|
|
|
groups = groups_by_relations(include_relations)
|
|
|
|
members = all_group_members(groups).distinct_on_user_with_max_access_level
|
2017-02-08 10:02:25 -05:00
|
|
|
|
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
|
|
|
|
2021-03-30 11:11:08 -04:00
|
|
|
def groups_by_relations(include_relations)
|
2021-08-12 11:09:58 -04:00
|
|
|
check_relation_arguments!(include_relations)
|
|
|
|
|
2021-12-20 10:12:25 -05:00
|
|
|
related_groups = []
|
|
|
|
|
|
|
|
related_groups << Group.by_id(group.id) if include_relations&.include?(:direct)
|
|
|
|
related_groups << group.ancestors if include_relations&.include?(:inherited)
|
|
|
|
related_groups << group.descendants if include_relations&.include?(:descendants)
|
|
|
|
related_groups << group.shared_with_groups.public_or_visible_to_user(user) if include_relations&.include?(:shared_from_groups)
|
|
|
|
|
|
|
|
find_union(related_groups, Group)
|
2021-03-30 11:11:08 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2021-03-30 11:11:08 -04:00
|
|
|
def all_group_members(groups)
|
|
|
|
members_of_groups(groups).non_minimal_access
|
2020-10-01 05:09:54 -04:00
|
|
|
end
|
|
|
|
|
2021-03-30 11:11:08 -04:00
|
|
|
def members_of_groups(groups)
|
|
|
|
GroupMember.non_request.of_groups(groups)
|
2020-10-01 05:09:54 -04:00
|
|
|
end
|
2021-08-12 11:09:58 -04:00
|
|
|
|
|
|
|
def check_relation_arguments!(include_relations)
|
|
|
|
unless include_relations & RELATIONS == include_relations
|
|
|
|
raise ArgumentError, "#{(include_relations - RELATIONS).first} #{INVALID_RELATION_TYPE_ERROR_MSG}"
|
|
|
|
end
|
|
|
|
end
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
GroupMembersFinder.prepend_mod_with('GroupMembersFinder')
|