Commit 84664d6b125c8078dc8b7ac69deaa0b2273934c8

Thomas de Grivel 2021-12-15T07:48:49

fix admin create user

diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 053d944..219b7d1 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -225,7 +225,7 @@ defmodule Kmxgit.UserManager do
     |> Repo.delete()
   end
 
-  def change_user(%User{} = user) do
+  def change_user(%User{} = user \\ %User{}) do
     User.changeset(user, %{})
   end
 
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 6b7a776..8bdb57f 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -170,9 +170,11 @@ defmodule Kmxgit.UserManager.User do
     |> common_changeset()
   end
 
-  def admin_changeset(user, attrs \\ %{}) do
+  def admin_changeset(user, attrs \\ %{}, opts \\ []) do
     user
-    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :ssh_keys])
+    |> cast(attrs, [:deploy_only, :description, :email, :is_admin, :name, :password, :ssh_keys])
+    |> validate_email()
+    |> validate_password(opts)
     |> common_changeset()
   end
 
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index cbbf9fd..258daa6 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -15,7 +15,7 @@ defmodule KmxgitWeb.Admin.UserController do
     |> render("index.html")
   end
 
-  def new(conn, params) do
+  def new(conn, _params) do
     changeset = UserManager.change_user()
     conn
     |> assign(:action, Routes.admin_user_path(conn, :create))
@@ -24,11 +24,15 @@ defmodule KmxgitWeb.Admin.UserController do
   end
 
   def create(conn, params) do
-    case UserManager.create_user(params) do
+    pw = :crypto.strong_rand_bytes(16) |> Base.url_encode64()
+    pw = "Az0!#{pw}"
+    user_params = Map.merge(params["user"], %{"password" => pw})
+    case UserManager.admin_create_user(user_params) do
       {:ok, user} ->
         conn
         |> redirect(to: Routes.admin_user_path(conn, :show, user))
       {:error, changeset} ->
+        IO.inspect changeset
         conn
         |> assign(:action, Routes.admin_user_path(conn, :create))
         |> assign(:changeset, changeset)
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 51252da..0949bfe 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -1,5 +1,12 @@
 <div class="container-fluid">
-  <h1>Users</h1>
+  <div class="row">
+    <div class="col col-md-7">
+      <h1>Users</h1>
+    </div>
+    <div class="col col-md-5">
+      <%= link gettext("Create user"), to: Routes.admin_user_path(@conn, :new), class: "btn btn-primary" %>
+    </div>
+  </div>
 
   <table class="table admin-index">
     <tr>
diff --git a/lib/kmxgit_web/templates/admin/user/new.html.heex b/lib/kmxgit_web/templates/admin/user/new.html.heex
new file mode 100644
index 0000000..8d67860
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/user/new.html.heex
@@ -0,0 +1,59 @@
+<div class="container-fluid">
+  <h1><%= gettext "Create user" %></h1>
+
+  <.form let={f} for={@changeset} action={@action}>
+    <%= if @changeset.action do %>
+      <div class="alert alert-danger">
+        <p>Oops, something went wrong! Please check the errors below.</p>
+      </div>
+    <% end %>
+
+    <div class="mb-3">
+      <%= label f, :name, class: "form-label" %>
+      <%= text_input f, :name, class: "form-control", required: true %>
+      <%= error_tag f, :name %>
+    </div>
+
+    <div class="mb-3">
+      <%= label f, :email, class: "form-label" %>
+      <%= text_input f, :email, class: "form-control", required: true %>
+      <%= error_tag f, :email %>
+    </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", required: true %>
+        <%= error_tag ff, :slug %>
+      </div>
+    <% end %>
+
+    <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">
+      <%= label f, :ssh_keys, gettext("SSH keys"), class: "form-label" %>
+      <%= textarea f, :ssh_keys, class: "form-control" %>
+      <%= error_tag f, :ssh_keys %>
+    </div>
+
+    <div class="mb-3 form-check">
+      <%= checkbox f, :is_admin, class: "form-check-input" %>
+      <%= label f, :is_admin, class: "form-check-label" %>
+      <%= error_tag f, :is_admin %>
+    </div>
+
+    <div class="mb-3 form-check">
+      <%= checkbox f, :deploy_only, class: "form-check-input" %>
+      <%= label f, :deploy_only, class: "form-check-label" %>
+      <%= error_tag f, :deploy_only %>
+    </div>
+
+    <div class="mb-3">
+      <%= submit gettext("Submit"), class: "btn btn-primary" %>
+    </div>
+  </.form>
+</div>