Commit c1d220d59e0378580964e46d8b96ec2b6d185fe3

Thomas de Grivel 2021-11-18T16:58:30

admin

diff --git a/assets/css/app.scss b/assets/css/app.scss
index 94c8c8c..cc4e9da 100644
--- a/assets/css/app.scss
+++ b/assets/css/app.scss
@@ -14,6 +14,9 @@ textarea#user_ssh_keys {
     overflow-x: scroll;
     overflow-y: scroll;
 }
+textarea#organisation_description {
+    min-height: 10em;
+}
 pre.ssh_keys {
     max-width: 20em;
     white-space: pre-wrap;       /* Since CSS 2.1 */
diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index dca53d1..9d7ad57 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -31,6 +31,7 @@ defmodule Kmxgit.OrganisationManager do
     Repo.one from organisation in Organisation,
       where: [id: ^id],
       preload: :slug,
+      preload: [users: :slug],
       limit: 1
   end
 
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 954ff23..2e63dd9 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -9,8 +9,8 @@ defmodule Kmxgit.OrganisationManager.Organisation do
   schema "organisations" do
     field :description, :string
     field :name, :string
-    many_to_many :users, User, join_through: "users_organisations"
-    has_one :slug, Slug
+    many_to_many :users, User, join_through: "users_organisations", on_delete: :delete_all
+    has_one :slug, Slug, on_delete: :delete_all
     timestamps()
   end
 
@@ -18,7 +18,6 @@ defmodule Kmxgit.OrganisationManager.Organisation do
   def changeset(organisation, attrs \\ %{}) do
     organisation
     |> cast(attrs, [:description, :name])
-    |> cast_assoc(:users)
     |> cast_assoc(:slug)
     |> validate_required([:slug])
     |> Markdown.validate_markdown(:description)
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 1a2a557..a7aea10 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -15,7 +15,7 @@ defmodule Kmxgit.UserManager.User do
     field :name, :string
     field :password, :string, virtual: true, redact: true
     field :password_confirmation, :string, virtual: true, redact: true
-    has_one :slug, Slug
+    has_one :slug, Slug, on_delete: :delete_all
     field :ssh_keys, :string
     many_to_many :organisations, Organisation, join_through: "users_organisations"
     timestamps()
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 3b31f38..35cbff6 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -53,22 +53,25 @@ defmodule KmxgitWeb.Admin.OrganisationController do
   end
 
   def show(conn, params) do
-    organisation = OrganisationManager.get_organisation(params["id"])
-    if organisation do
+    org = OrganisationManager.get_organisation(params["id"])
+    if org do
       conn
-      |> render("show.html", organisation: organisation)
+      |> assign(:org, org)
+      |> render("show.html")
     else
       not_found(conn)
     end
   end
 
   def edit(conn, params) do
-    organisation = OrganisationManager.get_organisation(params["id"])
-    if organisation do
-      changeset = OrganisationManager.change_organisation(organisation)
+    org = OrganisationManager.get_organisation(params["id"])
+    if org do
+      changeset = OrganisationManager.change_organisation(org)
       conn
-      |> render("edit.html", changeset: changeset,
-      action: Routes.admin_organisation_path(conn, :update, organisation))
+      |> assign(:action, Routes.admin_organisation_path(conn, :update, org))
+      |> assign(:changeset, changeset)
+      |> assign(:org, org)
+      |> render("edit.html")
     else
       not_found(conn)
     end
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index 614aef9..175655f 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -30,7 +30,7 @@ defmodule KmxgitWeb.Admin.UserController do
 
   defp show_user(conn, user) do
     conn
-    |> assign(:page_title, gettext("User %{login}", login: user.login))
+    |> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
     |> assign(:user, user)
     |> render("show.html")
   end
diff --git a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
new file mode 100644
index 0000000..ed84074
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
@@ -0,0 +1,32 @@
+<div class="container-fluid">
+  <h1><%= gettext("Edit organisation %{organisation}", organisation: @org.name || @org.slug.slug) %></h1>
+
+  <%= form_for @changeset, @action, fn f -> %>
+
+    <%= inputs_for f, :slug, fn ff -> %>
+      <div class="mb-3">
+        <%= label ff, :slug, class: "form-label" %>
+        <%= text_input ff, :slug, class: "form-control" %>
+        <%= error_tag ff, :slug %>
+      </div>
+    <% end %>
+
+    <div class="mb-3">
+      <%= label f, :name, class: "form-label" %>
+      <%= text_input f, :name, class: "form-control" %>
+      <%= error_tag f, :name %>
+    </div>
+
+    <div class="mb-3">
+      <%= label f, :description, class: "form-label" %>
+      <%= textarea f, :description, class: "form-control" %>
+      <%= error_tag f, :description %>
+    </div>
+
+    <div class="mb-3">
+      <%= submit gettext("Submit"), class: "btn btn-primary" %>
+    </div>
+
+  <% end %>
+
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
new file mode 100644
index 0000000..e9adaeb
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -0,0 +1,18 @@
+<div class="container-fluid">
+  <h1>Organisations</h1>
+
+  <table class="table admin-index">
+    <tr>
+      <th><%= gettext "Id" %></th>
+      <th><%= gettext "Name" %></th>
+      <th><%= gettext "Slug" %></th>
+    </tr>
+    <%= Enum.map @organisations, fn(org) -> %>
+      <tr>
+        <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+        <td><%= org.name %></td>
+        <td><%= link org.slug.slug, to: Routes.organisation_path(@conn, :show, org.slug.slug) %></td>
+      </tr>
+    <% end %>
+  </table>
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/show.html.heex b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
new file mode 100644
index 0000000..5bf3e00
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
@@ -0,0 +1,44 @@
+<div class="container-fluid">
+  <h1>Organisation <%= @org.name || @org.slug.slug %></h1>
+
+  <table class="table admin-properties">
+    <tr>
+      <th><%= gettext "Id" %></th>
+      <td><%= @org.id %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Name" %></th>
+      <td><%= link @org.name, to: Routes.organisation_path(@conn, :show, @org.slug.slug) %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Slug" %></th>
+      <td><%= link @org.slug.slug, to: Routes.organisation_path(@conn, :show, @org.slug.slug) %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Description" %></th>
+      <td>
+        <%= if @org.description do %>
+          <%= raw Earmark.as_html!(@org.description) %>
+        <% end %>
+      </td>
+    </tr>
+    <tr>
+      <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 %>
+      </td>
+    </tr>
+  </table>
+
+  <%= link gettext("Delete org"),
+      to: Routes.admin_organisation_path(@conn, :delete, @org),
+      class: "btn btn-danger",
+      data: [confirm: gettext("Are you sure you want to delete this org ?")],
+      method: :delete %>
+
+  <%= link gettext("Edit org"),
+      to: Routes.admin_organisation_path(@conn, :edit, @org),
+      class: "btn btn-primary" %>
+</div>
diff --git a/lib/kmxgit_web/templates/admin/user/edit.html.heex b/lib/kmxgit_web/templates/admin/user/edit.html.heex
index 7776383..b6d247f 100644
--- a/lib/kmxgit_web/templates/admin/user/edit.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/edit.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1><%= gettext "Edit user" %> <%= @user.login %></h1>
+  <h1><%= gettext "Edit user" %> <%= @user.slug.slug %></h1>
 
   <%= render("form.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/user/form.html.heex b/lib/kmxgit_web/templates/admin/user/form.html.heex
index c023dd3..954c41f 100644
--- a/lib/kmxgit_web/templates/admin/user/form.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/form.html.heex
@@ -6,11 +6,13 @@
     <%= error_tag f, :name %>
   </div>
 
-  <div class="mb-3">
-    <%= label f, :login, class: "form-label" %>
-    <%= text_input f, :login, class: "form-control" %>
-    <%= error_tag f, :login %>
-  </div>
+  <%= inputs_for f, :slug, fn ff -> %>
+    <div class="mb-3">
+      <%= label ff, :slug, gettext("Login"), class: "form-label" %>
+      <%= text_input ff, :slug, class: "form-control" %>
+      <%= error_tag ff, :slug %>
+    </div>
+  <% end %>
 
   <div class="mb-3">
     <%= label f, :email, class: "form-label" %>
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 0499b9b..a05c437 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -13,7 +13,7 @@
       <td><%= link user.id, to: Routes.admin_user_path(@conn, :show, user) %></td>
       <td><%= user.name %></td>
       <td><%= link user.email, to: "mailto:#{user.email}" %></td>
-      <td><%= link user.login, to: Routes.user_path(@conn, :show, user.login) %></td>
+      <td><%= link user.slug.slug, to: Routes.user_path(@conn, :show, user.slug.slug) %></td>
       <td><%= user.is_admin %></td>
     </tr>
   <% end %>
diff --git a/lib/kmxgit_web/templates/admin/user/show.html.heex b/lib/kmxgit_web/templates/admin/user/show.html.heex
index fcf9f6f..1c2d231 100644
--- a/lib/kmxgit_web/templates/admin/user/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/show.html.heex
@@ -1,4 +1,4 @@
-<h1>User <%= @user.login %></h1>
+<h1>User <%= @user.slug.slug %></h1>
 
 <table class="table admin-properties">
   <tr>
@@ -15,7 +15,25 @@
   </tr>
   <tr>
     <th><%= gettext "Login" %></th>
-    <td><%= link @user.login, to: Routes.user_path(@conn, :show, @user.login) %></td>
+    <td><%= link @user.slug.slug, to: Routes.user_path(@conn, :show, @user.slug.slug) %></td>
+  </tr>
+  <tr>
+    <th><%= gettext "Description" %></th>
+    <td>
+      <%= if @user.description do %>
+      <%= raw Earmark.as_html!(@user.description) %>
+      <% end %>
+    </td>
+  </tr>
+  <tr>
+    <th>
+      <%= gettext "Organisations" %>
+    </th>
+    <td>
+      <%= for org <- @user.organisations do %>
+        <%= link(org.name || org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org), class: "org") %>
+      <% end %>
+    </td>
   </tr>
   <tr>
     <th><%= gettext "Admin" %></th>
diff --git a/lib/kmxgit_web/templates/layout/nav_connected.html.heex b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
index f8fb67e..78ad2ef 100644
--- a/lib/kmxgit_web/templates/layout/nav_connected.html.heex
+++ b/lib/kmxgit_web/templates/layout/nav_connected.html.heex
@@ -1,8 +1,3 @@
-<li class="nav-item">
-  <%= link gettext("New organisation"),
-           to: Routes.organisation_path(@conn, :new),
-           class: "nav-link" %>
-</li>
 <%= if @conn.assigns[:current_organisation] do %>
   <li class="nav-item">
     <%= link @current_organisation.name || @current_organisation.slug.slug,
diff --git a/lib/kmxgit_web/templates/organisation/show.html.heex b/lib/kmxgit_web/templates/organisation/show.html.heex
index 8457985..d6ba952 100644
--- a/lib/kmxgit_web/templates/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/organisation/show.html.heex
@@ -21,14 +21,14 @@
       <h2><%= gettext "Properties" %></h2>
       <table class="table admin-properties">
         <tr>
-          <th><%= gettext "Slug" %></th>
-          <td><%= @org.slug.slug %></td>
-        </tr>
-        <tr>
           <th><%= gettext "Name" %></th>
           <td><%= @org.name %></td>
         </tr>
         <tr>
+          <th><%= gettext "Slug" %></th>
+          <td><%= @org.slug.slug %></td>
+        </tr>
+        <tr>
           <th><%= gettext "Description" %></th>
           <td>
             <%= if @org.description do %>
diff --git a/lib/kmxgit_web/templates/user/show.html.heex b/lib/kmxgit_web/templates/user/show.html.heex
index 36190df..387eab7 100644
--- a/lib/kmxgit_web/templates/user/show.html.heex
+++ b/lib/kmxgit_web/templates/user/show.html.heex
@@ -40,11 +40,16 @@
           </td>
         </tr>
         <tr>
-          <th><%= gettext "Organisations" %></th>
+          <th>
+            <%= gettext "Organisations" %>
+          </th>
           <td>
             <%= for org <- @user.organisations do %>
               <%= link(org.name || org.slug.slug, to: Routes.organisation_path(@conn, :show, org.slug.slug), class: "org") %>
             <% end %>
+            <%= if @user == @current_user do %>
+              <%= link("+", to: Routes.organisation_path(@conn, :new), class: "btn btn-primary") %>
+            <% end %>
           </td>
         </tr>
         <tr>
diff --git a/lib/kmxgit_web/views/admin/organisation_view.ex b/lib/kmxgit_web/views/admin/organisation_view.ex
new file mode 100644
index 0000000..2743368
--- /dev/null
+++ b/lib/kmxgit_web/views/admin/organisation_view.ex
@@ -0,0 +1,3 @@
+defmodule KmxgitWeb.Admin.OrganisationView do
+  use KmxgitWeb, :view
+end