Skip to content

Commit

Permalink
Add recaptcha to articles page. (#4850)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkinzer authored Jan 9, 2025
1 parent 65ca172 commit 51779ff
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ group :production do
gem "dalli"
gem "connection_pool"
end

gem "recaptcha", "~> 5.18"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ GEM
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
recaptcha (5.18.0)
regexp_parser (2.10.0)
reline (0.5.10)
io-console (~> 0.5)
Expand Down Expand Up @@ -761,6 +762,7 @@ DEPENDENCIES
rack-mini-profiler
rails (= 7.1.5.1)
rails-controller-testing
recaptcha (~> 5.18)
rsolr (~> 2.6)
rspec-rails
rubocop
Expand Down
7 changes: 6 additions & 1 deletion app/components/blacklight/search_bar_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
<%= search_button || render(Blacklight::SearchButtonComponent.new(id: "#{@prefix}search", text: scoped_t('submit'))) %>
</span>
</div>

<% if @recaptcha_action %>
<%= recaptcha_v3(action: @recaptcha_action) %>
<% end %>

<% end %>

<% if advanced_search_enabled? %>
<%= link_to t('blacklight.advanced_search.more_options'), @advanced_search_url, class: 'advanced_search btn btn-secondary'%>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion app/components/blacklight/search_bar_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SearchBarComponent < Blacklight::Component

# rubocop:disable Metrics/ParameterLists
def initialize(
url:, advanced_search_url: nil, params:,
url:, advanced_search_url: nil, params:, recaptcha_action:,
classes: ["search-query-form, d-flex "], presenter: nil, prefix: nil,
method: "GET", q: nil, query_param: :q,
search_field: nil, search_fields: nil, autocomplete_path: nil,
Expand All @@ -32,6 +32,7 @@ def initialize(
@search_fields = search_fields
@i18n = i18n
@form_options = form_options
@recaptcha_action = recaptcha_action

return if presenter.nil?

Expand Down
20 changes: 20 additions & 0 deletions app/controllers/primo_central_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,30 @@ class PrimoCentralController < CatalogController
include CatalogConfigReinit
include Blacklight::Document::Export

before_action :recaptcha, only: [ :index ]

helper_method :browse_creator
helper_method :tags_strip
helper_method :solr_range_queries_to_a

def recaptcha
# skip if recaptcha site key not set (using as feature flag)
return if ENV["RECAPTCHA_SITE_KEY"].blank?

@recaptcha_action = "articles_search"

# skip if referred from app.
return if request.referer.present? && URI.parse(request.referer).host == request.host

# skip if nothing to query.
return if params["q"].blank?

if !verify_recaptcha(action: @recaptcha_action)
head :forbidden
end
end


def advanced_override_path
end

Expand Down
1 change: 1 addition & 0 deletions app/views/catalog/_search_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%= render(Blacklight::SearchBarComponent.new(
url: search_action_url,
params: search_state.params_for_search.except(:qt),
recaptcha_action: @recaptcha_action,
search_fields: Deprecation.silence(Blacklight::ConfigurationHelperBehavior) { search_fields },
)) %>

Expand Down

0 comments on commit 51779ff

Please sign in to comment.