Commit daa203efca63fa1007fceca277a650d3ff070858

Thomas de Grivel 2021-11-25T07:10:25

/etc/git/auth.conf

diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index 752538d..8fe92e1 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -10,9 +10,10 @@ defmodule Kmxgit.RepositoryManager do
   alias Kmxgit.UserManager.User
 
   def list_repositories do
-    Repo.all from org in Repository,
-      preload: [organisation: :slug],
-      preload: [user: :slug]
+    Repo.all from r in Repository,
+      preload: [members: :slug,
+                organisation: [:slug, users: :slug],
+                user: :slug]
   end
 
   def change_repository(repository \\ %Repository{}) do
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index e4b85a5..fe2669c 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -53,4 +53,20 @@ defmodule Kmxgit.RepositoryManager.Repository do
     end
     |> Enum.uniq
   end
+
+  def auth(repo) do
+    repo
+    |> members()
+    |> Enum.sort(fn a, b ->
+      a.slug.slug < b.slug.slug
+    end)
+    |> Enum.map(fn user ->
+      mode = if user.deploy_only do
+          "r"
+        else
+          "rw"
+        end
+      "#{user.slug.slug} #{mode} \"#{full_slug(repo)}.git\"\n"
+    end)
+  end
 end
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index a338d4c..5ad99e9 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -2,6 +2,8 @@ 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}
 
@@ -56,4 +58,16 @@ defmodule KmxgitWeb.PageController do
     |> 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
diff --git a/lib/kmxgit_web/router.ex b/lib/kmxgit_web/router.ex
index 133fc0f..5f147f8 100644
--- a/lib/kmxgit_web/router.ex
+++ b/lib/kmxgit_web/router.ex
@@ -34,6 +34,7 @@ defmodule KmxgitWeb.Router do
     pipe_through [:browser, :auth]
 
     get  "/",           PageController, :index
+    get  "/_auth",      PageController, :auth
     get  "/_keys",      PageController, :keys
     get  "/_new_admin", PageController, :new_admin
     post "/_new_admin", PageController, :new_admin_post