Commit edf1fbc96f1efb9b9acafa998b5b4c4a4361bf34

Thomas de Grivel 2021-11-29T16:10:28

/etc/git/auth.conf

diff --git a/bin/update_auth b/bin/update_auth
new file mode 100755
index 0000000..8676ea4
--- /dev/null
+++ b/bin/update_auth
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -e
+cd /etc/git
+ftp -o auth.conf https://git.kmx.io/_etc/git/auth.conf
diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index 57e1923..ba0d695 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -78,6 +78,14 @@ defmodule Kmxgit.GitManager do
     end
   end
 
+  def update_auth do
+    {out, status} = System.cmd("./bin/update_auth", [], stderr_to_stdout: true)
+    case status do
+      0 -> :ok
+      _ -> {:error, out}
+    end
+  end
+
   def rename(from, to) do
     dir_from = git_dir(from)
     dir_to = git_dir(to)
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 2616b2c..5830a12 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -79,10 +79,28 @@ defmodule KmxgitWeb.Admin.OrganisationController do
   end
 
   def update(conn, params) do
-    organisation = OrganisationManager.get_organisation(params["id"])
-    if organisation do
-      case OrganisationManager.update_organisation(organisation, params["organisation"]) do
-        {:ok, org} ->
+    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.slug != org1.slug.slug do
+                  case GitManager.rename_dir(org.slug.slug, org1.slug.slug) do
+                    :ok ->
+                      case GitManager.update_auth() do
+                        :ok -> nil
+                        error -> IO.inspect(error)
+                      end
+                      org1
+                    {: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, org))
         {:error, changeset} ->
@@ -109,9 +127,13 @@ defmodule KmxgitWeb.Admin.OrganisationController do
     login = params["organisation"]["login"]
     org = OrganisationManager.get_organisation!(params["organisation_id"])
     case OrganisationManager.add_user(org, login) do
-      {:ok, org} ->
+      {:ok, org1} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
-        |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
+        |> redirect(to: Routes.admin_organisation_path(conn, :show, org1))
       {:error, _} ->
         conn
         |> assign(:action, Routes.admin_organisation__path(conn, :add_user_post, org))
@@ -133,6 +155,10 @@ defmodule KmxgitWeb.Admin.OrganisationController do
     org = OrganisationManager.get_organisation!(params["organisation_id"])
     case OrganisationManager.remove_user(org, login) do
       {:ok, org} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
       {:error, _} ->
@@ -144,11 +170,20 @@ defmodule KmxgitWeb.Admin.OrganisationController do
   end
 
   def delete(conn, params) do
-    organisation = OrganisationManager.get_organisation(params["id"])
-    if organisation do
-      {:ok, _} = OrganisationManager.delete_organisation(organisation)
-      conn
-      |> redirect(to: Routes.admin_organisation_path(conn, :index))
+    org = OrganisationManager.get_organisation(params["id"])
+    if org do
+      case OrganisationManager.delete_organisation(org) do
+        {:ok, _} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
+          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
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
index 01e2d32..beb4b5f 100644
--- a/lib/kmxgit_web/controllers/admin/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -55,6 +55,10 @@ defmodule KmxgitWeb.Admin.RepositoryController do
           end
         end) do
       {:ok, repo} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
       {:error, changeset} ->
@@ -106,6 +110,10 @@ defmodule KmxgitWeb.Admin.RepositoryController do
           end
         end) do
       {:ok, repo1} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo1))
       {:error, changeset} ->
@@ -129,6 +137,10 @@ defmodule KmxgitWeb.Admin.RepositoryController do
     repo = RepositoryManager.get_repository!(params["repository_id"])
     case RepositoryManager.add_member(repo, login) do
       {:ok, repo} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
       {:error, _} ->
@@ -152,6 +164,10 @@ defmodule KmxgitWeb.Admin.RepositoryController do
     repo = RepositoryManager.get_repository!(params["repository_id"])
     case RepositoryManager.remove_member(repo, login) do
       {:ok, repo} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
       {:error, _} ->
@@ -172,6 +188,10 @@ defmodule KmxgitWeb.Admin.RepositoryController do
             end
           end) do
         {:ok, _} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.admin_repository_path(conn, :index))
         {:error, changeset} ->
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index 175655f..2b89ced 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -61,9 +61,13 @@ defmodule KmxgitWeb.Admin.UserController do
 
   defp update_user(conn, user, params) do
     case UserManager.admin_update_user(user, params["user"]) do
-      {:ok, _updated_user} ->
+      {:ok, user1} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
-        |> redirect(to: Routes.admin_user_path(conn, :show, user))
+        |> redirect(to: Routes.admin_user_path(conn, :show, user1))
       {:error, changeset} ->
         conn
         |> render("edit.html", changeset: changeset)
@@ -81,6 +85,10 @@ defmodule KmxgitWeb.Admin.UserController do
     
   defp delete_user(conn, user) 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))
   end
diff --git a/lib/kmxgit_web/controllers/organisation_controller.ex b/lib/kmxgit_web/controllers/organisation_controller.ex
index c7e0777..a538b56 100644
--- a/lib/kmxgit_web/controllers/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/organisation_controller.ex
@@ -60,7 +60,12 @@ defmodule KmxgitWeb.OrganisationController do
               {:ok, org1} ->
                 if org.slug.slug != org1.slug.slug do
                   case GitManager.rename_dir(org.slug.slug, org1.slug.slug) do
-                    :ok -> org1
+                    :ok ->
+                      case GitManager.update_auth() do
+                        :ok -> nil
+                        error -> IO.inspect(error)
+                      end
+                      org1
                     {:error, err} -> Repo.rollback(err)
                   end
                 else
@@ -103,6 +108,10 @@ defmodule KmxgitWeb.OrganisationController do
     if org && Organisation.owner?(org, current_user) do
       case OrganisationManager.add_user(org, login) do
         {:ok, org} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.slug_path(conn, :show, org.slug.slug))
         {:error, _e} ->
@@ -136,6 +145,10 @@ defmodule KmxgitWeb.OrganisationController do
     if org && Organisation.owner?(org, current_user) do
       case OrganisationManager.remove_user(org, login) do
         {:ok, org} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.slug_path(conn, :show, org.slug.slug))
         {:error, _} ->
@@ -157,7 +170,12 @@ defmodule KmxgitWeb.OrganisationController do
             case OrganisationManager.delete_organisation(org) do
               {:ok, _} ->
                 case GitManager.delete_dir(org.slug.slug) do
-                  :ok -> :ok
+                  :ok ->
+                    case GitManager.update_auth() do
+                      :ok -> nil
+                      error -> IO.inspect(error)
+                    end
+                    :ok
                   {:error, out} -> Repo.rollback(status: out)
                 end
               {:error, e} -> Repo.rollback(e)
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 32d88c2..e7236a1 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -74,6 +74,10 @@ defmodule KmxgitWeb.RepositoryController do
           end
         end) do
       {:ok, repo} ->
+        case GitManager.update_auth() do
+          :ok -> nil
+          error -> IO.inspect(error)
+        end
         conn
         |> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
       {:error, changeset} ->
@@ -290,6 +294,10 @@ defmodule KmxgitWeb.RepositoryController do
             end
           end) do
         {:ok, repo} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
         {:error, changeset} ->
@@ -332,6 +340,10 @@ defmodule KmxgitWeb.RepositoryController do
       org = repo.organisation
       case RepositoryManager.add_member(repo, login) do
         {:ok, repo} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
         {:error, _} ->
@@ -373,6 +385,10 @@ defmodule KmxgitWeb.RepositoryController do
       org = repo.organisation
       case RepositoryManager.remove_member(repo, login) do
         {:ok, repo} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
         {:error, _} ->
@@ -400,6 +416,10 @@ defmodule KmxgitWeb.RepositoryController do
             end
           end) do
         {:ok, _} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.slug_path(conn, :show, params["owner"]))
         {:error, _changeset} ->
diff --git a/lib/kmxgit_web/controllers/user_controller.ex b/lib/kmxgit_web/controllers/user_controller.ex
index 1a34881..0336b7a 100644
--- a/lib/kmxgit_web/controllers/user_controller.ex
+++ b/lib/kmxgit_web/controllers/user_controller.ex
@@ -45,6 +45,10 @@ defmodule KmxgitWeb.UserController do
             end
           end) do
         {:ok, user} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: Routes.slug_path(conn, :show, user.slug.slug))
         {:error, changeset} ->
@@ -72,6 +76,10 @@ defmodule KmxgitWeb.UserController do
             end
           end) do
         {:ok, _} ->
+          case GitManager.update_auth() do
+            :ok -> nil
+            error -> IO.inspect(error)
+          end
           conn
           |> redirect(to: "/")
         {:error, changeset} ->