Skip to content

Commit 9b314fa

Browse files
committed
Update the way that callbacks are added to the model
Rebasing Tieg's work from #420
1 parent 2b52df5 commit 9b314fa

File tree

1 file changed

+29
-48
lines changed

1 file changed

+29
-48
lines changed

lib/authlogic/acts_as_authentic/password.rb

+29-48
Original file line numberDiff line numberDiff line change
@@ -148,36 +148,17 @@ def transition_from_crypto_providers(value = nil)
148148
module Callbacks
149149
# Does the order of this array matter?
150150
METHODS = %w[
151-
before_password_set
152-
after_password_set
153-
before_password_verification
154-
after_password_verification
151+
password_set
152+
password_verification
155153
].freeze
156154

157155
def self.included(klass)
158156
return if klass.crypted_password_field.nil?
159-
klass.define_callbacks(*METHODS)
160-
157+
klass.send :extend, ActiveModel::Callbacks
161158
METHODS.each do |method|
162-
klass.class_eval <<-EOS, __FILE__, __LINE__ + 1
163-
def self.#{method}(*methods, &block)
164-
set_callback :#{method}, *methods, &block
165-
end
166-
EOS
159+
klass.define_model_callbacks method, only: %i[before after]
167160
end
168161
end
169-
170-
# TODO: Ideally, once this module is included, the included copies of
171-
# the following methods would be private. This cannot be accomplished
172-
# by using calling `private` here in the module. Maybe we can set the
173-
# privacy inside `included`?
174-
METHODS.each do |method|
175-
class_eval <<-EOS, __FILE__, __LINE__ + 1
176-
def #{method}
177-
run_callbacks(:#{method}) { |result, object| result == false }
178-
end
179-
EOS
180-
end
181162
end
182163

183164
# The methods related to the password field.
@@ -203,17 +184,17 @@ def password
203184
# create new password salt as well as encrypt the password.
204185
def password=(pass)
205186
return if ignore_blank_passwords? && pass.blank?
206-
before_password_set
207-
@password = pass
208-
if password_salt_field
209-
send("#{password_salt_field}=", Authlogic::Random.friendly_token)
187+
run_callbacks :password_set do
188+
@password = pass
189+
if password_salt_field
190+
send("#{password_salt_field}=", Authlogic::Random.friendly_token)
191+
end
192+
send(
193+
"#{crypted_password_field}=",
194+
crypto_provider.encrypt(*encrypt_arguments(@password, false))
195+
)
196+
@password_changed = true
210197
end
211-
send(
212-
"#{crypted_password_field}=",
213-
crypto_provider.encrypt(*encrypt_arguments(@password, false))
214-
)
215-
@password_changed = true
216-
after_password_set
217198
end
218199

219200
# Accepts a raw password to determine if it is the correct password.
@@ -229,23 +210,23 @@ def valid_password?(
229210
)
230211
crypted = crypted_password_to_validate_against(check_against_database)
231212
return false if attempted_password.blank? || crypted.blank?
232-
before_password_verification
233-
234-
crypto_providers.each_with_index do |encryptor, index|
235-
next unless encryptor_matches?(
236-
crypted,
237-
encryptor,
238-
attempted_password,
239-
check_against_database
240-
)
241-
if transition_password?(index, encryptor, check_against_database)
242-
transition_password(attempted_password)
213+
run_callbacks :password_verification do
214+
crypto_providers.each_with_index.any? do |encryptor, index|
215+
if encryptor_matches?(
216+
crypted,
217+
encryptor,
218+
attempted_password,
219+
check_against_database
220+
)
221+
if transition_password?(index, encryptor, check_against_database)
222+
transition_password(attempted_password)
223+
end
224+
true
225+
else
226+
false
227+
end
243228
end
244-
after_password_verification
245-
return true
246229
end
247-
248-
false
249230
end
250231

251232
# Resets the password to a random friendly token.

0 commit comments

Comments
 (0)