From dbbc2ddb1cca7a1d99cf029c444a91539e20dd53 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Fri, 14 Jul 2017 12:22:57 +0200 Subject: [PATCH] Add confirmable email to user model --- app/models/spree/user_decorator.rb | 17 ++++++++++++++++- .../20170710145821_add_confirmable_to_user.rb | 13 +++++++++++++ db/schema.rb | 5 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20170710145821_add_confirmable_to_user.rb diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index ace0c9e165..415f5aca88 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -19,10 +19,17 @@ Spree.user_class.class_eval do accepts_nested_attributes_for :ship_address attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit, :locale, :bill_address_attributes, :ship_address_attributes - after_create :send_signup_confirmation validate :limit_owned_enterprises + # We use the same options as Spree and add :confirmable + devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable, + :rememberable, :trackable, :validatable, :confirmable, :encryptable, + :reconfirmable => true, confirmation_keys: [ :id, :email ], :encryptor => 'authlogic_sha512' + handle_asynchronously :send_confirmation_instructions + # TODO: Later versions of devise have a dedicated after_confirmation callback, so use that + after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? } + def known_users if admin? Spree::User.scoped @@ -46,6 +53,14 @@ Spree.user_class.class_eval do customers.find_by_enterprise_id(enterprise) end + def welcome_after_confirm + # Send welcome email if we are confirming an user's email + # Note: this callback only runs on email confirmation + if confirmed? && unconfirmed_email.nil? && !unconfirmed_email_changed? + send_signup_confirmation + end + end + def send_signup_confirmation Delayed::Job.enqueue ConfirmSignupJob.new(id) end diff --git a/db/migrate/20170710145821_add_confirmable_to_user.rb b/db/migrate/20170710145821_add_confirmable_to_user.rb new file mode 100644 index 0000000000..69d5788df8 --- /dev/null +++ b/db/migrate/20170710145821_add_confirmable_to_user.rb @@ -0,0 +1,13 @@ +class AddConfirmableToUser < ActiveRecord::Migration + def up + add_column :spree_users, :confirmation_token, :string + add_column :spree_users, :confirmed_at, :datetime + add_column :spree_users, :confirmation_sent_at, :datetime + add_column :spree_users, :unconfirmed_email, :string + add_index :spree_users, :confirmation_token, :unique => true + end + + def down + remove_columns :spree_users, :confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email + end +end diff --git a/db/schema.rb b/db/schema.rb index 061c97e920..16b54e9ae5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1004,8 +1004,13 @@ ActiveRecord::Schema.define(:version => 20170921065259) do t.string "api_key", :limit => 40 t.integer "enterprise_limit", :default => 1, :null => false t.string "locale", :limit => 5 + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "unconfirmed_email" end + add_index "spree_users", ["confirmation_token"], :name => "index_spree_users_on_confirmation_token", :unique => true add_index "spree_users", ["email"], :name => "email_idx_unique", :unique => true add_index "spree_users", ["persistence_token"], :name => "index_users_on_persistence_token"