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

49 lines
1.3 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
def execute(member, skip_authorization: false)
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member)
2016-08-24 12:49:21 +05:30
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
2018-11-18 11:00:15 +05:30
enqeue_delete_todos(member)
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
2018-11-18 11:00:15 +05:30
def enqeue_delete_todos(member)
type = member.is_a?(GroupMember) ? 'Group' : 'Project'
# don't enqueue immediately to prevent todos removal in case of a mistake
TodosDestroyer::EntityLeaveWorker.perform_in(1.hour, member.user_id, member.source_id, type)
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