Permalink
Browse files

Clean up lockable and class methods API.

  • Loading branch information...
josevalim committed Mar 10, 2010
1 parent cd17099 commit ac742e32710199fdd7cb81f50895de8cbb316be0
@@ -21,7 +21,7 @@ def create
# GET /resource/confirmation?confirmation_token=abcdef
def show
- self.resource = resource_class.confirm!(:confirmation_token => params[:confirmation_token])
+ self.resource = resource_class.confirm_by_token(params[:confirmation_token])
if resource.errors.empty?
set_flash_message :notice, :confirmed
@@ -30,7 +30,7 @@ def edit
# PUT /resource/password
def update
- self.resource = resource_class.reset_password!(params[resource_name])
+ self.resource = resource_class.reset_password_by_token(params[resource_name])
if resource.errors.empty?
set_flash_message :notice, :updated
@@ -21,7 +21,7 @@ def create
# GET /resource/unlock?unlock_token=abcdef
def show
- self.resource = resource_class.unlock!(:unlock_token => params[:unlock_token])
+ self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
if resource.errors.empty?
set_flash_message :notice, :unlocked
@@ -60,7 +60,7 @@ def send_confirmation_instructions
# Remove confirmation date and send confirmation instructions, to ensure
# after sending these instructions the user won't be able to sign in without
# confirming it's account
- def resend_confirmation!
+ def resend_confirmation_token
unless_confirmed do
generate_confirmation_token
save(:validate => false)
@@ -78,11 +78,7 @@ def active?
# The message to be shown if the account is inactive.
def inactive_message
- if !confirmed?
- :unconfirmed
- else
- super
- end
+ !confirmed? ? :unconfirmed : super
end
# If you don't want confirmation to be sent on create, neither a code
@@ -148,16 +144,16 @@ module ClassMethods
# Options must contain the user email
def send_confirmation_instructions(attributes={})
confirmable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
- confirmable.resend_confirmation! unless confirmable.new_record?
+ confirmable.resend_confirmation_token unless confirmable.new_record?
confirmable
end
# Find a user by it's confirmation token and try to confirm it.
# If no user is found, returns a new user with an error.
# If the user is already confirmed, create an error for the user
# Options must have the confirmation_token
- def confirm!(attributes={})
- confirmable = find_or_initialize_with_error_by(:confirmation_token, attributes[:confirmation_token])
+ def confirm_by_token(confirmation_token)
+ confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
confirmable.confirm! unless confirmable.new_record?
confirmable
end
@@ -22,23 +22,20 @@ module Lockable
include Devise::Models::Activatable
# Lock an user setting it's locked_at to actual time.
- def lock
+ def lock_access!
self.locked_at = Time.now
+
if unlock_strategy_enabled?(:email)
generate_unlock_token
send_unlock_instructions
end
- end
- # Lock an user also saving the record.
- def lock!
- lock
save(:validate => false)
end
# Unlock an user by cleaning locket_at and failed_attempts.
- def unlock!
- if_locked do
+ def unlock_access!
+ if_access_locked do
self.locked_at = nil
self.failed_attempts = 0
self.unlock_token = nil
@@ -47,7 +44,7 @@ def unlock!
end
# Verifies whether a user is locked or not.
- def locked?
+ def access_locked?
locked_at && !lock_expired?
end
@@ -57,8 +54,8 @@ def send_unlock_instructions
end
# Resend the unlock instructions if the user is locked.
- def resend_unlock!
- if_locked do
+ def resend_unlock_token
+ if_access_locked do
generate_unlock_token unless unlock_token.present?
save(:validate => false)
send_unlock_instructions
@@ -68,17 +65,13 @@ def resend_unlock!
# 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?
- super && !locked?
+ super && !access_locked?
end
# Overwrites invalid_message from Devise::Models::Authenticatable to define
# the correct reason for blocking the sign in.
def inactive_message
- if locked?
- :locked
- else
- super
- end
+ access_locked? ? :locked : super
end
# Overwrites valid_for_authentication? from Devise::Models::Authenticatable
@@ -89,7 +82,10 @@ def valid_for_authentication?(attributes)
self.failed_attempts = 0
else
self.failed_attempts += 1
- lock if failed_attempts > self.class.maximum_attempts
+ if failed_attempts > self.class.maximum_attempts
+ lock_access!
+ return false
+ end
end
save(:validate => false) if changed?
result
@@ -113,8 +109,8 @@ def lock_expired?
# Checks whether the record is locked or not, yielding to the block
# if it's locked, otherwise adds an error to email.
- def if_locked
- if locked?
+ def if_access_locked
+ if access_locked?
yield
else
self.errors.add(:email, :not_locked)
@@ -134,17 +130,17 @@ module ClassMethods
# Options must contain the user email
def send_unlock_instructions(attributes={})
lockable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
- lockable.resend_unlock! unless lockable.new_record?
+ lockable.resend_unlock_token unless lockable.new_record?
lockable
end
# Find a user by it's unlock token and try to unlock it.
# If no user is found, returns a new user with an error.
# If the user is not locked, creates an error for the user
# Options must have the unlock_token
- def unlock!(attributes={})
- lockable = find_or_initialize_with_error_by(:unlock_token, attributes[:unlock_token])
- lockable.unlock! unless lockable.new_record?
+ 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
@@ -65,7 +65,7 @@ def send_reset_password_instructions(attributes={})
# try saving the record. If not user is found, returns a new user
# containing an error in reset_password_token attribute.
# Attributes must contain reset_password_token, password and confirmation
- def reset_password!(attributes={})
+ def reset_password_by_token(attributes={})
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) unless recoverable.new_record?
recoverable
@@ -47,14 +47,14 @@ def visit_user_unlock_with_token(unlock_token)
test "locked user should be able to unlock account" do
user = create_user(:locked => true)
- assert user.locked?
+ assert user.access_locked?
visit_user_unlock_with_token(user.unlock_token)
assert_template 'home/index'
assert_contain 'Your account was successfully unlocked.'
- assert_not user.reload.locked?
+ assert_not user.reload.access_locked?
end
test "sign in user automatically after unlocking it's account" do
@@ -10,7 +10,7 @@ def setup
def user
@user ||= begin
user = create_user
- user.lock!
+ user.lock_access!
user
end
end
@@ -15,7 +15,7 @@ def setup
user = create_user
3.times do
token = user.confirmation_token
- user.resend_confirmation!
+ user.resend_confirmation_token
assert_not_equal token, user.confirmation_token
end
end
@@ -62,19 +62,19 @@ def setup
test 'should find and confirm an user automatically' do
user = create_user
- confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
assert_equal confirmed_user, user
assert user.reload.confirmed?
end
test 'should return a new record with errors when a invalid token is given' do
- confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
assert confirmed_user.new_record?
assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
end
test 'should return a new record with errors when a blank token is given' do
- confirmed_user = User.confirm!(:confirmation_token => '')
+ confirmed_user = User.confirm_by_token('')
assert confirmed_user.new_record?
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
end
@@ -83,7 +83,7 @@ def setup
user = create_user
user.confirmed_at = Time.now
user.save
- confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
assert confirmed_user.confirmed?
assert_equal "was already confirmed", confirmed_user.errors[:email].join
end
@@ -173,7 +173,7 @@ def setup
test 'should not be able to send instructions if the user is already confirmed' do
user = create_user
user.confirm!
- assert_not user.resend_confirmation!
+ assert_not user.resend_confirmation_token
assert user.confirmed?
assert_equal 'was already confirmed', user.errors[:email].join
end
Oops, something went wrong.

0 comments on commit ac742e3

Please sign in to comment.