2022-07-25 19:31:54 -04:00
|
|
|
defmodule MemexWeb.UserSettingsController do
|
|
|
|
use MemexWeb, :controller
|
|
|
|
import MemexWeb.Gettext
|
|
|
|
alias Memex.Accounts
|
2023-04-13 23:29:29 -04:00
|
|
|
alias MemexWeb.UserAuth
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
plug :assign_email_and_password_changesets
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
2023-04-13 23:29:29 -04:00
|
|
|
render(conn, :edit, page_title: gettext("settings"))
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
2022-05-05 20:55:59 -04:00
|
|
|
def update(%{assigns: %{current_user: user}} = conn, %{
|
|
|
|
"action" => "update_email",
|
|
|
|
"current_password" => password,
|
|
|
|
"user" => user_params
|
|
|
|
}) do
|
2021-03-11 21:12:55 -05:00
|
|
|
case Accounts.apply_user_email(user, password, user_params) do
|
|
|
|
{:ok, applied_user} ->
|
|
|
|
Accounts.deliver_update_email_instructions(
|
|
|
|
applied_user,
|
|
|
|
user.email,
|
2023-04-13 23:29:29 -04:00
|
|
|
fn token -> url(MemexWeb.Endpoint, ~p"/users/settings/confirm_email/#{token}") end
|
2021-03-11 21:12:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(
|
|
|
|
:info,
|
2022-02-25 21:53:04 -05:00
|
|
|
dgettext(
|
|
|
|
"prompts",
|
2023-02-04 17:22:06 -05:00
|
|
|
"a link to confirm your email change has been sent to the new address."
|
2022-02-25 21:53:04 -05:00
|
|
|
)
|
2021-03-11 21:12:55 -05:00
|
|
|
)
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/users/settings")
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2023-04-13 23:29:29 -04:00
|
|
|
conn |> render(:edit, email_changeset: changeset)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 20:55:59 -04:00
|
|
|
def update(%{assigns: %{current_user: user}} = conn, %{
|
|
|
|
"action" => "update_password",
|
|
|
|
"current_password" => password,
|
|
|
|
"user" => user_params
|
|
|
|
}) do
|
2021-03-11 21:12:55 -05:00
|
|
|
case Accounts.update_user_password(user, password, user_params) do
|
|
|
|
{:ok, user} ->
|
|
|
|
conn
|
2023-02-04 17:22:06 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "password updated successfully."))
|
2023-04-13 23:29:29 -04:00
|
|
|
|> put_session(:user_return_to, ~p"/users/settings")
|
2021-03-11 21:12:55 -05:00
|
|
|
|> UserAuth.log_in_user(user)
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
2023-04-13 23:29:29 -04:00
|
|
|
conn |> render(:edit, password_changeset: changeset)
|
2022-05-05 20:55:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(
|
|
|
|
%{assigns: %{current_user: user}} = conn,
|
|
|
|
%{"action" => "update_locale", "user" => %{"locale" => locale}}
|
|
|
|
) do
|
|
|
|
case Accounts.update_user_locale(user, locale) do
|
|
|
|
{:ok, _user} ->
|
|
|
|
conn
|
2023-01-22 23:52:08 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "language updated successfully."))
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/users/settings")
|
2022-05-05 20:55:59 -04:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2023-04-13 23:29:29 -04:00
|
|
|
conn |> render(:edit, locale_changeset: changeset)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 20:55:59 -04:00
|
|
|
def confirm_email(%{assigns: %{current_user: user}} = conn, %{"token" => token}) do
|
|
|
|
case Accounts.update_user_email(user, token) do
|
2021-03-11 21:12:55 -05:00
|
|
|
:ok ->
|
|
|
|
conn
|
2023-02-04 17:22:06 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "email changed successfully."))
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/users/settings")
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
:error ->
|
|
|
|
conn
|
2022-02-25 21:53:04 -05:00
|
|
|
|> put_flash(
|
|
|
|
:error,
|
2023-02-04 17:22:06 -05:00
|
|
|
dgettext("errors", "email change link is invalid or it has expired.")
|
2022-02-25 21:53:04 -05:00
|
|
|
)
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/users/settings")
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-25 21:53:04 -05:00
|
|
|
def delete(%{assigns: %{current_user: current_user}} = conn, %{"id" => user_id}) do
|
|
|
|
if user_id == current_user.id do
|
|
|
|
current_user |> Accounts.delete_user!(current_user)
|
|
|
|
|
|
|
|
conn
|
2023-02-04 17:22:06 -05:00
|
|
|
|> put_flash(:error, dgettext("prompts", "your account has been deleted"))
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/")
|
2022-02-25 21:53:04 -05:00
|
|
|
else
|
|
|
|
conn
|
2023-02-04 17:22:06 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "unable to delete user"))
|
2023-04-13 23:29:29 -04:00
|
|
|
|> redirect(to: ~p"/users/settings")
|
2022-02-25 21:53:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 20:55:59 -04:00
|
|
|
defp assign_email_and_password_changesets(%{assigns: %{current_user: user}} = conn, _opts) do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn
|
|
|
|
|> assign(:email_changeset, Accounts.change_user_email(user))
|
|
|
|
|> assign(:password_changeset, Accounts.change_user_password(user))
|
2022-05-05 20:55:59 -04:00
|
|
|
|> assign(:locale_changeset, Accounts.change_user_locale(user))
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|