Skip to content

Commit

Permalink
Merge pull request #4228 from consul/already_confirmed_user_emails
Browse files Browse the repository at this point in the history
Send informative email for already confirmed users
  • Loading branch information
taitus committed Apr 20, 2022
2 parents 96da315 + ad018c6 commit 7d1c672
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 11 deletions.
13 changes: 13 additions & 0 deletions app/controllers/users/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
class Users::ConfirmationsController < Devise::ConfirmationsController
# POST /resource/confirmation
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?

if successfully_sent?(resource)
Mailer.already_confirmed(resource).deliver_later unless resource.confirmation_required?
respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end

# new action, PATCH does not exist in the default Devise::ConfirmationsController
# PATCH /resource/confirmation
def update
Expand Down
9 changes: 9 additions & 0 deletions app/mailers/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def machine_learning_success(user)
mail(to: @email_to, subject: t("mailers.machine_learning_success.subject"))
end

def already_confirmed(user)
@email_to = user.email
@user = user

with_user(@user) do
mail(to: @email_to, subject: t("mailers.already_confirmed.subject"))
end
end

private

def with_user(user, &block)
Expand Down
17 changes: 17 additions & 0 deletions app/views/mailer/already_confirmed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<td style="padding-bottom: 20px; padding-left: 10px;">

<h1 style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;">
<%= t("mailers.already_confirmed.subject") %>
</h1>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;line-height: 24px;">
<%= t("mailers.already_confirmed.info") %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;line-height: 24px;">
<%= t("mailers.already_confirmed.new_password") %>
</p>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= link_to t("devise_views.shared.links.new_password"), new_password_url(@user), style: "color: #2895F1; text-decoration:none;" %>
</p>
</td>
4 changes: 2 additions & 2 deletions config/locales/en/devise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ en:
updated: "Password successfully updated"
confirmations:
confirmed: "Your account has been confirmed."
send_instructions: "In a few minutes you will receive an email containing instructions on how to reset your password."
send_paranoid_instructions: "If your email address is in our database, in a few minutes you will receive an email containing instructions on how to reset your password."
send_instructions: "In a few minutes you will receive an email containing instructions on how to confirm your email address."
send_paranoid_instructions: "If your email address exists in our database, in a few minutes you will receive an email with instructions on how to confirm your email address."
failure:
already_authenticated: "You are already signed in."
inactive: "Your account has not yet been activated."
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en/mailers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ en:
mailers:
title: "Open Government"
no_reply: "This message was sent from an email address that does not accept replies."
already_confirmed:
info: "We've received a request to send you instructions to confirm your account. However, your account is already confirmed, so there's no need to do so again."
new_password: "If you've forgotten your password, you can reset it at the following link:"
subject: Your account is already confirmed
comment:
hi: Hi
new_comment_by: There is a new comment from <strong>%{commenter}</strong>
Expand Down
4 changes: 2 additions & 2 deletions config/locales/es/devise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ es:
updated: "Contraseña actualizada con éxito"
confirmations:
confirmed: "Tu cuenta ha sido confirmada. Por favor autentifícate con tu red social o tu usuario y contraseña"
send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña."
send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña."
send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo confirmar tu cuenta."
send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos recibirás un correo electrónico en unos minutos con instrucciones sobre cómo confirmar tu cuenta."
failure:
already_authenticated: "Ya has iniciado sesión."
inactive: "Tu cuenta aún no ha sido activada."
Expand Down
4 changes: 4 additions & 0 deletions config/locales/es/mailers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ es:
mailers:
title: "Gobierno abierto"
no_reply: "Este mensaje se ha enviado desde una dirección de correo electrónico que no admite respuestas."
already_confirmed:
info: "Hemos recibido una solicitud para enviarte instrucciones para confirmar tu cuenta. Sin embargo, tu cuenta ya está confirmada, por lo que no es necesario volver a hacerlo."
new_password: "Si has olvidado tu contraseña, puedes restablecerla en el siguiente enlace:"
subject: Tu cuenta ya está confirmada
comment:
hi: Hola
new_comment_by: Hay un nuevo comentario de <strong>%{commenter}</strong> en
Expand Down
36 changes: 29 additions & 7 deletions spec/system/users_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@
end

scenario "Re-send confirmation instructions" do
create(:user, email: "manuela@consul.dev")
create(:user, email: "manuela@consul.dev", confirmed_at: nil)
ActionMailer::Base.deliveries.clear

visit "/"
click_link "Sign in"
Expand All @@ -594,22 +595,43 @@
fill_in "Email", with: "manuela@consul.dev"
click_button "Re-send instructions"

expect(page).to have_content "If your email address is in our database, in a few minutes you "\
"will receive an email containing instructions on how to reset "\
"your password."
expect(page).to have_content "If your email address exists in our database, in a few minutes you will "\
"receive an email with instructions on how to confirm your email address."
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.first.to).to eq(["manuela@consul.dev"])
expect(ActionMailer::Base.deliveries.first.subject).to eq("Confirmation instructions")
end

scenario "Re-send confirmation instructions with unexisting email" do
ActionMailer::Base.deliveries.clear
visit "/"
click_link "Sign in"
click_link "Haven't received instructions to activate your account?"

fill_in "Email", with: "fake@mail.dev"
click_button "Re-send instructions"

expect(page).to have_content "If your email address is in our database, in a few minutes you "\
"will receive an email containing instructions on how to reset "\
"your password."
expect(page).to have_content "If your email address exists in our database, in a few minutes you will "\
"receive an email with instructions on how to confirm your email address."
expect(ActionMailer::Base.deliveries.count).to eq(0)
end

scenario "Re-send confirmation instructions with already verified email" do
ActionMailer::Base.deliveries.clear

create(:user, email: "manuela@consul.dev")

visit new_user_session_path
click_link "Haven't received instructions to activate your account?"

fill_in "user_email", with: "manuela@consul.dev"
click_button "Re-send instructions"

expect(page).to have_content "If your email address exists in our database, in a few minutes you will "\
"receive an email with instructions on how to confirm your email address."
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.first.to).to eq(["manuela@consul.dev"])
expect(ActionMailer::Base.deliveries.first.subject).to eq("Your account is already confirmed")
end

scenario "Sign in, admin with password expired" do
Expand Down

0 comments on commit 7d1c672

Please sign in to comment.