From 7035fe5b2de48b5f2d11c3e9177dc6d4c6ee7639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Diedrich?= Date: Mon, 18 Jul 2022 15:16:57 +0200 Subject: [PATCH] something awful login data all the stuff to add 2 fields to the user object and to access the logged0in user in a liveview session --- lib/something_erlang/accounts.ex | 10 +++++ lib/something_erlang/accounts/user.ex | 11 +++++ lib/something_erlang/forums.ex | 3 +- .../controllers/user_settings_controller.ex | 20 ++++++++- .../live/thread_live/show.ex | 2 + .../live/user_live_auth.ex | 17 ++++++++ lib/something_erlang_web/router.ex | 19 ++++++--- .../templates/layout/_user_menu.html.heex | 2 +- .../templates/layout/root.html.heex | 26 +++++++----- .../templates/page/index.html.heex | 41 ------------------- .../templates/user_settings/edit.html.heex | 30 +++++++++++++- .../20220718094805_users_add_sadata.exs | 10 +++++ 12 files changed, 129 insertions(+), 62 deletions(-) create mode 100644 lib/something_erlang_web/live/user_live_auth.ex create mode 100644 priv/repo/migrations/20220718094805_users_add_sadata.exs diff --git a/lib/something_erlang/accounts.ex b/lib/something_erlang/accounts.ex index 8adefc6..71a2c13 100644 --- a/lib/something_erlang/accounts.ex +++ b/lib/something_erlang/accounts.ex @@ -95,6 +95,16 @@ defmodule SomethingErlang.Accounts do ## Settings + def change_user_sadata(%User{} = user, attrs \\ %{}) do + User.sadata_changeset(user, attrs) + end + + def update_sadata(%User{} = user, attrs \\ %{}) do + user + |> change_user_sadata(attrs) + |> Repo.update() + end + @doc """ Returns an `%Ecto.Changeset{}` for changing the user email. diff --git a/lib/something_erlang/accounts/user.ex b/lib/something_erlang/accounts/user.ex index b17a7ca..969b32b 100644 --- a/lib/something_erlang/accounts/user.ex +++ b/lib/something_erlang/accounts/user.ex @@ -8,9 +8,20 @@ defmodule SomethingErlang.Accounts.User do field :hashed_password, :string, redact: true field :confirmed_at, :naive_datetime + field :bbuserid, :string + field :bbpassword, :string + timestamps() end + @doc """ + A user changeset for SA data. + """ + def sadata_changeset(user, attrs, _opts \\ []) do + user + |> cast(attrs, [:bbuserid, :bbpassword]) + end + @doc """ A user changeset for registration. diff --git a/lib/something_erlang/forums.ex b/lib/something_erlang/forums.ex index b2b071f..a95f017 100644 --- a/lib/something_erlang/forums.ex +++ b/lib/something_erlang/forums.ex @@ -35,7 +35,8 @@ defmodule SomethingErlang.Forums do ** (Ecto.NoResultsError) """ - def get_thread!(id), do: Repo.get!(Thread, id) + def get_thread!(id), + do: %Thread{id: id, thread_id: id, title: "foo"} #Repo.get!(Thread, id) @doc """ Creates a thread. diff --git a/lib/something_erlang_web/controllers/user_settings_controller.ex b/lib/something_erlang_web/controllers/user_settings_controller.ex index 2b52c37..911d8ae 100644 --- a/lib/something_erlang_web/controllers/user_settings_controller.ex +++ b/lib/something_erlang_web/controllers/user_settings_controller.ex @@ -4,12 +4,27 @@ defmodule SomethingErlangWeb.UserSettingsController do alias SomethingErlang.Accounts alias SomethingErlangWeb.UserAuth - plug :assign_email_and_password_changesets + plug :assign_changesets def edit(conn, _params) do render(conn, "edit.html") end + def update(conn, %{"action" => "update_sadata"} = params) do + %{"user" => user_params} = params + user = conn.assigns.current_user + + case Accounts.update_sadata(user, user_params) do + {:ok, _user} -> + conn + |> put_flash(:info, "Settings updated successfully.") + |> redirect(to: Routes.user_settings_path(conn, :edit)) + + {:error, changeset} -> + render(conn, "edit.html", sadata_changeset: changeset) + end + end + def update(conn, %{"action" => "update_email"} = params) do %{"current_password" => password, "user" => user_params} = params user = conn.assigns.current_user @@ -64,10 +79,11 @@ defmodule SomethingErlangWeb.UserSettingsController do end end - defp assign_email_and_password_changesets(conn, _opts) do + defp assign_changesets(conn, _opts) do user = conn.assigns.current_user conn + |> assign(:sadata_changeset, Accounts.change_user_sadata(user)) |> assign(:email_changeset, Accounts.change_user_email(user)) |> assign(:password_changeset, Accounts.change_user_password(user)) end diff --git a/lib/something_erlang_web/live/thread_live/show.ex b/lib/something_erlang_web/live/thread_live/show.ex index 08e075d..829de1c 100644 --- a/lib/something_erlang_web/live/thread_live/show.ex +++ b/lib/something_erlang_web/live/thread_live/show.ex @@ -1,10 +1,12 @@ defmodule SomethingErlangWeb.ThreadLive.Show do use SomethingErlangWeb, :live_view + on_mount SomethingErlangWeb.UserLiveAuth alias SomethingErlang.Forums @impl true def mount(_params, _session, socket) do + socket |> IO.inspect {:ok, socket} end diff --git a/lib/something_erlang_web/live/user_live_auth.ex b/lib/something_erlang_web/live/user_live_auth.ex new file mode 100644 index 0000000..74759d5 --- /dev/null +++ b/lib/something_erlang_web/live/user_live_auth.ex @@ -0,0 +1,17 @@ +defmodule SomethingErlangWeb.UserLiveAuth do + import Phoenix.LiveView + + alias SomethingErlang.Accounts + + + def on_mount(:default, _params, %{"user_token" => user_token} = _session, socket) do + user = Accounts.get_user_by_session_token(user_token) + socket = assign_new(socket, :current_user, fn -> user end) + + if socket.assigns.current_user.confirmed_at do + {:cont, socket} + else + {:halt, redirect(socket, to: "/login")} + end + end +end diff --git a/lib/something_erlang_web/router.ex b/lib/something_erlang_web/router.ex index f1295d6..9203cd3 100644 --- a/lib/something_erlang_web/router.ex +++ b/lib/something_erlang_web/router.ex @@ -21,13 +21,22 @@ defmodule SomethingErlangWeb.Router do pipe_through :browser get "/", PageController, :index + end - live "/threads", ThreadLive.Index, :index - live "/threads/new", ThreadLive.Index, :new - live "/threads/:id/edit", ThreadLive.Index, :edit + scope "/thread", SomethingErlangWeb do + pipe_through :browser - live "/threads/:id", ThreadLive.Show, :show - live "/threads/:id/show/edit", ThreadLive.Show, :edit + live "/:id", ThreadLive.Show, :show + end + + scope "/admin", SomethingErlangWeb do + pipe_through [:browser, :require_authenticated_user] + + live "/thread", ThreadLive.Index, :index + live "/thread/new", ThreadLive.Index, :new + live "/thread/:id/edit", ThreadLive.Index, :edit + + live "/thread/:id/show/edit", ThreadLive.Show, :edit end # Other scopes may use custom stacks. diff --git a/lib/something_erlang_web/templates/layout/_user_menu.html.heex b/lib/something_erlang_web/templates/layout/_user_menu.html.heex index 68c2f5a..6548516 100644 --- a/lib/something_erlang_web/templates/layout/_user_menu.html.heex +++ b/lib/something_erlang_web/templates/layout/_user_menu.html.heex @@ -1,4 +1,4 @@ -
+
<%= if @current_user do %>

<%= @current_user.email %>

<%= link "Settings", class: "link", diff --git a/lib/something_erlang_web/templates/layout/root.html.heex b/lib/something_erlang_web/templates/layout/root.html.heex index dadb811..95bb09c 100644 --- a/lib/something_erlang_web/templates/layout/root.html.heex +++ b/lib/something_erlang_web/templates/layout/root.html.heex @@ -5,21 +5,27 @@ - <%= live_title_tag assigns[:page_title] || "SomethingErlang", suffix: " · Phoenix Framework" %> - - + <%= live_title_tag assigns[:page_title] || "SomethingErlang", + suffix: " · Phoenix Framework" %> + +
-
-
<%= @inner_content %> diff --git a/lib/something_erlang_web/templates/page/index.html.heex b/lib/something_erlang_web/templates/page/index.html.heex index f844bd8..e69de29 100644 --- a/lib/something_erlang_web/templates/page/index.html.heex +++ b/lib/something_erlang_web/templates/page/index.html.heex @@ -1,41 +0,0 @@ -
-

<%= gettext "Welcome to %{name}!", name: "Phoenix" %>

-

Peace of mind from prototype to production

-
- -
- - -
diff --git a/lib/something_erlang_web/templates/user_settings/edit.html.heex b/lib/something_erlang_web/templates/user_settings/edit.html.heex index 9863bc5..5b8a40c 100644 --- a/lib/something_erlang_web/templates/user_settings/edit.html.heex +++ b/lib/something_erlang_web/templates/user_settings/edit.html.heex @@ -1,5 +1,31 @@

Settings

+

Change SA data

+ +<.form let={f} for={@sadata_changeset} + action={Routes.user_settings_path(@conn, :update)} + id="update_sadata"> + <%= if @sadata_changeset.action do %> +
+

Oops, something went wrong! Please check the errors below.

+
+ <% end %> + + <%= hidden_input f, :action, name: "action", value: "update_sadata" %> + + <%= label f, :bbuserid %> + <%= text_input f, :bbuserid, required: true %> + <%= error_tag f, :bbuserid %> + + <%= label f, :bbpassword %> + <%= text_input f, :bbpassword, required: true %> + <%= error_tag f, :bbpassword %> + +
+ <%= submit "Change sadata", class: "btn" %> +
+ +

Change email

<.form let={f} for={@email_changeset} action={Routes.user_settings_path(@conn, :update)} id="update_email"> @@ -20,7 +46,7 @@ <%= error_tag f, :current_password %>
- <%= submit "Change email" %> + <%= submit "Change email", class: "btn" %>
@@ -48,6 +74,6 @@ <%= error_tag f, :current_password %>
- <%= submit "Change password" %> + <%= submit "Change password", class: "btn" %>
diff --git a/priv/repo/migrations/20220718094805_users_add_sadata.exs b/priv/repo/migrations/20220718094805_users_add_sadata.exs new file mode 100644 index 0000000..ecb6fe0 --- /dev/null +++ b/priv/repo/migrations/20220718094805_users_add_sadata.exs @@ -0,0 +1,10 @@ +defmodule SomethingErlang.Repo.Migrations.UsersAddSadata do + use Ecto.Migration + + def change do + alter table("users") do + add :bbuserid, :string + add :bbpassword, :string + end + end +end