diff --git a/Gemfile b/Gemfile index 48df8b2d7..1245d3ba1 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ gem 'sass-rails', '~> 5.0' gem 'bootstrap_form' gem 'yomu' gem 'font-awesome-rails', '~> 4.6' +gem 'recaptcha', require: 'recaptcha/rails' # JS datetime library, requirement of datetime picker gem 'momentjs-rails', '>= 2.9.0' diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 950ecfd44..ba1c2ce95 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,5 +1,6 @@ class Users::RegistrationsController < Devise::RegistrationsController before_action :load_paperclip_vars + prepend_before_action :check_captcha, only: [:create] def avatar user = User.find_by_id(params[:id]) || current_user @@ -253,6 +254,15 @@ class Users::RegistrationsController < Devise::RegistrationsController private + def check_captcha + if Rails.configuration.x.enable_recaptcha + unless verify_recaptcha + self.resource = resource_class.new sign_up_params + respond_with_navigational(resource) { render :new } + end + end + end + # Redirect to login page after signing up def after_sign_up_path_for(resource) new_user_session_path diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb index 563d7362b..cdc6117b9 100644 --- a/app/views/users/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -38,6 +38,13 @@ <%= t 'users.registrations.new.team_name_help' %> + <% if Rails.configuration.x.enable_recaptcha %> +
+ <%= recaptcha_tags %> + <%= flash[:recaptcha_error] %> +
+ <% end %> +
<%= f.submit "Sign up", class: "btn btn-primary" %>
diff --git a/config/environments/development.rb b/config/environments/development.rb index a1cc7d013..1b3e2454f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -68,4 +68,7 @@ Rails.application.configure do # Enable first-time tutorial for users signing in the sciNote for # the first time. config.x.enable_tutorial = ENV["ENABLE_TUTORIAL"] == "true" + + # Enable reCAPTCHA + config.x.enable_recaptcha = ENV["ENABLE_RECAPTCHA"] == "true" end diff --git a/config/environments/production.rb b/config/environments/production.rb index 6fafc23c8..06ddae208 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -101,4 +101,7 @@ Rails.application.configure do # Enable first-time tutorial for users signing in the sciNote for # the first time. config.x.enable_tutorial = ENV["ENABLE_TUTORIAL"] != "false" + + # Enable reCAPTCHA + config.x.enable_recaptcha = ENV["ENABLE_RECAPTCHA"] == "true" end diff --git a/config/environments/test.rb b/config/environments/test.rb index c3a380d8c..8d974dc89 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -68,4 +68,7 @@ Rails.application.configure do # Enable first-time tutorial for users signing in the sciNote for # the first time. config.x.enable_tutorial = false + + # Enable reCAPTCHA + config.x.enable_recaptcha = true end