2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-17 12:06:55 +00:00
|
|
|
module Members
|
2017-10-11 14:47:08 +00:00
|
|
|
class DestroyService < Members::BaseService
|
2018-11-16 15:09:32 +00:00
|
|
|
def execute(member, skip_authorization: false, skip_subresources: false)
|
2018-02-16 14:10:22 +00:00
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member)
|
2016-07-28 17:31:17 +00:00
|
|
|
|
2018-11-16 15:09:32 +00:00
|
|
|
@skip_auth = skip_authorization
|
|
|
|
|
2018-02-16 14:10:22 +00:00
|
|
|
return member if member.is_a?(GroupMember) && member.source.last_owner?(member.user)
|
|
|
|
|
2018-03-07 19:54:28 +00:00
|
|
|
member.destroy
|
2018-02-16 14:10:22 +00:00
|
|
|
|
2018-03-07 19:54:28 +00:00
|
|
|
member.user&.invalidate_cache_counts
|
2018-02-16 14:10:22 +00:00
|
|
|
|
|
|
|
if member.request? && member.user != current_user
|
|
|
|
notification_service.decline_access_request(member)
|
|
|
|
end
|
2016-07-28 17:31:17 +00:00
|
|
|
|
2018-11-16 15:09:32 +00:00
|
|
|
delete_subresources(member) unless skip_subresources
|
2018-12-11 18:15:10 +00:00
|
|
|
enqueue_delete_todos(member)
|
2018-07-16 18:30:17 +00:00
|
|
|
|
2017-10-11 14:47:08 +00:00
|
|
|
after_execute(member: member)
|
2016-07-28 17:31:17 +00:00
|
|
|
|
2017-10-11 14:47:08 +00:00
|
|
|
member
|
2016-06-17 12:06:55 +00:00
|
|
|
end
|
2016-07-28 17:31:17 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-16 15:09:32 +00:00
|
|
|
def delete_subresources(member)
|
|
|
|
return unless member.is_a?(GroupMember) && member.user && member.group
|
|
|
|
|
|
|
|
delete_project_members(member)
|
2019-07-24 09:20:54 +00:00
|
|
|
delete_subgroup_members(member)
|
2018-11-16 15:09:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_project_members(member)
|
|
|
|
groups = member.group.self_and_descendants
|
|
|
|
|
|
|
|
ProjectMember.in_namespaces(groups).with_user(member.user).each do |project_member|
|
|
|
|
self.class.new(current_user).execute(project_member, skip_authorization: @skip_auth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_subgroup_members(member)
|
|
|
|
groups = member.group.descendants
|
|
|
|
|
2019-05-13 04:42:06 +00:00
|
|
|
GroupMember.of_groups(groups).with_user(member.user).each do |group_member|
|
2018-11-16 15:09:32 +00:00
|
|
|
self.class.new(current_user).execute(group_member, skip_authorization: @skip_auth, skip_subresources: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 11:37:21 +00:00
|
|
|
def can_destroy_member?(member)
|
2018-02-16 14:10:22 +00:00
|
|
|
can?(current_user, destroy_member_permission(member), member)
|
2017-03-02 06:01:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_member_permission(member)
|
|
|
|
case member
|
|
|
|
when GroupMember
|
|
|
|
:destroy_group_member
|
|
|
|
when ProjectMember
|
|
|
|
:destroy_project_member
|
2017-10-11 14:47:08 +00:00
|
|
|
else
|
|
|
|
raise "Unknown member type: #{member}!"
|
2017-03-02 06:01:02 +00:00
|
|
|
end
|
2016-07-28 17:31:17 +00:00
|
|
|
end
|
2016-06-17 12:06:55 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Members::DestroyService.prepend_if_ee('EE::Members::DestroyService')
|