Hash :
8bfb8418
Author :
Thomas de Grivel
Date :
2021-12-28T21:56:04
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
defmodule KmxgitWeb.UserController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.Repo
alias Kmxgit.UserManager
alias Kmxgit.UserManager.User
alias KmxgitWeb.ErrorView
def edit(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == User.login(current_user) do
user = current_user
changeset = UserManager.change_user(user)
email_changeset = UserManager.change_user_email(user)
password_changeset = UserManager.change_user_password(user)
conn
|> assign(:changeset, changeset)
|> assign(:email_changeset, email_changeset)
|> assign(:page_title, gettext("Edit user %{login}", login: user.slug.slug))
|> assign(:password_changeset, password_changeset)
|> assign(:user, user)
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == User.login(current_user) do
user = current_user
case Repo.transaction(fn ->
case UserManager.update_user(user, params["user"]) do
{:ok, user} ->
if user.slug.slug != user.slug.slug do
case GitManager.rename_dir(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: user.slug.slug))
|> assign(:changeset, changeset)
|> assign(:user, user)
|> render("edit.html")
end
else
not_found(conn)
end
end
defp img_src_data(data, type) do
"data:#{type};base64,#{Base.encode64(data)}"
end
defp totp_enrolment_qrcode_src(user) do
UserManager.totp_enrolment_url(user)
|> QRCodeEx.encode()
|> QRCodeEx.svg()
|> img_src_data("image/svg+xml")
end
def totp(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == User.login(current_user) do
user = current_user
changeset = UserManager.change_user(user)
|> Ecto.Changeset.put_change(:otp_last, "")
totp_enrolment_qrcode_src = totp_enrolment_qrcode_src(user)
conn
|> assign(:changeset, changeset)
|> assign(:page_title, gettext("Enrol TOTP for user %{login}", login: User.login(user)))
|> assign(:totp_enrolment_qrcode_src, totp_enrolment_qrcode_src)
|> assign(:user, user)
|> render("totp.html")
else
not_found(conn)
end
end
def totp_update(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == User.login(current_user) do
user = current_user
IO.inspect(params)
case UserManager.update_user_totp(user, params["user"]) do
{:ok, user} ->
conn
|> put_flash(:info, "Enroled 2FA (TOTP) successfuly.")
|> redirect(to: Routes.slug_path(conn, :show, User.login(user)))
{:error, changeset} ->
totp_enrolment_qrcode_src = totp_enrolment_qrcode_src(user)
conn
|> assign(:changeset, changeset)
|> assign(:page_title, gettext("Enrol TOTP for user %{login}", login: User.login(user)))
|> assign(:totp_enrolment_qrcode_src, totp_enrolment_qrcode_src)
|> assign(:user, user)
|> render("totp.html")
end
else
not_found(conn)
end
end
def totp_delete(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == User.login(current_user) do
user = current_user
case UserManager.delete_user_totp(user) do
{:ok, user} ->
conn
|> put_flash(:info, "Removed 2FA (TOTP) successfuly.")
|> redirect(to: Routes.slug_path(conn, :show, User.login(user)))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> put_flash(:error, "Failed to remove 2FA (TOTP).")
|> redirect(to: Routes.user_path(conn, :edit, User.login(user)))
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