lib/devise/models/lockable.rb in devise-1.0.5 vs lib/devise/models/lockable.rb in devise-1.0.6

- old
+ new

@@ -26,13 +26,14 @@ end end # Lock an user setting it's locked_at to actual time. def lock_access! + return true if access_locked? self.locked_at = Time.now - if unlock_strategy_enabled?(:email) + if self.class.unlock_strategy_enabled?(:email) generate_unlock_token send_unlock_instructions end save(false) @@ -58,15 +59,11 @@ ::DeviseMailer.deliver_unlock_instructions(self) end # Resend the unlock instructions if the user is locked. def resend_unlock_token - if_access_locked do - generate_unlock_token unless unlock_token.present? - save(false) - send_unlock_instructions - end + if_access_locked { send_unlock_instructions } end # Overwrites active? from Devise::Models::Activatable for locking purposes # by verifying whether an user is active to sign in or not based on locked? def active? @@ -85,14 +82,11 @@ def valid_for_authentication?(attributes) if result = super self.failed_attempts = 0 else self.failed_attempts += 1 - if failed_attempts > self.class.maximum_attempts - lock_access! - return false - end + lock_access! if failed_attempts > self.class.maximum_attempts end save(false) if changed? result end @@ -103,11 +97,11 @@ self.unlock_token = Devise.friendly_token end # Tells if the lock is expired if :time unlock strategy is active def lock_expired? - if unlock_strategy_enabled?(:time) + if self.class.unlock_strategy_enabled?(:time) locked_at && locked_at < self.class.unlock_in.ago else false end end @@ -121,15 +115,10 @@ self.class.add_error_on(self, :email, :not_locked) false end end - # Is the unlock enabled for the given unlock strategy? - def unlock_strategy_enabled?(strategy) - [:both, strategy].include?(self.class.unlock_strategy) - end - module ClassMethods # Attempt to find a user by it's email. If a record is found, send new # unlock instructions to it. If not user is found, returns a new user # with an email not found error. # Options must contain the user email @@ -145,9 +134,14 @@ # Options must have the unlock_token def unlock_access_by_token(unlock_token) lockable = find_or_initialize_with_error_by(:unlock_token, unlock_token) lockable.unlock_access! unless lockable.new_record? lockable + end + + # Is the unlock enabled for the given unlock strategy? + def unlock_strategy_enabled?(strategy) + [:both, strategy].include?(self.unlock_strategy) end Devise::Models.config(self, :maximum_attempts, :unlock_strategy, :unlock_in) end end