Hash :
503f5be6
Author :
Thomas de Grivel
Date :
2021-12-15T09:11:03
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
defmodule KmxgitWeb.Admin.UserController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.RepositoryManager
alias Kmxgit.UserManager
alias Kmxgit.UserManager.User
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 new(conn, _params) do
changeset = UserManager.change_user()
conn
|> assign(:action, Routes.admin_user_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
def create(conn, params) do
pw = :crypto.strong_rand_bytes(16) |> Base.url_encode64()
pw = "Az0!#{pw}"
user_params = Map.merge(params["user"], %{"password" => pw})
case UserManager.admin_create_user(user_params) do
{:ok, user} ->
conn
|> redirect(to: Routes.admin_user_path(conn, :show, user))
{:error, changeset} ->
IO.inspect changeset
conn
|> assign(:action, Routes.admin_user_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
end
def show(conn, params) do
user = UserManager.get_user(params["id"])
if user do
owned_repos = User.owned_repositories(user)
contributor_repos = RepositoryManager.list_contributor_repositories(user)
repos = owned_repos ++ contributor_repos
conn
|> assign(:page_title, gettext("User %{login}", login: User.login(user)))
|> assign(:repos, repos)
|> 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} ->
IO.inspect(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