Commit ba816e5cdb25861d8af5a072f14080db7d223a46

Thomas de Grivel 2021-12-14T17:00:14

fix user registration

diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 4432431..6b7a776 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -47,6 +47,7 @@ defmodule Kmxgit.UserManager.User do
     |> cast(attrs, [:email, :password])
     |> validate_email()
     |> validate_password(opts)
+    |> common_changeset()
   end
 
   defp validate_email(changeset) do
@@ -197,4 +198,16 @@ defmodule Kmxgit.UserManager.User do
     user.owned_repositories
     |> Enum.sort_by(&Repository.full_slug/1)
   end
+
+  def login(user) do
+    if user do
+      if user.slug do
+        user.slug.slug
+      else
+        nil
+      end
+    else
+      nil
+    end
+  end
 end
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index ab491d3..cbbf9fd 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -15,6 +15,27 @@ defmodule KmxgitWeb.Admin.UserController do
     |> render("index.html")
   end
 
+  def new(conn, params) do
+    changeset = UserManager.change_user()
+    conn
+    |> assign(:action, Routes.admin_user_path(conn, :create))
+    |> assign(:changeset, changeset)
+    |> render("new.html")
+  end
+
+  def create(conn, params) do
+    case UserManager.create_user(params) do
+      {:ok, user} ->
+        conn
+        |> redirect(to: Routes.admin_user_path(conn, :show, user))
+      {:error, changeset} ->
+        conn
+        |> assign(:action, Routes.admin_user_path(conn, :create))
+        |> assign(:changeset, changeset)
+        |> render("new.html")
+    end
+  end
+
   def show(conn, params) do
     user = UserManager.get_user(params["id"])
     if user do
@@ -22,7 +43,7 @@ defmodule KmxgitWeb.Admin.UserController do
       contributor_repos = RepositoryManager.list_contributor_repositories(user)
       repos = owned_repos ++ contributor_repos
       conn
-      |> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
+      |> assign(:page_title, gettext("User %{login}", login: User.login(user)))
       |> assign(:repos, repos)
       |> assign(:user, user)
       |> render("show.html")
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 4dfde1e..51252da 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -16,10 +16,10 @@
         <td><%= link user.id, to: Routes.admin_user_path(@conn, :show, user) %></td>
         <td><%= link user.name, to: Routes.admin_user_path(@conn, :show, user) %></td>
         <td><%= link user.email, to: "mailto:#{user.email}" %></td>
-        <td><%= link user.slug.slug, to: Routes.admin_user_path(@conn, :show, user) %></td>
+        <td><%= link User.login(user), to: Routes.admin_user_path(@conn, :show, user) %></td>
         <td><%= user.is_admin %></td>
         <td><%= user.deploy_only %></td>
-        <td><%= link gettext("Show"), to: Routes.slug_path(@conn, :show, user.slug.slug), class: "btn btn-sm btn-primary" %></td>
+        <td><%= link gettext("Show"), to: Routes.slug_path(@conn, :show, User.login(user) || ""), class: "btn btn-sm btn-primary" %></td>
       </tr>
     <% end %>
   </table>
diff --git a/lib/kmxgit_web/templates/admin/user/show.html.heex b/lib/kmxgit_web/templates/admin/user/show.html.heex
index d78a989..69b06d4 100644
--- a/lib/kmxgit_web/templates/admin/user/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/show.html.heex
@@ -1,5 +1,5 @@
 <div class="container-fluid">
-  <h1>User <%= @user.slug.slug %></h1>
+  <h1>User <%= User.login(@user) %></h1>
 
   <table class="table admin-properties">
     <tr>
@@ -16,7 +16,7 @@
     </tr>
     <tr>
       <th><%= gettext "Login" %></th>
-      <td><%= link @user.slug.slug, to: Routes.slug_path(@conn, :show, @user.slug.slug) %></td>
+      <td><%= User.login(@user) %></td>
     </tr>
     <tr>
       <th><%= gettext "Description" %></th>
@@ -77,6 +77,6 @@
       class: "btn btn-primary" %>
 
   <%= link gettext("Show"),
-      to: Routes.slug_path(@conn, :show, @user.slug.slug),
+      to: Routes.slug_path(@conn, :show, User.login(@user) || ""),
       class: "btn btn-primary" %>
 </div>
diff --git a/lib/kmxgit_web/views/admin/user_view.ex b/lib/kmxgit_web/views/admin/user_view.ex
index 200672b..3d175c7 100644
--- a/lib/kmxgit_web/views/admin/user_view.ex
+++ b/lib/kmxgit_web/views/admin/user_view.ex
@@ -2,4 +2,5 @@ defmodule KmxgitWeb.Admin.UserView do
   use KmxgitWeb, :view
 
   alias Kmxgit.RepositoryManager.Repository
+  alias Kmxgit.UserManager.User
 end