Skip to content

Commit 7530b0b

Browse files
committed
Attempts to reduce spam registrations
* Don't display website until user has signed in 10 times * Don't display unverified users in index * Honeypot field
1 parent 0d1968a commit 7530b0b

7 files changed

Lines changed: 58 additions & 13 deletions

File tree

app/controllers/tess_devise/registrations_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def update_resource(resource, params)
2525

2626
# Pinched from https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise
2727
def check_captcha
28-
if !Rails.application.config.secrets.recaptcha[:sitekey].blank? && !verify_recaptcha
28+
if (Rails.application.config.secrets.recaptcha[:sitekey].present? && !verify_recaptcha) ||
29+
params.dig('user', 'website').present?
2930
self.resource = resource_class.new sign_up_params
3031
respond_with_navigational(resource) { render :new }
3132
end

app/controllers/users_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class UsersController < ApplicationController
1414
# GET /users.json
1515
def index
1616
@users = User.visible
17-
@users = @users.with_query(params[:q].chomp('*')) if params[:q].present?
17+
if params[:q].present?
18+
@users = @users.with_query(params[:q].chomp('*'))
19+
elsif !(current_user&.is_admin? || current_user&.is_curator?)
20+
@users = @users.verified
21+
end
1822
@users = @users.paginate(page: params[:page], per_page: 50)
1923

2024
respond_to do |format|
@@ -26,7 +30,7 @@ def index
2630

2731
# GET/invitees
2832
def invitees
29-
if current_user.is_admin? || current_user.is_curator?
33+
if current_user&.is_admin? || current_user&.is_curator?
3034
@users = User.invited
3135
respond_to do |format|
3236
format.html

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ def has_space_role?(space, role)
379379
space_roles.where(key: role, space: space).any?
380380
end
381381

382+
def show_website?
383+
sign_in_count > 10
384+
end
385+
382386
protected
383387

384388
def reassign_resources(new_owner = User.get_default_user)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@
2929

3030
<%= f.input :email, required: true %>
3131

32+
<div style="opacity: 0; height: 0; overflow: hidden">
33+
<div class="form-group website required user_website">
34+
<label class="control-label website" for="user_website"><abbr title="required">*</abbr> Website</label>
35+
<input class="form-control string website required" type="text" value="" name="user[website]" id="user_website">
36+
</div>
37+
</div>
38+
3239
<%= f.input :publicize_email, as: :boolean, label: 'Make my email publicly visible' %>
3340

3441
<%= f.input :password, required: true, input_html: { autocomplete: 'off' },
35-
hint: "(#{@minimum_password_length} characters minimum)" if @minimum_password_length %>
42+
hint: @minimum_password_length ? "(#{@minimum_password_length} characters minimum)" : nil %>
3643

3744
<%= f.input :password_confirmation, required: true, input_html: { autocomplete: 'off' } %>
3845

app/views/users/show.html.erb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@
3636
<% end %>
3737
</p>
3838

39-
<p>
40-
<strong><%= model_class.human_attribute_name(:website) %></strong><br/>
41-
<% if @user.profile.website.blank? %>
42-
<span class="empty">None specified</span>
43-
<% else %>
44-
<%= link_to @user.profile.website, @user.profile.website, rel: 'nofollow', target: '_blank' %>
45-
<% end %>
46-
</p>
39+
<% if @user.show_website? || (current_user&.is_admin? || current_user&.is_curator?) %>
40+
<p>
41+
<strong><%= model_class.human_attribute_name(:website) %></strong><br/>
42+
<% if @user.profile.website.blank? %>
43+
<span class="empty">None specified</span>
44+
<% else %>
45+
<%= link_to @user.profile.website, @user.profile.website, rel: 'nofollow', target: '_blank' %>
46+
<% end %>
47+
</p>
48+
<% end %>
4749

4850
<p>
4951
<strong>ORCID</strong><br/>

test/controllers/tess_devise/registrations_controller_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ class RegistrationsControllerTest < ActionController::TestCase
6868
assert assigns(:user).errors[:base].first.include?('processing')
6969
end
7070

71+
test 'should not register user if website given' do
72+
assert_no_difference('User.count') do
73+
post :create, params: {
74+
user: {
75+
username: 'mileyfan1997',
76+
email: 'h4nn4hm0nt4n4@example.com',
77+
password: '12345678',
78+
password_confirmation: '12345678',
79+
processing_consent: '1',
80+
website: 'https://myhomepage.com'
81+
}
82+
}
83+
end
84+
end
85+
7186
test 'should redirect to user page after changing password' do
7287
user = users(:regular_user)
7388
sign_in user

test/controllers/users_controller_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,19 @@ class UsersControllerTest < ActionController::TestCase
380380
refute assigns(:users).include?(users(:another_regular_user))
381381
end
382382

383-
test 'should not show banned or basic users in index' do
383+
test 'should not show banned or basic users in index if not admin' do
384+
get :index
385+
assert_response :success
386+
all_users = assigns(:users).to_a
387+
assert users(:shadowbanned_user).banned?
388+
assert_not_includes all_users, users(:shadowbanned_user)
389+
assert_not_includes all_users, users(:unverified_user)
390+
assert_not_includes all_users, users(:basic_user)
391+
assert_includes all_users, users(:regular_user)
392+
end
393+
394+
test 'should show banned or basic users in index if admin' do
395+
sign_in @admin
384396
get :index
385397
assert_response :success
386398
all_users = assigns(:users).to_a

0 commit comments

Comments
 (0)