-
Notifications
You must be signed in to change notification settings - Fork 166
Add the ability to ban users #5875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 BannedUserController < ApplicationController | ||
| def show | ||
| analytics.track_event(Analytics::BANNED_USER_VISITED) | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| class SignInRestriction < ApplicationRecord | ||
| belongs_to :user | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| class BannedUserResolver | ||
| attr_reader :user | ||
|
|
||
| def initialize(user) | ||
| @user = user | ||
| end | ||
|
|
||
| def banned_for_sp?(issuer:) | ||
| user.sign_in_restrictions.where(service_provider: [nil, issuer]).any? | ||
| end | ||
| end |
This file contains hidden or 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,7 @@ | ||
| <% title t('banned_user.title') %> | ||
|
|
||
| <%= image_tag('alert/fail-x.svg', width: 54, alt: '', class: 'display-block margin-bottom-4') %> | ||
| <%= render PageHeadingComponent.new.with_content(t('banned_user.title')) %> | ||
| <p> | ||
| <%= t('banned_user.details') %> | ||
| </p> |
This file contains hidden or 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,4 @@ | ||
| en: | ||
| banned_user: | ||
| details: We are unable to authenticate you at this time. | ||
| title: Access is restricted |
This file contains hidden or 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,4 @@ | ||
| es: | ||
| banned_user: | ||
| details: No podemos autenticarlo en este momento. | ||
| title: El acceso está restringido |
This file contains hidden or 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,4 @@ | ||
| fr: | ||
| banned_user: | ||
| details: Nous ne sommes pas en mesure de vous authentifier pour le moment. | ||
| title: L’accès est restreint |
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
db/primary_migrate/20220129181752_create_sign_in_restrictions.rb
This file contains hidden or 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,17 @@ | ||||||||
| class CreateSignInRestrictions < ActiveRecord::Migration[6.1] | ||||||||
| def change | ||||||||
| create_table :sign_in_restrictions do |t| | ||||||||
| t.integer :user_id, null: false | ||||||||
| t.string :service_provider | ||||||||
|
|
||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe for the future we can add a followup for with a reason (internal only, just for record keeping)
Suggested change
|
||||||||
| t.timestamps | ||||||||
| end | ||||||||
|
|
||||||||
| add_index( | ||||||||
| :sign_in_restrictions, | ||||||||
| [:user_id, :service_provider], | ||||||||
| unique: true, | ||||||||
| name: :index_sign_in_restrictions_on_user_id_and_service_provider, | ||||||||
| ) | ||||||||
| end | ||||||||
| end | ||||||||
This file contains hidden or 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 hidden or 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,70 @@ | ||
| require 'rails_helper' | ||
|
|
||
| feature 'Banning users for an SP' do | ||
| include SamlAuthHelper | ||
|
|
||
| context 'a user is banned from all SPs' do | ||
| it 'does not let the user sign in to any SP' do | ||
| user = create(:user, :signed_up) | ||
|
|
||
| SignInRestriction.create(user: user) | ||
|
|
||
| sign_in_user(user) | ||
| expect_user_to_be_banned | ||
|
|
||
| visit_idp_from_sp_with_ial1(:saml) | ||
| sign_in_user(user) | ||
| expect_user_to_be_banned | ||
|
|
||
| visit_idp_from_sp_with_ial1(:oidc) | ||
| sign_in_user(user) | ||
| expect_user_to_be_banned | ||
| end | ||
| end | ||
|
|
||
| context 'a user is banned for a SAML SP' do | ||
| it 'bans the user from signing in to the banned SP but allows other sign ins' do | ||
| user = create(:user, :signed_up) | ||
|
|
||
| SignInRestriction.create(user: user, service_provider: 'http://localhost:3000') | ||
|
|
||
| sign_in_live_with_2fa(user) | ||
| expect(current_path).to eq(account_path) | ||
|
|
||
| visit_idp_from_sp_with_ial1(:saml) | ||
| expect_user_to_be_banned | ||
|
|
||
| visit_idp_from_sp_with_ial1(:oidc) | ||
| sign_in_live_with_2fa(user) | ||
| click_agree_and_continue | ||
| expect(current_url).to start_with('http://localhost:7654/auth/result') | ||
| end | ||
| end | ||
|
|
||
| context 'a user is banner for an OIDC SP' do | ||
| it 'bans the user from signing in to the banned SP but allows other sign ins' do | ||
| user = create(:user, :signed_up) | ||
|
|
||
| SignInRestriction.create(user: user, service_provider: 'urn:gov:gsa:openidconnect:sp:server') | ||
|
|
||
| sign_in_live_with_2fa(user) | ||
| expect(current_path).to eq(account_path) | ||
|
|
||
| visit_idp_from_sp_with_ial1(:oidc) | ||
| expect_user_to_be_banned | ||
|
|
||
| visit_idp_from_sp_with_ial1(:saml) | ||
| sign_in_live_with_2fa(user) | ||
| click_agree_and_continue | ||
| expect(current_path).to eq(api_saml_auth2022_path) | ||
| end | ||
| end | ||
|
|
||
| def expect_user_to_be_banned | ||
| expect(current_path).to eq(banned_user_path) | ||
| expect(page).to have_content(I18n.t('banned_user.title')) | ||
|
|
||
| visit account_path | ||
| expect(current_path).to eq(new_user_session_path) | ||
| end | ||
| end |
This file contains hidden or 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,47 @@ | ||
| require 'rails_helper' | ||
|
|
||
| describe BannedUserResolver do | ||
| context 'the user is not banned' do | ||
| it 'returns false' do | ||
| user = create(:user) | ||
| sp = create(:service_provider) | ||
|
|
||
| expect(described_class.new(user).banned_for_sp?(issuer: sp.issuer)).to eq(false) | ||
| end | ||
| end | ||
|
|
||
| context 'the user is banned for a single SP' do | ||
| it 'returns true for the banned SP' do | ||
| user = create(:user) | ||
| sp = create(:service_provider) | ||
|
|
||
| SignInRestriction.create(user: user, service_provider: sp.issuer) | ||
|
|
||
| expect(described_class.new(user).banned_for_sp?(issuer: sp.issuer)).to eq(true) | ||
| end | ||
|
|
||
| it 'returns false for the not banned SP' do | ||
| user = create(:user) | ||
| sp = create(:service_provider) | ||
| banned_sp = create(:service_provider) | ||
|
|
||
| SignInRestriction.create(user: user, service_provider: banned_sp.issuer) | ||
|
|
||
| expect(described_class.new(user).banned_for_sp?(issuer: sp.issuer)).to eq(false) | ||
| end | ||
| end | ||
|
|
||
| context 'the user is banned for all SPs' do | ||
| it 'returns true for all SPs' do | ||
| user = create(:user) | ||
| sp1 = create(:service_provider) | ||
| sp2 = create(:service_provider) | ||
|
|
||
| SignInRestriction.create(user: user, service_provider: nil) | ||
|
|
||
| expect(described_class.new(user).banned_for_sp?(issuer: sp1.issuer)).to eq(true) | ||
| expect(described_class.new(user).banned_for_sp?(issuer: sp2.issuer)).to eq(true) | ||
| expect(described_class.new(user).banned_for_sp?(issuer: nil)).to eq(true) | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.