2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Email.User do
|
2018-07-04 17:16:02 +02:00
|
|
|
@moduledoc """
|
2019-09-07 23:58:53 +02:00
|
|
|
Handles emails sent to users.
|
2018-07-04 17:16:02 +02:00
|
|
|
"""
|
2018-07-04 14:29:17 +02:00
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2019-09-07 23:58:53 +02:00
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
import Mobilizon.Web.Gettext, only: [gettext: 2]
|
2018-07-04 14:29:17 +02:00
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.{Config, Crypto, Users}
|
|
|
|
alias Mobilizon.Storage.Repo
|
2019-09-07 23:58:53 +02:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2021-07-27 19:47:54 +02:00
|
|
|
alias Mobilizon.Web.Email
|
2019-09-17 02:45:32 +02:00
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
require Logger
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
@spec confirmation_email(User.t(), String.t()) :: Swoosh.Email.t()
|
2019-09-07 23:58:53 +02:00
|
|
|
def confirmation_email(
|
|
|
|
%User{email: email, confirmation_token: confirmation_token},
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-07 23:58:53 +02:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
2019-09-23 19:33:58 +02:00
|
|
|
"Instructions to confirm your Mobilizon account on %{instance}",
|
|
|
|
instance: Config.instance_name()
|
2019-09-07 23:58:53 +02:00
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:registration_confirmation, %{
|
|
|
|
locale: locale,
|
|
|
|
token: confirmation_token,
|
|
|
|
subject: subject,
|
|
|
|
offer_unsupscription: false
|
|
|
|
})
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
@spec reset_password_email(User.t(), String.t()) :: Swoosh.Email.t()
|
2019-09-07 23:58:53 +02:00
|
|
|
def reset_password_email(
|
|
|
|
%User{email: email, reset_password_token: reset_password_token},
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Gettext.put_locale(locale)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2019-09-07 23:58:53 +02:00
|
|
|
subject =
|
2018-07-27 10:45:35 +02:00
|
|
|
gettext(
|
2019-09-23 19:33:58 +02:00
|
|
|
"Instructions to reset your password on %{instance}",
|
|
|
|
instance: Config.instance_name()
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2019-09-07 23:58:53 +02:00
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:password_reset, %{
|
|
|
|
locale: locale,
|
|
|
|
token: reset_password_token,
|
|
|
|
subject: subject,
|
|
|
|
offer_unsupscription: false
|
|
|
|
})
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
@spec check_confirmation_token(String.t()) ::
|
|
|
|
{:ok, User.t()} | {:error, :invalid_token | Ecto.Changeset.t()}
|
2020-01-23 00:55:07 +01:00
|
|
|
def check_confirmation_token(token) when is_binary(token) do
|
2021-09-10 11:35:32 +02:00
|
|
|
case Users.get_user_by_activation_token(token) do
|
|
|
|
%User{} = user ->
|
|
|
|
case Users.update_user(user, %{
|
|
|
|
confirmed_at: DateTime.utc_now() |> DateTime.truncate(:second),
|
|
|
|
confirmation_sent_at: nil,
|
|
|
|
confirmation_token: nil,
|
|
|
|
email: user.unconfirmed_email || user.email
|
|
|
|
}) do
|
|
|
|
{:ok, %User{} = user} ->
|
|
|
|
Logger.info("User #{user.email} has been confirmed")
|
|
|
|
{:ok, user}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
2020-01-23 00:55:07 +01:00
|
|
|
{:error, :invalid_token}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def resend_confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
with :ok <- we_can_send_email(user, :confirmation_sent_at),
|
|
|
|
{:ok, user} <-
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmation_sent_at" => DateTime.utc_now() |> DateTime.truncate(:second)
|
2022-04-05 12:16:22 +02:00
|
|
|
}) do
|
|
|
|
send_confirmation_email(user, locale)
|
2020-01-23 00:55:07 +01:00
|
|
|
Logger.info("Sent confirmation email again to #{user.email}")
|
|
|
|
{:ok, user.email}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
@spec send_confirmation_email(User.t(), String.t()) :: Swoosh.Email.t()
|
2020-01-23 00:55:07 +01:00
|
|
|
def send_confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
user
|
|
|
|
|> Email.User.confirmation_email(locale)
|
2022-04-05 12:16:22 +02:00
|
|
|
|> Email.Mailer.send_email()
|
2020-01-23 00:55:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Check that the provided token is correct and update provided password
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec check_reset_password_token(String.t(), String.t()) ::
|
2021-09-10 11:35:32 +02:00
|
|
|
{:ok, User.t()} | {:error, :user_not_found | Ecto.Changeset.t()}
|
2020-01-23 00:55:07 +01:00
|
|
|
def check_reset_password_token(password, token) do
|
2021-09-10 11:35:32 +02:00
|
|
|
case Users.get_user_by_reset_password_token(token) do
|
|
|
|
%User{} = user ->
|
|
|
|
user
|
|
|
|
|> User.password_reset_changeset(%{
|
|
|
|
"password" => password,
|
|
|
|
"reset_password_sent_at" => nil,
|
|
|
|
"reset_password_token" => nil
|
|
|
|
})
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :user_not_found}
|
2020-01-23 00:55:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send the email reset password, if it's not too soon since the last send
|
|
|
|
"""
|
|
|
|
@spec send_password_reset_email(User.t(), String.t()) :: tuple
|
|
|
|
def send_password_reset_email(%User{} = user, locale \\ "en") do
|
|
|
|
with :ok <- we_can_send_email(user, :reset_password_sent_at),
|
|
|
|
{:ok, %User{} = user_updated} <-
|
|
|
|
Repo.update(
|
|
|
|
User.send_password_reset_changeset(user, %{
|
|
|
|
"reset_password_token" => Crypto.random_string(30),
|
|
|
|
"reset_password_sent_at" => DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
})
|
2022-04-05 12:16:22 +02:00
|
|
|
) do
|
|
|
|
user_updated
|
|
|
|
|> Email.User.reset_password_email(locale)
|
|
|
|
|> Email.Mailer.send_email()
|
|
|
|
|
|
|
|
{:ok, user.email}
|
2020-01-23 00:55:07 +01:00
|
|
|
else
|
|
|
|
{:error, reason} -> {:error, reason}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-05 15:29:06 +02:00
|
|
|
def send_email_reset_old_email(%User{
|
|
|
|
locale: user_locale,
|
|
|
|
email: email,
|
|
|
|
unconfirmed_email: unconfirmed_email
|
|
|
|
}) do
|
2020-02-13 15:48:12 +01:00
|
|
|
Gettext.put_locale(user_locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Mobilizon on %{instance}: email changed",
|
|
|
|
instance: Config.instance_name()
|
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:email_changed_old, %{
|
|
|
|
locale: user_locale,
|
|
|
|
new_email: unconfirmed_email,
|
|
|
|
subject: subject,
|
|
|
|
offer_unsupscription: false
|
|
|
|
})
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
|
2021-10-05 15:29:06 +02:00
|
|
|
def send_email_reset_new_email(%User{
|
|
|
|
locale: user_locale,
|
|
|
|
unconfirmed_email: unconfirmed_email,
|
|
|
|
confirmation_token: confirmation_token
|
|
|
|
}) do
|
2020-02-13 15:48:12 +01:00
|
|
|
Gettext.put_locale(user_locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Mobilizon on %{instance}: confirm your email address",
|
2021-09-30 09:34:39 +02:00
|
|
|
instance: Config.instance_name()
|
2020-02-13 15:48:12 +01:00
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: unconfirmed_email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:email_changed_new, %{
|
|
|
|
locale: user_locale,
|
|
|
|
token: confirmation_token,
|
|
|
|
subject: subject,
|
|
|
|
offer_unsupscription: false
|
|
|
|
})
|
2020-02-13 15:48:12 +01:00
|
|
|
end
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
@spec we_can_send_email(User.t(), atom) :: :ok | {:error, :email_too_soon}
|
|
|
|
defp we_can_send_email(%User{} = user, key) do
|
|
|
|
case Map.get(user, key) do
|
|
|
|
nil ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
_ ->
|
2021-09-24 16:46:42 +02:00
|
|
|
case DateTime.compare(
|
|
|
|
DateTime.add(Map.get(user, key), 3600),
|
2020-01-23 21:59:50 +01:00
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
) do
|
2021-09-24 16:46:42 +02:00
|
|
|
:lt ->
|
2020-01-23 00:55:07 +01:00
|
|
|
:ok
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
_ ->
|
2020-01-23 00:55:07 +01:00
|
|
|
{:error, :email_too_soon}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|