Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit a6bb27c

Browse files
author
Philippe Hässig
committed
Reject login when a user is locked
When a user is locked (locked_until is in the future) he can't log in anymore.
1 parent 967fa33 commit a6bb27c

5 files changed

Lines changed: 30 additions & 1 deletion

File tree

app/controllers/casino/sessions_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def create
2525
if !validation_result
2626
handle_failed_login params[:username]
2727
show_login_error I18n.t('login_credential_acceptor.invalid_login_credentials')
28+
elsif user_from_validation_result(validation_result).locked?
29+
show_login_error I18n.t('sessions.create.user_locked')
2830
else
2931
sign_in(validation_result, long_term: params[:rememberMe], credentials_supplied: true)
3032
end
@@ -83,4 +85,11 @@ def load_ticket_granting_ticket_from_parameter
8385
@ticket_granting_ticket = find_valid_ticket_granting_ticket(params[:tgt], request.user_agent, ignore_two_factor: true)
8486
redirect_to login_path if @ticket_granting_ticket.nil?
8587
end
88+
89+
def user_from_validation_result(validation_result)
90+
user_data = validation_result[:user_data]
91+
load_or_initialize_user(validation_result[:authenticator],
92+
user_data[:username],
93+
user_data[:extra_attributes])
94+
end
8695
end

config/locales/de.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ de:
22
login_credential_acceptor:
33
invalid_login_ticket: "Ihre Anfrage enthielt kein gültiges Login-Ticket."
44
invalid_login_credentials: "Benutzername oder Passwort falsch."
5+
user_is_locked: "Ihr Account ist wegen zu vieler falscher Loginversuche gesperrt. Bitte versuchen Sie es später nochmal."
56
login:
67
label_username: "Benutzername"
78
label_password: "Passwort"

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ en:
22
login_credential_acceptor:
33
invalid_login_ticket: "Your login request did not include a valid login ticket."
44
invalid_login_credentials: "Incorrect username or password."
5+
user_is_locked: "Your user is currently locked because of failed login attempts. Please try again later."
56
login:
67
label_username: "Username"
78
label_password: "Password"

config/locales/fr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ fr:
22
login_credential_acceptor:
33
invalid_login_ticket: "La demande de connexion n'inclue pas un ticket de connexion valide."
44
invalid_login_credentials: "Nom d'utilisateur ou mot de passe incorrect."
5+
user_is_locked: "Votre utilisateur est actuellement bloqué dû à des tentatives de connexions échouées. Veuillez réessayer ultérieurement."
56
login:
67
label_username: "Nom d'utilisateur"
78
label_password: "Mot de passe"

spec/controllers/sessions_controller_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@
188188
let(:login_ticket) { FactoryGirl.create :login_ticket }
189189
let(:username) { 'testuser' }
190190
let(:params) { { lt: login_ticket.ticket, username: username, password: 'wrrooonnng' }}
191-
let!(:user) { FactoryGirl.create :user, username: username }
191+
let(:locked_until) { nil }
192+
let!(:user) { FactoryGirl.create :user, authenticator: 'static', username: username, locked_until: locked_until }
192193

193194
context 'with invalid credentials' do
194195
it 'renders the new template' do
@@ -328,6 +329,8 @@
328329
end
329330

330331
context 'when the user does not exist yet' do
332+
before { CASino::User.destroy_all }
333+
331334
it 'generates exactly one user' do
332335
lambda do
333336
post :create, params
@@ -385,6 +388,20 @@
385388
end.should change(CASino::TicketGrantingTicket, :count).by(1)
386389
end
387390
end
391+
392+
context 'when the user is locked' do
393+
let(:locked_until) { 5.minutes.from_now }
394+
395+
it 'renders the new template' do
396+
post :create, params
397+
expect(response).to render_template(:new)
398+
end
399+
400+
it 'sets a flash to inform the user' do
401+
post :create, params
402+
expect(flash[:error]).to be_present
403+
end
404+
end
388405
end
389406
end
390407
end

0 commit comments

Comments
 (0)