Hash :
8667cd15
Author :
Thomas de Grivel
Date :
2021-11-19T14:03: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
defmodule KmxgitWeb.PageController do
use KmxgitWeb, :controller
alias Kmxgit.Repo
alias Kmxgit.UserManager
alias Kmxgit.UserManager.{Guardian, User}
def index(conn, _params) do
if ! UserManager.admin_user_present? do
redirect(conn, to: Routes.page_path(conn, :new_admin))
else
conn
|> render(:index)
end
end
def new_admin(conn, _params) do
if ! UserManager.admin_user_present? do
changeset = UserManager.change_user(%User{})
conn
|> assign(:no_navbar_links, true)
|> render("new_admin.html", changeset: changeset,
action: Routes.page_path(conn, :new_admin))
else
redirect(conn, to: "/")
end
end
def new_admin_post(conn, params) do
if ! UserManager.admin_user_present? do
user_params = Map.merge(params["user"], %{"is_admin" => true})
Repo.transaction fn ->
case UserManager.admin_create_user(user_params) do
{:ok, user} ->
conn
|> Guardian.Plug.sign_in(user)
|> redirect(to: "/")
{:error, changeset} ->
IO.inspect(changeset)
conn
|> assign(:no_navbar_links, true)
|> assign(:changeset, changeset)
|> assign(:action, Routes.page_path(conn, :new_admin))
|> render("new_admin.html")
end
end
else
redirect(conn, to: "/")
end
end
end