@@ -148,36 +148,17 @@ def transition_from_crypto_providers(value = nil)
148
148
module Callbacks
149
149
# Does the order of this array matter?
150
150
METHODS = %w[
151
- before_password_set
152
- after_password_set
153
- before_password_verification
154
- after_password_verification
151
+ password_set
152
+ password_verification
155
153
] . freeze
156
154
157
155
def self . included ( klass )
158
156
return if klass . crypted_password_field . nil?
159
- klass . define_callbacks ( *METHODS )
160
-
157
+ klass . send :extend , ActiveModel ::Callbacks
161
158
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 ]
167
160
end
168
161
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
181
162
end
182
163
183
164
# The methods related to the password field.
@@ -203,17 +184,17 @@ def password
203
184
# create new password salt as well as encrypt the password.
204
185
def password = ( pass )
205
186
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
210
197
end
211
- send (
212
- "#{ crypted_password_field } =" ,
213
- crypto_provider . encrypt ( *encrypt_arguments ( @password , false ) )
214
- )
215
- @password_changed = true
216
- after_password_set
217
198
end
218
199
219
200
# Accepts a raw password to determine if it is the correct password.
@@ -229,23 +210,23 @@ def valid_password?(
229
210
)
230
211
crypted = crypted_password_to_validate_against ( check_against_database )
231
212
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
243
228
end
244
- after_password_verification
245
- return true
246
229
end
247
-
248
- false
249
230
end
250
231
251
232
# Resets the password to a random friendly token.
0 commit comments