2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-17 08:06:55 -04:00
|
|
|
module Members
|
2017-10-11 10:47:08 -04:00
|
|
|
class DestroyService < Members::BaseService
|
2020-07-14 20:09:23 -04:00
|
|
|
def execute(member, skip_authorization: false, skip_subresources: false, unassign_issuables: false, destroy_bot: false)
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || authorized?(member, destroy_bot)
|
2016-07-28 13:31:17 -04:00
|
|
|
|
2018-11-16 10:09:32 -05:00
|
|
|
@skip_auth = skip_authorization
|
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
return member if member.is_a?(GroupMember) && member.source.last_owner?(member.user)
|
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
member.destroy
|
2018-02-16 09:10:22 -05:00
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
member.user&.invalidate_cache_counts
|
2018-02-16 09:10:22 -05:00
|
|
|
|
|
|
|
if member.request? && member.user != current_user
|
|
|
|
notification_service.decline_access_request(member)
|
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
|
2018-11-16 10:09:32 -05:00
|
|
|
delete_subresources(member) unless skip_subresources
|
2020-09-02 11:10:54 -04:00
|
|
|
delete_project_invitations_by(member) unless skip_subresources
|
2018-12-11 13:15:10 -05:00
|
|
|
enqueue_delete_todos(member)
|
2020-06-23 11:08:41 -04:00
|
|
|
enqueue_unassign_issuables(member) if unassign_issuables
|
2018-07-16 14:30:17 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
after_execute(member: member)
|
2016-07-28 13:31:17 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
member
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
def authorized?(member, destroy_bot)
|
|
|
|
return can_destroy_bot_member?(member) if destroy_bot
|
|
|
|
|
|
|
|
can_destroy_member?(member)
|
|
|
|
end
|
|
|
|
|
2018-11-16 10:09:32 -05:00
|
|
|
def delete_subresources(member)
|
|
|
|
return unless member.is_a?(GroupMember) && member.user && member.group
|
|
|
|
|
|
|
|
delete_project_members(member)
|
2019-07-24 05:20:54 -04:00
|
|
|
delete_subgroup_members(member)
|
2020-09-02 11:10:54 -04:00
|
|
|
delete_invited_members(member)
|
2018-11-16 10:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_project_members(member)
|
|
|
|
groups = member.group.self_and_descendants
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
destroy_project_members(ProjectMember.in_namespaces(groups).with_user(member.user))
|
2018-11-16 10:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_subgroup_members(member)
|
|
|
|
groups = member.group.descendants
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
destroy_group_members(GroupMember.of_groups(groups).with_user(member.user))
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_invited_members(member)
|
|
|
|
groups = member.group.self_and_descendants
|
|
|
|
|
|
|
|
destroy_group_members(GroupMember.of_groups(groups).not_accepted_invitations_by_user(member.user))
|
|
|
|
|
|
|
|
destroy_project_members(ProjectMember.in_namespaces(groups).not_accepted_invitations_by_user(member.user))
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_project_members(members)
|
|
|
|
members.each do |project_member|
|
|
|
|
self.class.new(current_user).execute(project_member, skip_authorization: @skip_auth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_group_members(members)
|
|
|
|
members.each do |group_member|
|
2018-11-16 10:09:32 -05:00
|
|
|
self.class.new(current_user).execute(group_member, skip_authorization: @skip_auth, skip_subresources: true)
|
|
|
|
end
|
|
|
|
end
|
2020-09-02 11:10:54 -04:00
|
|
|
|
|
|
|
def delete_project_invitations_by(member)
|
|
|
|
return unless member.is_a?(ProjectMember) && member.user && member.project
|
|
|
|
|
|
|
|
members_to_delete = member.project.members.not_accepted_invitations_by_user(member.user)
|
|
|
|
destroy_project_members(members_to_delete)
|
|
|
|
end
|
2018-11-16 10:09:32 -05:00
|
|
|
|
2016-09-16 07:37:21 -04:00
|
|
|
def can_destroy_member?(member)
|
2018-02-16 09:10:22 -05:00
|
|
|
can?(current_user, destroy_member_permission(member), member)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
def can_destroy_bot_member?(member)
|
|
|
|
can?(current_user, destroy_bot_member_permission(member), member)
|
|
|
|
end
|
|
|
|
|
2017-03-02 01:01:02 -05:00
|
|
|
def destroy_member_permission(member)
|
|
|
|
case member
|
|
|
|
when GroupMember
|
|
|
|
:destroy_group_member
|
|
|
|
when ProjectMember
|
|
|
|
:destroy_project_member
|
2017-10-11 10:47:08 -04:00
|
|
|
else
|
|
|
|
raise "Unknown member type: #{member}!"
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
end
|
2020-06-23 11:08:41 -04:00
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
def destroy_bot_member_permission(member)
|
|
|
|
raise "Unsupported bot member type: #{member}" unless member.is_a?(ProjectMember)
|
|
|
|
|
|
|
|
:destroy_project_bot_member
|
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
def enqueue_unassign_issuables(member)
|
|
|
|
source_type = member.is_a?(GroupMember) ? 'Group' : 'Project'
|
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
member.run_after_commit_or_now do
|
2020-07-10 14:09:45 -04:00
|
|
|
MembersDestroyer::UnassignIssuablesWorker.perform_async(member.user_id, member.source_id, source_type)
|
2020-06-23 11:08:41 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Members::DestroyService.prepend_mod_with('Members::DestroyService')
|