FEATURE: send set password instructions after invite redemption

This commit is contained in:
Arpit Jalan 2014-10-10 21:17:52 +05:30
parent e8637344c3
commit 861f321263
5 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,17 @@
require_dependency 'email/sender'
module Jobs
# Asynchronously send an email
class InvitePasswordInstructionsEmail < Jobs::Base
def execute(args)
raise Discourse::InvalidParameters.new(:username) unless args[:username].present?
user = User.find_by_username_or_email(args[:username])
message = InviteMailer.send_password_instructions(user)
Email::Sender.new(message, :invite_password_instructions).send
end
end
end

View file

@ -42,4 +42,13 @@ class InviteMailer < ActionMailer::Base
end end
def send_password_instructions(user)
if user.present?
email_token = user.email_tokens.create(email: user.email)
build_email(user.email,
template: 'invite_password_instructions',
email_token: email_token.token)
end
end
end end

View file

@ -48,6 +48,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do
send_welcome_message send_welcome_message
approve_account_if_needed approve_account_if_needed
notify_invitee notify_invitee
send_password_instructions
end end
def invite_was_redeemed? def invite_was_redeemed?
@ -102,6 +103,12 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do
invited_user.approve(invite.invited_by_id, false) invited_user.approve(invite.invited_by_id, false)
end end
def send_password_instructions
if !SiteSetting.enable_sso && SiteSetting.enable_local_logins && !invited_user.has_password?
Jobs.enqueue(:invite_password_instructions_email, username: invited_user.username)
end
end
def notify_invitee def notify_invitee
invite.invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted], invite.invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted],
data: {display_username: invited_user.username}.to_json) data: {display_username: invited_user.username}.to_json)

View file

@ -1156,6 +1156,14 @@ en:
This invitation is from a trusted user, so you won't need to log in. This invitation is from a trusted user, so you won't need to log in.
invite_password_instructions:
subject_template: "Set password for your %{site_name} account"
text_body_template: |
Thanks for accepting your invitation to %{site_name} -- welcome!
To log in again, click the following link to choose a password:
%{base_url}/users/password-reset/%{email_token}
test_mailer: test_mailer:
subject_template: "[%{site_name}] Email Deliverability Test" subject_template: "[%{site_name}] Email Deliverability Test"
text_body_template: | text_body_template: |

View file

@ -161,6 +161,33 @@ describe Invite do
invite.redeem.should be_blank invite.redeem.should be_blank
end end
context 'enqueues a job to email "set password" instructions' do
it 'does not enqueue an email if sso is enabled' do
SiteSetting.stubs(:enable_sso).returns(true)
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
invite.redeem
end
it 'does not enqueue an email if local login is disabled' do
SiteSetting.stubs(:enable_local_logins).returns(false)
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
invite.redeem
end
it 'does not enqueue an email if the user has already set password' do
user = Fabricate(:user, email: invite.email, password_hash: "7af7805c9ee3697ed1a83d5e3cb5a3a431d140933a87fdcdc5a42aeef9337f81")
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
invite.redeem
end
it 'enqueues an email if all conditions are satisfied' do
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username))
invite.redeem
end
end
context "when inviting to groups" do context "when inviting to groups" do
it "add the user to the correct groups" do it "add the user to the correct groups" do
group = Fabricate(:group) group = Fabricate(:group)