Hash :
c5b0aca3
Author :
Thomas de Grivel
Date :
2021-12-09T11:24:45
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
defmodule KmxgitWeb.Admin.UserController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.UserManager
alias KmxgitWeb.ErrorView
def index(conn, _params) do
users = UserManager.list_users
conn
|> assign(:page_title, gettext("Users"))
|> assign(:users, users)
|> render("index.html")
end
def show(conn, params) do
user = UserManager.get_user(params["id"])
if user do
conn
|> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
|> assign(:user, user)
|> render("show.html")
else
not_found(conn)
end
end
def edit(conn, params) do
user = UserManager.get_user(params["id"])
if user do
changeset = UserManager.change_user(user)
conn
|> assign(:changeset, changeset)
|> assign(:user, user)
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
user = UserManager.get_user(params["id"])
if user do
case UserManager.admin_update_user(user, params["user"]) do
{:ok, user1} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_user_path(conn, :show, user1))
{:error, changeset} ->
conn
|> assign(:changeset, changeset)
|> assign(:user, user)
|> render("edit.html")
end
else
not_found(conn)
end
end
def edit_password(conn, params) do
user = UserManager.get_user(params["user_id"])
if user do
changeset = UserManager.change_user(user)
conn
|> assign(:changeset, changeset)
|> assign(:user, user)
|> render("edit_password.html")
else
not_found(conn)
end
end
def update_password(conn, params) do
user = UserManager.get_user(params["user_id"])
if user do
case UserManager.admin_update_user_password(user, params["user"]) do
{:ok, user1} ->
conn
|> redirect(to: Routes.admin_user_path(conn, :show, user1))
{:error, changeset} ->
conn
|> assign(:changeset, changeset)
|> assign(:user, user)
|> render("edit_password.html")
end
else
not_found(conn)
end
end
def delete(conn, params) do
if user = UserManager.get_user(params["id"]) do
{:ok, _} = UserManager.delete_user(user)
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_user_path(conn, :index))
else
not_found(conn)
end
end
end