Hash :
612276da
Author :
Thomas de Grivel
Date :
2022-07-10T16:29:02
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 170 171 172 173 174 175 176 177 178 179 180
defmodule KmxgitWeb.Admin.OrganisationController do
use KmxgitWeb, :controller
alias Kmxgit.IndexParams
alias Kmxgit.GitAuth
alias Kmxgit.GitManager
alias Kmxgit.OrganisationManager
alias Kmxgit.Repo
alias Kmxgit.SlugManager
alias KmxgitWeb.ErrorView
def index(conn, params) do
index_params = %IndexParams{}
|> KmxgitWeb.Admin.page_params(params["page"], params["per"])
|> KmxgitWeb.Admin.search_param(params["search"])
|> KmxgitWeb.Admin.sort_param(params["sort"])
pagination = OrganisationManager.list_organisations(index_params)
conn
|> assign(:index, index_params)
|> assign(:pagination, pagination)
|> assign(:search, params["search"])
|> assign(:search_action, Routes.admin_organisation_path(conn, :index, sort: params["sort"], search: params["search"]))
|> assign(:sort, params["sort"])
|> render("index.html")
end
def new(conn, _params) do
changeset = OrganisationManager.change_organisation
conn
|> assign(:action, Routes.admin_organisation_path(conn, :create))
|> assign(:changeset, changeset)
|> assign(:org, nil)
|> render("new.html")
end
def create(conn, params) do
org_params = params["organisation"]
case OrganisationManager.admin_create_organisation(org_params) do
{:ok, org} ->
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org))
{:error, changeset} ->
conn
|> assign(:action, Routes.admin_organisation_path(conn, :create))
|> assign(:changeset, changeset)
|> assign(:org, nil)
|> render("new.html")
end
end
def show(conn, params) do
org = OrganisationManager.get_organisation(params["id"])
if org do
org = org
|> OrganisationManager.put_disk_usage()
conn
|> assign(:org, org)
|> render("show.html")
else
not_found(conn)
end
end
def edit(conn, params) do
org = OrganisationManager.get_organisation(params["id"])
if org do
changeset = OrganisationManager.change_organisation(org)
conn
|> assign(:action, Routes.admin_organisation_path(conn, :update, org))
|> assign(:changeset, changeset)
|> assign(:org, org)
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
org = OrganisationManager.get_organisation(params["id"])
if org do
case Repo.transaction(fn ->
case OrganisationManager.update_organisation(org, params["organisation"]) do
{:ok, org1} ->
if org.slug_ != org1.slug_ do
case SlugManager.rename_slug(org.slug_, org1.slug_) do
{:ok, _slug} ->
case GitManager.rename_dir(org.slug_, org1.slug_) do
:ok ->
GitAuth.update()
org1
{:error, err} -> Repo.rollback(err)
end
{:error, err} -> Repo.rollback(err)
end
else
org1
end
{:error, changeset} -> Repo.rollback(changeset)
end
end) do
{:ok, org1} ->
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org1))
{:error, changeset} ->
conn
|> assign(:action, Routes.admin_organisation_path(conn, :update, org))
|> assign(:changeset, changeset)
|> render("edit.html")
end
else
not_found(conn)
end
end
def add_user(conn, params) do
org = OrganisationManager.get_organisation!(params["organisation_id"])
conn
|> assign(:action, Routes.admin_organisation__path(conn, :add_user_post, org))
|> assign(:org, org)
|> render("add_user.html")
end
def add_user_post(conn, params) do
login = params["organisation"]["login"]
org = OrganisationManager.get_organisation!(params["organisation_id"])
case OrganisationManager.add_user(org, login) do
{:ok, org1} ->
GitAuth.update()
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org1))
{:error, _} ->
conn
|> assign(:action, Routes.admin_organisation__path(conn, :add_user_post, org))
|> assign(:org, org)
|> render("add_user.html")
end
end
def remove_user(conn, params) do
org = OrganisationManager.get_organisation!(params["organisation_id"])
conn
|> assign(:action, Routes.admin_organisation__path(conn, :remove_user_post, org))
|> assign(:org, org)
|> render("remove_user.html")
end
def remove_user_post(conn, params) do
login = params["organisation"]["login"]
org = OrganisationManager.get_organisation!(params["organisation_id"])
case OrganisationManager.remove_user(org, login) do
{:ok, org} ->
GitAuth.update()
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org))
{:error, _} ->
conn
|> assign(:action, Routes.admin_organisation__path(conn, :remove_user_post, org))
|> assign(:org, org)
|> render("remove_user.html")
end
end
def delete(conn, params) do
org = OrganisationManager.get_organisation(params["id"])
if org do
case OrganisationManager.delete_organisation(org) do
{:ok, _} ->
GitAuth.update()
conn
|> redirect(to: Routes.admin_organisation_path(conn, :index))
{:error, _} ->
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org))
end
else
not_found(conn)
end
end
end