Module: Spree::AccountLockout

Extended by:
ActiveSupport::Concern
Included in:
AdminUser, Customer
Defined in:
app/models/concerns/spree/account_lockout.rb

Overview

Failed-login tracking and temporary account lockout, shared by the default auth models (Customer, AdminUser). Thresholds are configuration-driven via Spree::Config[:max_failed_login_attempts] and Spree::Config[:lockout_duration] (seconds). Enforcement lives in the authentication strategy — including this concern opts a model in, but nothing locks until the login flow records attempts.

Instance Method Summary collapse

Instance Method Details

#locked? ⇒ Boolean

Returns whether the account is currently within its lockout window.

Returns:

  • (Boolean) —

    whether the account is currently within its lockout window



15
16
17
# File 'app/models/concerns/spree/account_lockout.rb', line 15

def locked?
  locked_at.present? && locked_at > Spree::Config[:lockout_duration].seconds.ago
end

#record_failed_attempt! ⇒ void

This method returns an undefined value.

Records a failed login attempt, locking the account once the configured threshold is reached. Serialized with a row lock so concurrent failures read the latest counter — otherwise a stale in-memory value could cross the threshold without ever persisting locked_at.



24
25
26
27
28
29
30
# File 'app/models/concerns/spree/account_lockout.rb', line 24

def record_failed_attempt!
  with_lock do
    increment(:failed_attempts)
    self.locked_at = Time.current if failed_attempts >= Spree::Config[:max_failed_login_attempts]
    save!
  end
end

#reset_failed_attempts! ⇒ void

This method returns an undefined value.

Clears the failed-attempt counter and any lockout. Call on successful login.



34
35
36
# File 'app/models/concerns/spree/account_lockout.rb', line 34

def reset_failed_attempts!
  with_lock { update!(failed_attempts: 0, locked_at: nil) }
end