Hash :
04b5f55d
Author :
Thomas de Grivel
Date :
2021-11-28T14:21:53
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
defmodule KmxgitWeb.PageController do
use KmxgitWeb, :controller
alias Kmxgit.Repo
alias Kmxgit.RepositoryManager
alias Kmxgit.RepositoryManager.Repository
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} ->
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
def keys(conn, _params) do
k = UserManager.list_users
|> Enum.map(fn user -> User.ssh_keys_with_env(user) end)
|> Enum.join("\n")
conn
|> put_resp_content_type("text/text")
|> resp(200, k)
end
def auth(conn, _params) do
a = RepositoryManager.list_repositories
|> Enum.sort(fn a, b ->
Repository.full_slug(a) < Repository.full_slug(b)
end)
|> Enum.map(fn repo -> Repository.auth(repo) end)
|> Enum.join("\n")
conn
|> put_resp_content_type("text/text")
|> resp(200, a)
end
end