From e0232c9f7b029d2964eaded4e7624d140a36fdc5 Mon Sep 17 00:00:00 2001 From: lamtu1 Date: Mon, 11 Mar 2024 11:40:55 -0700 Subject: [PATCH] Add in recaptcha --- .../torrent_form_recaptcha_behavior.rb | 27 +++++++++++++++++++ .../torrent_form_controller.rb | 4 +++ app/models/scholars_archive/torrent_form.rb | 9 +++++++ .../torrent_form/new.html.erb | 6 ++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/controllers/concerns/scholars_archive/torrent_form_recaptcha_behavior.rb diff --git a/app/controllers/concerns/scholars_archive/torrent_form_recaptcha_behavior.rb b/app/controllers/concerns/scholars_archive/torrent_form_recaptcha_behavior.rb new file mode 100644 index 000000000..acb941e5b --- /dev/null +++ b/app/controllers/concerns/scholars_archive/torrent_form_recaptcha_behavior.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module ScholarsArchive + # CLASS: Torrent Form Recaptcha Controller + module TorrentFormRecaptchaBehavior + # METHOD: Check the recaptcha to see if verify or not + def check_recaptcha + if is_recaptcha? + if verify_recaptcha(model: @torrent_form) + true + else + flash[:error] = 'Captcha did not verify properly.' + false + end + else + true + end + end + + # METHOD: A check method to see if recaptcha exist + # rubocop:disable Naming/PredicateName + def is_recaptcha? + Hyrax.config.recaptcha? + end + # rubocop:enable Naming/PredicateName + end +end diff --git a/app/controllers/scholars_archive/torrent_form_controller.rb b/app/controllers/scholars_archive/torrent_form_controller.rb index 00e7333fa..07b3b3d0b 100644 --- a/app/controllers/scholars_archive/torrent_form_controller.rb +++ b/app/controllers/scholars_archive/torrent_form_controller.rb @@ -5,6 +5,7 @@ module ScholarsArchive class TorrentFormController < ApplicationController # ACTION: Before page load, build the form with all the params before_action :build_torrent_form + invisible_captcha only: [:create] layout 'homepage' def new; end @@ -13,6 +14,9 @@ def create render :new end + # NOTE: Override if needed to perform after email delivery + def after_deliver; end + private # METHOD: Create a new form with all the params diff --git a/app/models/scholars_archive/torrent_form.rb b/app/models/scholars_archive/torrent_form.rb index 2c737a671..460945965 100644 --- a/app/models/scholars_archive/torrent_form.rb +++ b/app/models/scholars_archive/torrent_form.rb @@ -14,5 +14,14 @@ class TorrentForm def spam? torrent_method.present? end + + # HEADER: Declare the e-mail headers. It accepts anything the mail method in ActionMailer accepts + def headers + { + subject: 'Scholars Archive Torrent Form: Report of Torrent', + to: Hyrax.config.contact_email, + from: email + } + end end end diff --git a/app/views/scholars_archive/torrent_form/new.html.erb b/app/views/scholars_archive/torrent_form/new.html.erb index 88648ad9f..ff4770672 100644 --- a/app/views/scholars_archive/torrent_form/new.html.erb +++ b/app/views/scholars_archive/torrent_form/new.html.erb @@ -38,5 +38,9 @@
<%= f.file_field :additional_item, class: 'form-control' %>
+ <%# CAPTCHA: Add in the captcha on form %> + <%= recaptcha_tags if Hyrax.config.recaptcha? %> + <%= invisible_captcha %> + <%= f.submit value: t('hyrax.contact_form.button_label'), class: "btn btn-primary" %> -<% end %> \ No newline at end of file +<% end %>