Skip to content

Commit fb8c439

Browse files
authored
Merge pull request #1353 from operable/peck/password-reset-graceful-fail
fail more gracefully when smtp isn't configured properly
2 parents 5fcbb2f + ac9a7a2 commit fb8c439

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

lib/cog/repository/groups.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ defmodule Cog.Repository.Groups do
1919
alias Cog.Models.Role
2020
import Ecto.Query, only: [from: 2]
2121

22-
@preloads [:users, :roles]
22+
@preloads [[users: from(u in User, order_by: u.username)],
23+
[roles: from(r in Role, order_by: r.name)]]
2324

2425
@doc """
2526
Creates a new user group given a map of attributes

lib/cog/repository/users.ex

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,36 @@ defmodule Cog.Repository.Users do
165165
password_reset = PasswordReset.changeset(%PasswordReset{}, %{user_id: user_id})
166166
|> Repo.insert!
167167

168-
Cog.Email.reset_password(email_address, password_reset.id)
169-
|> Cog.Mailer.deliver_later
170-
171-
password_reset
168+
try do
169+
Cog.Email.reset_password(email_address, password_reset.id)
170+
|> Cog.Mailer.deliver_later()
171+
172+
password_reset
173+
rescue
174+
# Raised when bamboo is not properly configured
175+
# We try to be helpful and give a more informative error message,
176+
# but since we just get a generic ArgumentError back we include
177+
# the original error just in case there is another issue.
178+
error in ArgumentError ->
179+
msg = """
180+
SMTP may not be configured. You must set 'COG_SMTP_SERVER', \
181+
'COG_SMTP_PORT', 'COG_SMTP_USERNAME', 'COG_SMTP_PASSWORD' and \
182+
'COG_EMAIL_FROM' to enable email support. See the documentation \
183+
for 'Configuring Password Resets' for more information.
184+
Original error: #{inspect error}
185+
"""
186+
Repo.rollback({:not_configured, msg})
187+
# Raised when the from address is not set
188+
Bamboo.EmptyFromAddressError ->
189+
msg = """
190+
You must set 'COG_EMAIL_FROM' to enable email support. \
191+
See the documentation for 'Configuring Password Resets' \
192+
for more information.
193+
"""
194+
Repo.rollback({:not_configured, msg})
195+
error ->
196+
Repo.rollback(error)
197+
end
172198
end)
173199
end
174200

web/controllers/v1/password_reset_controller.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ defmodule Cog.V1.PasswordResetController do
1515
# We don't want folks spamming this endpoint to find valid email
1616
# addresses.
1717
send_resp(conn, :no_content, "")
18+
{:error, {:not_configured, error}} ->
19+
Logger.warn("Email support has not been properly configured: #{inspect error}")
20+
conn
21+
|> put_status(:internal_server_error)
22+
|> json(%{errors: ["Password resets have been disabled or are not properly configured."]})
1823
{:error, error} ->
1924
Logger.warn("Failed to generate password reset: #{inspect error}")
2025
send_resp(conn, :internal_server_error, "")
@@ -28,7 +33,7 @@ defmodule Cog.V1.PasswordResetController do
2833
{:error, :not_found} ->
2934
conn
3035
|> put_status(:not_found)
31-
|> json(%{errors: "Invalid password reset token."})
36+
|> json(%{errors: ["Invalid password reset token."]})
3237
{:error, changeset} ->
3338
conn
3439
|> put_status(:unprocessable_entity)

0 commit comments

Comments
 (0)