Created
May 3, 2020 03:21
-
-
Save anon987654321/06459b9411251c105c9ab6d93e3f527d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit a33ef0b33c72c03ca3713a5b35104e3094627523 | |
Author: dev <dev@dev.my.domain> | |
Date: Wed Jan 1 00:00:00 2020 -0100 | |
Spam protection with text-based CAPTCHAs | |
diff --git a/Gemfile b/Gemfile | |
index d009330..172128e 100644 | |
--- a/Gemfile | |
+++ b/Gemfile | |
@@ -28,6 +28,9 @@ gem 'image_processing', '~> 1.2' | |
# Flexible authentication solution for Rails with Warden | |
gem 'devise', '~> 4.7', '>= 4.7.1' | |
+# Spam protection with text-based CAPTCHAs | |
+gem 'acts_as_textcaptcha', '~> 4.5' | |
+ | |
# Reduces boot times through caching; required in config/boot.rb | |
gem 'bootsnap', '>= 1.4.2', require: false | |
diff --git a/Gemfile.lock b/Gemfile.lock | |
index 3e0f4f6..adb79ab 100644 | |
--- a/Gemfile.lock | |
+++ b/Gemfile.lock | |
@@ -56,6 +56,7 @@ GEM | |
minitest (~> 5.1) | |
tzinfo (~> 1.1) | |
zeitwerk (~> 2.2) | |
+ acts_as_textcaptcha (4.5.0) | |
addressable (2.7.0) | |
public_suffix (>= 2.0.2, < 5.0) | |
bcrypt (3.1.13) | |
@@ -199,6 +200,7 @@ PLATFORMS | |
ruby | |
DEPENDENCIES | |
+ acts_as_textcaptcha (~> 4.5) | |
bootsnap (>= 1.4.2) | |
byebug | |
capybara (>= 2.15) | |
diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb | |
new file mode 100644 | |
index 0000000..ceb2f29 | |
--- /dev/null | |
+++ b/app/controllers/registrations_controller.rb | |
@@ -0,0 +1,23 @@ | |
+class RegistrationsController < Devise::RegistrationsController | |
+ def new | |
+ @user = User.new | |
+ @user.textcaptcha | |
+ end | |
+ | |
+ def create | |
+ @user = User.new(user_params) | |
+ @user.save | |
+ | |
+ if @user.valid? | |
+ sign_in(@user) | |
+ redirect_to root_path, notice: "Registration successful." | |
+ else | |
+ redirect_to new_user_session_path, warning: "Registration failed." | |
+ end | |
+ end | |
+ | |
+ private | |
+ def user_params | |
+ params.require(:user).permit(:email, :display_name, :password, :password_confirmation, :textcaptcha_answer, :textcaptcha_question, :textcaptcha_key) | |
+ end | |
+end | |
diff --git a/app/models/user.rb b/app/models/user.rb | |
index 80895dd..765edca 100644 | |
--- a/app/models/user.rb | |
+++ b/app/models/user.rb | |
@@ -1,3 +1,5 @@ | |
class User < ApplicationRecord | |
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable | |
+ | |
+ acts_as_textcaptcha | |
end | |
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb | |
index d655b66..901a357 100644 | |
--- a/app/views/devise/registrations/new.html.erb | |
+++ b/app/views/devise/registrations/new.html.erb | |
@@ -21,6 +21,13 @@ | |
<%= f.password_field :password_confirmation, autocomplete: "new-password" %> | |
</div> | |
+ <%= textcaptcha_fields(f) do %> | |
+ <div class="field"> | |
+ <%= f.label :textcaptcha_answer, @user.textcaptcha_question %> | |
+ <%= f.text_field :textcaptcha_answer, :value => "" %> | |
+ </div> | |
+ <% end %> | |
+ | |
<div class="actions"> | |
<%= f.submit "Sign up" %> | |
</div> | |
diff --git a/config/environments/development.rb b/config/environments/development.rb | |
index 7e49c00..8f3eff1 100644 | |
--- a/config/environments/development.rb | |
+++ b/config/environments/development.rb | |
@@ -25,7 +25,8 @@ Rails.application.configure do | |
else | |
config.action_controller.perform_caching = false | |
- config.cache_store = :null_store | |
+ # config.cache_store = :null_store | |
+ config.cache_store = :memory_store | |
end | |
# Store uploaded files on the local file system (see config/storage.yml for options). | |
diff --git a/config/routes.rb b/config/routes.rb | |
index 9fa035f..9a1030b 100644 | |
--- a/config/routes.rb | |
+++ b/config/routes.rb | |
@@ -1,5 +1,5 @@ | |
Rails.application.routes.draw do | |
- devise_for :users | |
+ devise_for :users, controllers: { registrations: "registrations" } | |
resources :posts | |
diff --git a/config/textcaptcha.yml b/config/textcaptcha.yml | |
new file mode 100644 | |
index 0000000..059749e | |
--- /dev/null | |
+++ b/config/textcaptcha.yml | |
@@ -0,0 +1,22 @@ | |
+development: | |
+ questions: | |
+ - question: "Blomst, hvile, advokat og hotell; hvilket ord starter med «a»?" | |
+ answers: "advokat" | |
+ - question: "Hemp, hasj, cannabis og MARIJUANA; hvilket ord er skrevet med store bokstaver?" | |
+ answers: "MARIJUANA,Marijuana, marijuana" | |
+ - question: "I tall, hvor mange farger er det i listen rødt, hvitt og blått?" | |
+ answers: "tre,3" | |
+ - question: "Hva er 1 pluss syv minus fem?" | |
+ answers: "tre,3" | |
+ - question: "Hva er det andre nummeret i listen av 24, 19 og trettifem?" | |
+ answers: "19" | |
+ - question: "Hvilket nummer er størst av 12, 19 og 382?" | |
+ answers: "382" | |
+ - question: "Studio, bibliotek, kake og rød; hvilket ord er en farge?" | |
+ answers: "rød" | |
+ - question: "Skriv nummeret tretten tusen fem hundre og trettifem i tall:" | |
+ answers: "3535" | |
+ - question: "Er is varmt eller kaldt?" | |
+ answers: "kaldt" | |
+ - question: "Hvilken farge er grønt?" | |
+ answers: "grønt,grønn" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment