Skip to content

Commit b1e1358

Browse files
authored
Merge pull request #282 from fuseumass/solveSpamProblems
Solve spam problems
2 parents fbca9a8 + d68250e commit b1e1358

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ gem 'ransack'
125125
# Generate PDF
126126
gem 'hexapdf'
127127

128+
# Validate email addresses with MX lookups
129+
gem 'valid_email2'
130+
131+
# Validate user registrations with captcha
132+
gem 'recaptcha'
133+
128134
gem 'omniauth'
129135
gem 'devise_token_auth'
130136
gem 'rack-cors'

Gemfile.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ GEM
138138
thor (>= 0.14, < 2.0)
139139
jquery-ui-rails (6.0.1)
140140
railties (>= 3.2.16)
141+
json (2.6.2)
141142
listen (3.1.5)
142143
rb-fsevent (~> 0.9, >= 0.9.4)
143144
rb-inotify (~> 0.9, >= 0.9.7)
@@ -156,10 +157,12 @@ GEM
156157
nokogiri (~> 1)
157158
rake
158159
mini_mime (1.1.2)
160+
mini_portile2 (2.6.1)
159161
minitest (5.15.0)
160162
multipart-post (2.1.1)
161163
nio4r (2.5.8)
162-
nokogiri (1.12.5-x86_64-linux)
164+
nokogiri (1.12.5)
165+
mini_portile2 (~> 2.6.1)
163166
racc (~> 1.4)
164167
omniauth (2.0.4)
165168
hashie (>= 3.4.6)
@@ -240,6 +243,8 @@ GEM
240243
rb-fsevent (0.11.0)
241244
rb-inotify (0.10.1)
242245
ffi (~> 1.0)
246+
recaptcha (5.9.0)
247+
json
243248
responders (3.0.1)
244249
actionpack (>= 5.0)
245250
railties (>= 5.0)
@@ -297,6 +302,9 @@ GEM
297302
thread_safe (~> 0.1)
298303
uglifier (4.2.0)
299304
execjs (>= 0.3.0, < 3)
305+
valid_email2 (4.0.4)
306+
activemodel (>= 3.2)
307+
mail (~> 2.5)
300308
warden (1.2.9)
301309
rack (>= 2.0.9)
302310
web-console (3.7.0)
@@ -338,6 +346,7 @@ DEPENDENCIES
338346
rails-jquery-autocomplete
339347
rails_12factor
340348
ransack
349+
recaptcha
341350
rqrcode
342351
sass-rails (~> 5.0)
343352
sentry-raven
@@ -348,6 +357,7 @@ DEPENDENCIES
348357
turbolinks (~> 5)
349358
tzinfo-data
350359
uglifier (>= 1.3.0)
360+
valid_email2
351361
web-console (>= 3.3.0)
352362
will_paginate
353363

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class RegistrationsController < Devise::RegistrationsController
2+
def create
3+
if verify_recaptcha
4+
super
5+
else
6+
build_resource(sign_up_params)
7+
clean_up_passwords(resource)
8+
flash.now[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
9+
flash.delete :recaptcha_error
10+
render :new
11+
end
12+
end
13+
end

app/models/user.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class User < ApplicationRecord
22
# Include default devise modules. Others available are:
33
# :confirmable, :lockable, :timeoutable and :omniauthable
4-
validates_presence_of :first_name, :last_name
4+
validates :first_name, :last_name, presence: true, format: { with: /\A[a-zA-Z .'-]+\Z/, message: 'only allows letters, periods, apostrophes, hyphens and spaces' }
5+
# Confirm that the email is not disposable or missing an MX record
6+
validates :email, presence: true, 'valid_email_2/email': {mx: true, disposable: true}
57

68
if !Rails.env.development? && HackumassWeb::Application::EMAIL_VERIFICATION
79
devise :database_authenticatable, :registerable,

app/views/devise/registrations/new.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<label class="form-label">Confirm Password</label>
2626
<%= f.password_field :password_confirmation, placeholder: "Confirm Password", autocomplete: "off", class: "form-control" %>
2727
</div>
28+
<%= flash[:recaptcha_error] %>
29+
<%= recaptcha_tags %>
2830
<div class="form-footer">
2931
<button type="submit" class="btn btn-primary btn-block">Create new account</button>
3032
</div>

config/initializers/recaptcha.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Recaptcha.configure do |config|
2+
config.site_key = ENV['RECAPTCHA_SITE_KEY']
3+
config.secret_key = ENV['RECAPTCHA_SECRET_KEY']
4+
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
get 'users/edit' => 'devise/registrations#edit', :as => :edit_user_registration
2929
delete 'logout', to: 'devise/sessions#destroy', as: :destroy_user_session
3030
get 'signup', to: 'devise/registrations#new', as: :new_user_registration
31-
post 'signup', to: 'devise/registrations#create', as: :user_registration
31+
post 'signup', to: 'registrations#create', as: :user_registration
3232
patch 'signup', to: 'devise/registrations#update', as: :update_user_registration
3333
get 'change_pass', to: 'users#go_to_forgot'
3434
end

0 commit comments

Comments
 (0)