2020-05-03 00:17:55 +02:00
|
|
|
defmodule PhilomenaWeb.Registration.NameController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users
|
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
|
|
|
plug :verify_authorized
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = Users.change_user(conn.assigns.current_user)
|
|
|
|
|
|
|
|
render(conn, "edit.html", title: "Editing Name", changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"user" => user_params}) do
|
|
|
|
case Users.update_name(conn.assigns.current_user, user_params) do
|
2020-10-21 20:44:20 +02:00
|
|
|
{:ok, user} ->
|
2020-05-03 00:17:55 +02:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Name successfully updated.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{user}")
|
2020-05-03 00:17:55 +02:00
|
|
|
|
2020-10-21 20:39:16 +02:00
|
|
|
{:error, changeset} ->
|
2020-05-03 00:17:55 +02:00
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :change_username, conn.assigns.current_user) do
|
|
|
|
true -> conn
|
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|