Commit 960495e1228846da125d9b1e58f2cec81ef6830b

Thomas de Grivel 2021-11-22T01:26:43

admin: add and remove user to and from an organisation

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index f47db9b..c368af7 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -11,6 +11,13 @@ defmodule Kmxgit.OrganisationManager do
     Repo.all from org in Organisation, preload: :slug
   end
 
+  def get_organisation!(id) do
+    org = Repo.one from org in Organisation,
+      where: org.id == ^id,
+      preload: [:slug, :users]
+    org || raise Ecto.NoResultsError
+  end
+
   def change_organisation(organisation \\ %Organisation{}) do
     Organisation.changeset(organisation, %{})
   end
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index f1b1424..f94f5b3 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -88,13 +88,59 @@ defmodule KmxgitWeb.Admin.OrganisationController do
           IO.inspect(changeset)
           conn
           |> render("edit.html", changeset: changeset,
-                    action: Routes.admin_organisation_path(conn, :update, organisation))
+                    action: Routes.admin_organisation__path(conn, :update, organisation))
       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, org} ->
+        conn
+        |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
+      {: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} ->
+        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
     organisation = OrganisationManager.get_organisation(params["id"])
     if organisation do
diff --git a/lib/kmxgit_web/router.ex b/lib/kmxgit_web/router.ex
index 3496dab..67243cb 100644
--- a/lib/kmxgit_web/router.ex
+++ b/lib/kmxgit_web/router.ex
@@ -84,7 +84,18 @@ defmodule KmxgitWeb.Router do
     scope "/admin", Admin, as: "admin" do
       pipe_through :admin
       get "/", DashboardController, :index
-      resources "/organisations", OrganisationController
+      resources "/organisations", OrganisationController do
+        get  "/add_user/",   OrganisationController, :add_user, as: :""
+        post "/add_user",    OrganisationController, :add_user_post, as: :""
+        get  "/remove_user", OrganisationController, :remove_user, as: :""
+        post "/remove_user", OrganisationController, :remove_user_post, as: :""
+      end
+      resources "/repositories", RepositoryController do
+        get  "/add_user",    RepositoryController, :add_user, as: :""
+        post "/add_user",    RepositoryController, :add_user_post, as: :""
+        get  "/remove_user", RepositoryController, :remove_user, as: :""
+        post "/remove_user", RepositoryController, :remove_user_post, as: :""
+      end
       resources "/users", UserController
 
       import Phoenix.LiveDashboard.Router
diff --git a/lib/kmxgit_web/templates/admin/dashboard/index.html.heex b/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
index b441922..018617b 100644
--- a/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
@@ -1 +1,5 @@
-<h1>Admin dashboard</h1>
+<div class="container-fluid">
+
+  <h1>Admin dashboard</h1>
+
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex b/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex
new file mode 100644
index 0000000..3ff78fa
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/add_user.html.heex
@@ -0,0 +1,17 @@
+<div class="container-fluid">
+
+  <h1><%= gettext "Add user to %{org}", org: @org.name || @org.slug.slug %></h1>
+
+  <%= form_for :organisation, @action, fn f -> %>
+
+    <div class="mb-3">
+      <%= label f, :login, class: "form-label" %>
+      <%= text_input f, :login, class: "form=control" %>
+    </div>
+
+    <div class="mb-3">
+      <%= submit gettext("Submit"), class: "btn btn-primary" %>
+    </div>
+
+  <% end %>
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex b/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex
new file mode 100644
index 0000000..96339ff
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/remove_user.html.heex
@@ -0,0 +1,17 @@
+<div class="container-fluid">
+
+  <h1><%= gettext "Remove user from %{org}", org: @org.name || @org.slug.slug %></h1>
+
+  <%= form_for :organisation, @action, fn f -> %>
+
+    <div class="mb-3">
+      <%= label f, :login, class: "form-label" %>
+      <%= text_input f, :login, class: "form=control" %>
+    </div>
+
+    <div class="mb-3">
+      <%= submit gettext("Submit"), class: "btn btn-primary" %>
+    </div>
+
+  <% end %>
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/show.html.heex b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
index 6efd2eb..2eb5479 100644
--- a/lib/kmxgit_web/templates/admin/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
@@ -26,8 +26,14 @@
       <th><%= gettext "Users" %></th>
       <td>
         <%= for user <- @org.users do %>
-            <%= link(user.slug.slug, to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
-          <% end %>
+          <%= link(user.slug.slug, to: Routes.admin_user_path(@conn, :show, user), class: "user") %>
+        <% end %>
+        <%= link "-",
+            to: Routes.admin_organisation__path(@conn, :remove_user, @org),
+            class: "btn btn-danger btn-sm" %>
+        <%= link "+",
+            to: Routes.admin_organisation__path(@conn, :add_user, @org),
+            class: "btn btn-primary btn-sm" %>
       </td>
     </tr>
   </table>