debian-mirror-gitlab/app/services/members/destroy_service.rb

69 lines
1.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
module Members
2018-03-27 19:54:05 +05:30
class DestroyService < Members::BaseService
2019-01-03 12:48:30 +05:30
def execute(member, skip_authorization: false, skip_subresources: false)
2018-03-27 19:54:05 +05:30
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member)
2016-08-24 12:49:21 +05:30
2019-01-03 12:48:30 +05:30
@skip_auth = skip_authorization
2018-03-27 19:54:05 +05:30
return member if member.is_a?(GroupMember) && member.source.last_owner?(member.user)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
member.destroy
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
member.user&.invalidate_cache_counts
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
if member.request? && member.user != current_user
notification_service.decline_access_request(member)
end
2016-11-03 12:29:30 +05:30
2019-01-03 12:48:30 +05:30
delete_subresources(member) unless skip_subresources
enqueue_delete_todos(member)
2018-11-18 11:00:15 +05:30
2018-03-27 19:54:05 +05:30
after_execute(member: member)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
member
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
private
2019-01-03 12:48:30 +05:30
def delete_subresources(member)
return unless member.is_a?(GroupMember) && member.user && member.group
delete_project_members(member)
2019-02-15 15:39:39 +05:30
delete_subgroup_members(member) if Group.supports_nested_objects?
2019-01-03 12:48:30 +05:30
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-09-04 21:01:54 +05:30
GroupMember.of_groups(groups).with_user(member.user).each do |group_member|
2019-01-03 12:48:30 +05:30
self.class.new(current_user).execute(group_member, skip_authorization: @skip_auth, skip_subresources: true)
end
end
2016-11-03 12:29:30 +05:30
def can_destroy_member?(member)
2018-03-27 19:54:05 +05:30
can?(current_user, destroy_member_permission(member), member)
2018-03-17 18:26:18 +05:30
end
def destroy_member_permission(member)
case member
when GroupMember
:destroy_group_member
when ProjectMember
:destroy_project_member
2018-03-27 19:54:05 +05:30
else
raise "Unknown member type: #{member}!"
2018-03-17 18:26:18 +05:30
end
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
end
end