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

42 lines
976 B
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module WebHooks
2022-08-27 11:52:29 +05:30
# Destroy a hook, and schedule the logs for deletion.
2021-01-03 14:25:43 +05:30
class DestroyService
2022-08-27 11:52:29 +05:30
include Services::ReturnServiceResponses
2022-07-23 23:45:48 +05:30
attr_accessor :current_user
2021-01-03 14:25:43 +05:30
2022-08-27 11:52:29 +05:30
DENIED = 'Insufficient permissions'
2021-01-03 14:25:43 +05:30
def initialize(current_user)
@current_user = current_user
end
def execute(web_hook)
2022-08-27 11:52:29 +05:30
return error(DENIED, 401) unless authorized?(web_hook)
2022-07-23 23:45:48 +05:30
hook_id = web_hook.id
2021-01-03 14:25:43 +05:30
2022-07-23 23:45:48 +05:30
if web_hook.destroy
WebHooks::LogDestroyWorker.perform_async({ 'hook_id' => hook_id })
2022-08-27 11:52:29 +05:30
Gitlab::AppLogger.info(log_message(web_hook))
2021-01-03 14:25:43 +05:30
2022-08-27 11:52:29 +05:30
success({ async: false })
2021-01-03 14:25:43 +05:30
else
2022-08-27 11:52:29 +05:30
error("Unable to destroy #{web_hook.model_name.human}", 500)
2021-01-03 14:25:43 +05:30
end
end
2022-08-27 11:52:29 +05:30
private
def log_message(hook)
"User #{current_user&.id} scheduled a deletion of logs for hook ID #{hook.id}"
end
def authorized?(web_hook)
Ability.allowed?(current_user, :destroy_web_hook, web_hook)
end
2021-01-03 14:25:43 +05:30
end
end