2016-08-24 12:49:21 +05:30
|
|
|
module Members
|
|
|
|
class DestroyService < BaseService
|
2016-11-03 12:29:30 +05:30
|
|
|
include MembersHelper
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
attr_accessor :source
|
|
|
|
|
|
|
|
ALLOWED_SCOPES = %i[members requesters all]
|
|
|
|
|
|
|
|
def initialize(source, current_user, params = {})
|
|
|
|
@source = source
|
2016-09-13 17:45:13 +05:30
|
|
|
@current_user = current_user
|
2016-11-03 12:29:30 +05:30
|
|
|
@params = params
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def execute(scope = :members)
|
|
|
|
raise "scope :#{scope} is not allowed!" unless ALLOWED_SCOPES.include?(scope)
|
|
|
|
|
|
|
|
member = find_member!(scope)
|
|
|
|
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless can_destroy_member?(member)
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
AuthorizedDestroyService.new(member, current_user).execute
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_member!(scope)
|
|
|
|
condition = params[:user_id] ? { user_id: params[:user_id] } : { id: params[:id] }
|
|
|
|
case scope
|
|
|
|
when :all
|
|
|
|
source.members.find_by(condition) ||
|
|
|
|
source.requesters.find_by!(condition)
|
|
|
|
else
|
|
|
|
source.public_send(scope).find_by!(condition)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_destroy_member?(member)
|
|
|
|
member && can?(current_user, action_member_permission(:destroy, member), member)
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|