From b3bef61c8560624b4cab9e9dfc77eb70a1e67918 Mon Sep 17 00:00:00 2001 From: Robert Nowakowski Date: Tue, 17 Nov 2020 14:04:35 +0100 Subject: [PATCH 1/3] [SD-1004] Changes emails urls to contain current store url --- app/mailers/spree/user_mailer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mailers/spree/user_mailer.rb b/app/mailers/spree/user_mailer.rb index 917d025f3..4eefe0ab7 100644 --- a/app/mailers/spree/user_mailer.rb +++ b/app/mailers/spree/user_mailer.rb @@ -8,7 +8,7 @@ def reset_password_instructions(user, token, *_args) @edit_password_reset_url = spree.edit_spree_user_password_url(reset_password_token: token, host: @current_store.url) @user = user - mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions]) + mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions]), store_url: @current_store.url end def confirmation_instructions(user, token, _opts = {}) From 7b5d188fcf76ac35d28bffd830b50eb739decc8a Mon Sep 17 00:00:00 2001 From: Robert Nowakowski Date: Tue, 24 Nov 2020 16:36:27 +0100 Subject: [PATCH 2/3] Changes for confirmation instructions emails --- .../spree/user_confirmations_controller.rb | 12 +++++++++ .../spree/user_registrations_controller.rb | 3 +++ app/mailers/spree/user_mailer.rb | 6 +++-- app/models/spree/user.rb | 25 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/user_confirmations_controller.rb b/app/controllers/spree/user_confirmations_controller.rb index 7615638a1..e58ad95e1 100644 --- a/app/controllers/spree/user_confirmations_controller.rb +++ b/app/controllers/spree/user_confirmations_controller.rb @@ -8,6 +8,18 @@ class Spree::UserConfirmationsController < Devise::ConfirmationsController before_action :set_current_order + # POST /resource/confirmation + def create + self.resource = resource_class.send_confirmation_instructions(resource_params, current_store) + yield resource if block_given? + + if successfully_sent?(resource) + respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name)) + else + respond_with(resource) + end + end + # GET /resource/confirmation?confirmation_token=abcdef def show self.resource = resource_class.confirm_by_token(params[:confirmation_token]) diff --git a/app/controllers/spree/user_registrations_controller.rb b/app/controllers/spree/user_registrations_controller.rb index 9bc8c44d7..d6188a81e 100644 --- a/app/controllers/spree/user_registrations_controller.rb +++ b/app/controllers/spree/user_registrations_controller.rb @@ -23,6 +23,7 @@ def new # POST /resource/sign_up def create @user = build_resource(spree_user_params) + resource.skip_confirmation_notification! if Spree::Auth::Config[:confirmable] resource_saved = resource.save yield resource if block_given? if resource_saved @@ -30,10 +31,12 @@ def create set_flash_message :notice, :signed_up sign_up(resource_name, resource) session[:spree_user_signup] = true + resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable] redirect_to_checkout_or_account_path(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" expire_data_after_sign_in! + resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable] respond_with resource, location: after_inactive_sign_up_path_for(resource) end else diff --git a/app/mailers/spree/user_mailer.rb b/app/mailers/spree/user_mailer.rb index 4eefe0ab7..1b8883447 100644 --- a/app/mailers/spree/user_mailer.rb +++ b/app/mailers/spree/user_mailer.rb @@ -12,10 +12,12 @@ def reset_password_instructions(user, token, *_args) end def confirmation_instructions(user, token, _opts = {}) - @confirmation_url = spree.spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url) + current_store_id = _opts[:current_store_id] + @current_store = Spree::Store.find(current_store_id) || Spree::Store.current + @confirmation_url = spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url) @email = user.email - mail to: user.email, from: from_address, subject: Spree::Store.current.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions]) + mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions]), store_url: @current_store.url end end end diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 6c3993330..dc98944fe 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -27,6 +27,31 @@ def admin? has_spree_role?('admin') end + def self.send_confirmation_instructions(attributes = {}, current_store) + confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable + unless confirmable.try(:persisted?) + confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found) + end + confirmable.resend_confirmation_instructions(current_store) if confirmable.persisted? + confirmable + end + + def resend_confirmation_instructions(current_store) + pending_any_confirmation do + send_confirmation_instructions(current_store) + end + end + + def send_confirmation_instructions(current_store) + unless @raw_confirmation_token + generate_confirmation_token! + end + + opts = pending_reconfirmation? ? { to: unconfirmed_email } : {} + opts[:current_store_id] = current_store.id + send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts) + end + def self.send_reset_password_instructions(attributes={}, current_store) recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found) recoverable.send_reset_password_instructions(current_store) if recoverable.persisted? From 66250fb46e0bba58bcdc243e4b0dc6a69b2cbe18 Mon Sep 17 00:00:00 2001 From: Robert Nowakowski Date: Wed, 25 Nov 2020 13:57:26 +0100 Subject: [PATCH 3/3] Adds and changes tests --- spec/features/confirmation_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/confirmation_spec.rb b/spec/features/confirmation_spec.rb index ce9e95d16..41dbd202a 100644 --- a/spec/features/confirmation_spec.rb +++ b/spec/features/confirmation_spec.rb @@ -3,7 +3,7 @@ RSpec.feature 'Confirmation', type: :feature, reload_user: true do before do set_confirmable_option(true) - Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true)) + expect(Spree::UserMailer).to receive(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true)) end after(:each) { set_confirmable_option(false) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0ef22b2f2..e16727b10 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -68,7 +68,7 @@ describe "confirmable", reload_user: true do it "is confirmable if the confirmable option is enabled" do set_confirmable_option(true) - Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true)) + Spree::UserMailer.stub(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true)) expect(Spree.user_class.devise_modules).to include(:confirmable) set_confirmable_option(false) end