Hash :
cde9b996
Author :
Thomas de Grivel
Date :
2021-12-09T12:15:43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
defmodule KmxgitWeb.UserController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.Repo
alias Kmxgit.UserManager
alias KmxgitWeb.ErrorView
def edit(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
changeset = UserManager.change_user(current_user)
email_changeset = UserManager.change_user_email(current_user)
password_changeset = UserManager.change_user_password(current_user)
conn
|> assign(:changeset, changeset)
|> assign(:email_changeset, email_changeset)
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> assign(:password_changeset, password_changeset)
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
case Repo.transaction(fn ->
case UserManager.update_user(current_user, params["user"]) do
{:ok, user} ->
if user.slug.slug != current_user.slug.slug do
case GitManager.rename_dir(current_user.slug.slug, user.slug.slug) do
:ok -> user
{:error, err} -> Repo.rollback(err)
end
else
user
end
{:error, changeset} -> Repo.rollback(changeset)
end
end) do
{:ok, user} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.slug_path(conn, :show, user.slug.slug))
{:error, changeset} ->
conn
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> assign(:changeset, changeset)
|> render("edit.html")
end
else
not_found(conn)
end
end
def delete(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
case Repo.transaction(fn ->
case UserManager.delete_user(current_user) do
{:ok, _} ->
case GitManager.delete_dir(current_user.slug.slug) do
:ok -> :ok
{:error, out} -> Repo.rollback(status: out)
end
{:error, e} -> Repo.rollback(e)
end
end) do
{:ok, _} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: "/")
{:error, changeset} ->
conn
|> assign(:changeset, changeset)
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> render("edit.html")
end
else
not_found(conn)
end
end
end