-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement feature to ban users (#214)
We currently have some users that cause some spam in the platform and we need to ban them. We can't only remove they from the platform, because this can be easily reverted (they can re-signup). This commit adds a flag in the users table to identify if the user is banned. If the flag is present, we don't allow them to login.
- Loading branch information
1 parent
075ed12
commit 152414d
Showing
6 changed files
with
91 additions
and
53 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class UserAllowedGuard < Clearance::SignInGuard | ||
def call | ||
return next_guard if !signed_in? || allowed? | ||
|
||
failure('Your user is banned.') | ||
end | ||
|
||
def allowed? | ||
!current_user.banned? | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
config.routes = false | ||
config.mailer_sender = '[email protected]' | ||
config.rotate_csrf_on_sign_in = true | ||
config.sign_in_guards = [UserAllowedGuard] | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddBannedToUsers < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :banned, :boolean, null: false, default: false | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.feature 'Signing in with a Banned User' do | ||
scenario 'does not allow banned user to sign in' do | ||
create(:user, email: '[email protected]', password: 'password', banned: true) | ||
|
||
visit sign_in_path | ||
|
||
fill_in 'E-mail', with: '[email protected]' | ||
fill_in 'Password', with: 'password' | ||
|
||
click_button 'Sign in' | ||
|
||
expect(page).to have_text 'Your user is banned.' | ||
end | ||
|
||
scenario 'allows unbanned user to sign in' do | ||
create(:user, email: '[email protected]', password: 'password', banned: false) | ||
|
||
visit sign_in_path | ||
|
||
fill_in 'E-mail', with: '[email protected]' | ||
fill_in 'Password', with: 'password' | ||
|
||
click_button 'Sign in' | ||
|
||
expect(page).not_to have_text 'Your user is banned.' | ||
end | ||
end |