2016-06-17 08:06:55 -04:00
|
|
|
module Members
|
2017-10-11 10:47:08 -04:00
|
|
|
class DestroyService < Members::BaseService
|
2018-02-16 09:10:22 -05:00
|
|
|
def execute(member, skip_authorization: false)
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member)
|
2016-07-28 13:31:17 -04:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
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
|