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
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
RELATIONS_DESCRIPTIONS = {
|
|
|
|
direct: 'Members in the group itself',
|
|
|
|
inherited: "Members in the group's ancestor groups",
|
|
|
|
descendants: "Members in the group's subgroups"
|
|
|
|
}.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-04-29 21:17:54 +05:30
|
|
|
return filter_members(group_members_list) if include_relations == [:direct]
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
groups = groups_by_relations(include_relations)
|
|
|
|
return GroupMember.none unless groups
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
members = all_group_members(groups).distinct_on_user_with_max_access_level
|
2017-08-17 22:00:37 +05:30
|
|
|
|
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
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def groups_by_relations(include_relations)
|
|
|
|
case include_relations.sort
|
|
|
|
when [:inherited]
|
|
|
|
group.ancestors
|
|
|
|
when [:descendants]
|
|
|
|
group.descendants
|
|
|
|
when [:direct, :inherited]
|
|
|
|
group.self_and_ancestors
|
|
|
|
when [:descendants, :direct]
|
|
|
|
group.self_and_descendants
|
|
|
|
when [:descendants, :inherited]
|
|
|
|
find_union([group.ancestors, group.descendants], Group)
|
|
|
|
when [:descendants, :direct, :inherited]
|
|
|
|
group.self_and_hierarchy
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def all_group_members(groups)
|
|
|
|
members_of_groups(groups).non_minimal_access
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def members_of_groups(groups)
|
|
|
|
GroupMember.non_request.of_groups(groups)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
GroupMembersFinder.prepend_mod_with('GroupMembersFinder')
|