From 8d8a2f9c1e804a17d7196fa0351469e6a633e6e9 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 28 Nov 2017 22:37:02 -0600 Subject: [PATCH] Make recaptcha optional on signup page. --- app/controllers/users_controller.rb | 2 +- app/views/users/new.html.erb | 6 +++++- config/danbooru_default_config.rb | 12 ++++++++++++ config/initializers/recaptcha.rb | 5 +++++ test/functional/users_controller_test.rb | 6 +----- 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 config/initializers/recaptcha.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 60778d866..52622b9a4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -44,7 +44,7 @@ class UsersController < ApplicationController def create @user = User.new(params[:user], :as => CurrentUser.role) @user.last_ip_addr = request.remote_ip - if verify_recaptcha(model: @user) + if !Danbooru.config.enable_recaptcha? || verify_recaptcha(model: @user) @user.save if @user.errors.empty? session[:user_id] = @user.id diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index d8d188a44..c40ebbef0 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -15,7 +15,11 @@ <%= f.input :password %> <%= f.input :password_confirmation %> - <%= invisible_recaptcha_tags callback: 'submitInvisibleRecaptchaForm', text: 'Sign up' %> + <% if Danbooru.config.enable_recaptcha? %> + <%= invisible_recaptcha_tags callback: 'submitInvisibleRecaptchaForm', text: 'Sign up' %> + <% else %> + <%= f.submit "Sign up", :data => { :disable_with => "Signing up..." } %> + <% end %> <% end %> diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index dd2e88dec..93383f1b1 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -619,6 +619,18 @@ module Danbooru def aws_sqs_cropper_url end + + # Use a recaptcha on the signup page to protect against spambots creating new accounts. + # https://developers.google.com/recaptcha/intro + def enable_recaptcha? + Rails.env.production? && Danbooru.config.recaptcha_site_key.present? && Danbooru.config.recaptcha_secret_key.present? + end + + def recaptcha_site_key + end + + def recaptcha_secret_key + end end class EnvironmentConfiguration diff --git a/config/initializers/recaptcha.rb b/config/initializers/recaptcha.rb new file mode 100644 index 000000000..af169cd35 --- /dev/null +++ b/config/initializers/recaptcha.rb @@ -0,0 +1,5 @@ +Recaptcha.configure do |config| + config.site_key = Danbooru.config.recaptcha_site_key + config.secret_key = Danbooru.config.recaptcha_secret_key + # config.proxy = "http://example.com" +end diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 624fbd8f3..70d1945dd 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -76,11 +76,7 @@ class UsersControllerTest < ActionController::TestCase context "new action" do setup do - ENV["RECAPTCHA_SITE_KEY"] = "x" - end - - teardown do - ENV["RECAPTCHA_SITE_KEY"] = nil + Danbooru.config.stubs(:enable_recaptcha?).returns(false) end should "render" do