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
|
2018-11-16 10:09:32 -05:00
|
|
|
def execute(member, skip_authorization: false, skip_subresources: false)
|
2018-02-16 09:10:22 -05:00
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member)
|
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
|
2018-12-11 13:15:10 -05:00
|
|
|
enqueue_delete_todos(member)
|
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
|
|
|
|
|
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)
|
|
|
|
delete_subgroup_members(member) if Group.supports_nested_objects?
|
|
|
|
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
|
|
|
|
|
|
|
|
GroupMember.in_groups(groups).with_user(member.user).each do |group_member|
|
|
|
|
self.class.new(current_user).execute(group_member, skip_authorization: @skip_auth, skip_subresources: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
end
|