From 8959f8c736866133271fdfe29c071b6e04229506 Mon Sep 17 00:00:00 2001 From: Alexey Naumov Date: Mon, 9 Jul 2018 21:23:10 +0300 Subject: [PATCH] send activation email after the commit in transactional databases (#130) * send activation email after the commit in transactional databases * extract callback_name to separate method * fix typo in parameter * disable transactional fixtures to be able to test after_commit * Raise on failure when change_password! --- lib/sorcery/adapters/active_record_adapter.rb | 2 +- lib/sorcery/adapters/mongoid_adapter.rb | 10 +++++++++- lib/sorcery/model/submodules/reset_password.rb | 6 +++--- lib/sorcery/model/submodules/user_activation.rb | 2 +- spec/spec_helper.rb | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/sorcery/adapters/active_record_adapter.rb b/lib/sorcery/adapters/active_record_adapter.rb index d5a956ef..c778fa48 100644 --- a/lib/sorcery/adapters/active_record_adapter.rb +++ b/lib/sorcery/adapters/active_record_adapter.rb @@ -35,7 +35,7 @@ def define_field(name, type, options = {}) end def define_callback(time, event, method_name, options = {}) - @klass.send "#{time}_#{event}", method_name, options.slice(:if) + @klass.send "#{time}_#{event}", method_name, options.slice(:if, :on) end def find_by_oauth_credentials(provider, uid) diff --git a/lib/sorcery/adapters/mongoid_adapter.rb b/lib/sorcery/adapters/mongoid_adapter.rb index 9009c335..baace2da 100644 --- a/lib/sorcery/adapters/mongoid_adapter.rb +++ b/lib/sorcery/adapters/mongoid_adapter.rb @@ -33,7 +33,15 @@ def define_field(name, type, options={}) end def define_callback(time, event, method_name, options={}) - @klass.send "#{time}_#{event}", method_name, options.slice(:if) + @klass.send callback_name(time, event, options), method_name, options.slice(:if) + end + + def callback_name(time, event, options) + if event == :commit + options[:on] == :create ? "#{time}_create" : "#{time}_save" + else + "#{time}_#{event}" + end end def credential_regex(credential) diff --git a/lib/sorcery/model/submodules/reset_password.rb b/lib/sorcery/model/submodules/reset_password.rb index 41d68bcc..1c0a5f7f 100644 --- a/lib/sorcery/model/submodules/reset_password.rb +++ b/lib/sorcery/model/submodules/reset_password.rb @@ -107,14 +107,14 @@ def deliver_reset_password_instructions! end mail end - + # Increment access_count_to_reset_password_page attribute. # For example, access_count_to_reset_password_page attribute is over 1, which # means the user doesn't have a right to access. def increment_password_reset_page_access_counter sorcery_adapter.increment(self.sorcery_config.reset_password_page_access_count_attribute_name) end - + # Reset access_count_to_reset_password_page attribute into 0. # This is expected to be used after sending an instruction email. def reset_password_reset_page_access_counter @@ -126,7 +126,7 @@ def reset_password_reset_page_access_counter def change_password!(new_password) clear_reset_password_token send(:"#{sorcery_config.password_attribute_name}=", new_password) - sorcery_adapter.save + sorcery_adapter.save raise_on_failure: true end protected diff --git a/lib/sorcery/model/submodules/user_activation.rb b/lib/sorcery/model/submodules/user_activation.rb index 1c58bbbb..9d65e1d6 100644 --- a/lib/sorcery/model/submodules/user_activation.rb +++ b/lib/sorcery/model/submodules/user_activation.rb @@ -45,7 +45,7 @@ def self.included(base) # don't setup activation if no password supplied - this user is created automatically sorcery_adapter.define_callback :before, :create, :setup_activation, if: proc { |user| user.send(sorcery_config.password_attribute_name).present? } # don't send activation needed email if no crypted password created - this user is external (OAuth etc.) - sorcery_adapter.define_callback :after, :create, :send_activation_needed_email!, if: :send_activation_needed_email? + sorcery_adapter.define_callback :after, :commit, :send_activation_needed_email!, on: :create, if: :send_activation_needed_email? end base.sorcery_config.after_config << :validate_mailer_defined diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c8d9782f..6c033504 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,7 +29,7 @@ class TestMailer < ActionMailer::Base; end config.include RSpec::Rails::ControllerExampleGroup, file_path: /controller(.)*_spec.rb$/ config.mock_with :rspec - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false config.before(:suite) { setup_orm } config.after(:suite) { teardown_orm }