Module: Devise::Models::Lockable

Includes:
Activatable
Defined in:
lib/devise/models/lockable.rb

Overview

Handles blocking a user access after a certain number of attempts. Lockable accepts two different strategies to unlock a user after it’s blocked: email and time. The former will send an email to the user when the lock happens, containing a link to unlock it’s account. The second will unlock the user automatically after some configured time (ie 2.hours). It’s also possible to setup lockable to use both email and time strategies.

Configuration:

maximum_attempts: how many attempts should be accepted before blocking the user.
unlock_strategy: unlock the user account by :time, :email or :both.
unlock_in: the time you want to lock the user after to lock happens. Only
           available when unlock_strategy is :time or :both.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



23
24
25
26
27
# File 'lib/devise/models/lockable.rb', line 23

def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

#access_locked?Boolean

Verifies whether a user is locked or not.

Returns:

  • (Boolean)


53
54
55
# File 'lib/devise/models/lockable.rb', line 53

def access_locked?
  locked_at && !lock_expired?
end

#active?Boolean

Overwrites active? from Devise::Models::Activatable for locking purposes by verifying whether an user is active to sign in or not based on locked?

Returns:

  • (Boolean)


69
70
71
# File 'lib/devise/models/lockable.rb', line 69

def active?
  super && !access_locked?
end

#inactive_messageObject

Overwrites invalid_message from Devise::Models::Authenticatable to define the correct reason for blocking the sign in.



75
76
77
# File 'lib/devise/models/lockable.rb', line 75

def inactive_message
  access_locked? ? :locked : super
end

#lock_access!Object

Lock an user setting it’s locked_at to actual time.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/devise/models/lockable.rb', line 30

def lock_access!
  return true if access_locked?
  self.locked_at = Time.now

  if self.class.unlock_strategy_enabled?(:email)
    generate_unlock_token
    send_unlock_instructions
  end

  save(false)
end

#resend_unlock_tokenObject

Resend the unlock instructions if the user is locked.



63
64
65
# File 'lib/devise/models/lockable.rb', line 63

def resend_unlock_token
  if_access_locked { send_unlock_instructions }
end

#send_unlock_instructionsObject

Send unlock instructions by email



58
59
60
# File 'lib/devise/models/lockable.rb', line 58

def send_unlock_instructions
  ::DeviseMailer.deliver_unlock_instructions(self)
end

#unlock_access!Object

Unlock an user by cleaning locket_at and failed_attempts.



43
44
45
46
47
48
49
50
# File 'lib/devise/models/lockable.rb', line 43

def unlock_access!
  if_access_locked do
    self.locked_at = nil
    self.failed_attempts = 0
    self.unlock_token = nil
    save(false)
  end
end

#valid_for_authentication?(attributes) ⇒ Boolean

Overwrites valid_for_authentication? from Devise::Models::Authenticatable for verifying whether an user is allowed to sign in or not. If the user is locked, it should never be allowed.

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
# File 'lib/devise/models/lockable.rb', line 82

def valid_for_authentication?(attributes)
  if result = super
    self.failed_attempts = 0
  else
    self.failed_attempts += 1
    lock_access! if failed_attempts > self.class.maximum_attempts
  end
  save(false) if changed?
  result
end