demarches-normaliennes/app/controllers/webhook_controller.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2018-08-29 21:26:22 +02:00
class WebhookController < ActionController::Base
before_action :verify_signature!, only: :helpscout
def helpscout
email = params[:customer][:email].downcase
2018-08-29 21:26:22 +02:00
user = User.find_by(email: email)
instructeur = user.instructeur
administrateur = user.administrateur
2018-08-29 21:26:22 +02:00
html = []
if user
2018-09-18 12:06:27 +02:00
url = manager_user_url(user)
2018-08-29 21:26:22 +02:00
html << link_to_manager(user, url)
end
if instructeur
url = manager_instructeur_url(instructeur)
html << link_to_manager(instructeur, url)
2018-08-29 21:26:22 +02:00
end
if administrateur
2018-09-18 12:06:27 +02:00
url = manager_administrateur_url(administrateur)
2018-08-29 21:26:22 +02:00
html << link_to_manager(administrateur, url)
end
if html.empty?
head :not_found
else
render json: { html: html.join('<br>') }
end
end
private
def link_to_manager(model, url)
"<a target='_blank' href='#{url}' rel='noopener'>#{model.model_name.human}##{model.id}</a>"
2018-08-29 21:26:22 +02:00
end
def verify_signature!
if generate_body_signature(request.body.read) != request.headers['X-Helpscout-Signature']
request_http_token_authentication
end
end
def generate_body_signature(body)
Base64.strict_encode64(OpenSSL::HMAC.digest('sha1',
Rails.application.secrets.helpscout[:webhook_secret],
body))
end
end