From 2a1afb30b084243f012a41aefda376072d7dca9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Wed, 16 Aug 2023 22:59:39 +0100 Subject: [PATCH 01/12] Add handle field to users --- lib/atomic/accounts/user.ex | 13 ++++++++++++- lib/atomic_web/templates/layout/live.html.heex | 7 +++++-- .../20221014155230_create_users_auth_tables.exs | 1 + priv/repo/seeds/accounts.exs | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index a2ec48297..f230a8dab 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -9,7 +9,7 @@ defmodule Atomic.Accounts.User do alias Atomic.Organizations.{Membership, Organization} alias Atomic.Uploaders.ProfilePicture - @required_fields ~w(email password)a + @required_fields ~w(email handle password)a @optional_fields ~w(name role confirmed_at course_id default_organization_id)a @roles ~w(admin student)a @@ -17,6 +17,7 @@ defmodule Atomic.Accounts.User do schema "users" do field :name, :string field :email, :string + field :handle, :string field :password, :string, virtual: true, redact: true field :hashed_password, :string, redact: true field :confirmed_at, :naive_datetime @@ -53,6 +54,7 @@ defmodule Atomic.Accounts.User do user |> cast(attrs, @required_fields ++ @optional_fields) |> validate_email() + |> validate_handle() |> validate_password(opts) end @@ -79,6 +81,15 @@ defmodule Atomic.Accounts.User do |> unique_constraint(:email) end + defp validate_handle(changeset) do + changeset + |> validate_required([:handle]) + |> validate_format(:handle, ~r/^[a-z0-9_]+$/, message: "must only contain lowercase characters, numbers, and underscores") + |> validate_length(:handle, min: 3, max: 30) + |> unsafe_validate_unique(:handle, Atomic.Repo) + |> unique_constraint(:handle) + end + defp validate_password(changeset, opts) do changeset |> validate_required([:password]) diff --git a/lib/atomic_web/templates/layout/live.html.heex b/lib/atomic_web/templates/layout/live.html.heex index bf76b873d..12c3c873d 100644 --- a/lib/atomic_web/templates/layout/live.html.heex +++ b/lib/atomic_web/templates/layout/live.html.heex @@ -203,9 +203,12 @@ <% end %> <%= live_redirect to: Routes.user_settings_path(@socket, :edit), class: "bg-zinc-200 flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-gray-900" do %> - <%= Atomic.Accounts.extract_initials(@current_user.email) %> + <%= Atomic.Accounts.extract_initials(@current_user.name) %> + + + + - <% end %> diff --git a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs index f7fc7f948..7149ca795 100644 --- a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs +++ b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs @@ -8,6 +8,7 @@ defmodule Atomic.Repo.Migrations.CreateUsersAuthTables do add :id, :binary_id, primary_key: true add :name, :string add :email, :citext, null: false + add :handle, :citext, null: false add :hashed_password, :string, null: false add :confirmed_at, :naive_datetime add :profile_picture, :string diff --git a/priv/repo/seeds/accounts.exs b/priv/repo/seeds/accounts.exs index 544f0679c..d998efc9b 100644 --- a/priv/repo/seeds/accounts.exs +++ b/priv/repo/seeds/accounts.exs @@ -111,10 +111,12 @@ defmodule Atomic.Repo.Seeds.Accounts do for character <- characters do email = (character |> String.downcase() |> String.replace(~r/\s*/, "")) <> "@mail.pt" + handle = (character |> String.downcase() |> String.replace(~r/\s/, "_")) user = %{ "name" => character, "email" => email, + "handle" => handle, "password" => "password1234", "role" => role, "course_id" => Enum.random(courses).id, From a57f36e6862b745e146ab74f1b3d179459f3bffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 02:20:45 +0100 Subject: [PATCH 02/12] Add user profile page route --- lib/atomic/accounts.ex | 16 ++++++++++ lib/atomic_web/live/user_live/show.ex | 29 +++++++++++++++++++ lib/atomic_web/live/user_live/show.html.heex | 12 ++++++++ lib/atomic_web/router.ex | 2 ++ .../templates/layout/live.html.heex | 4 +-- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 lib/atomic_web/live/user_live/show.ex create mode 100644 lib/atomic_web/live/user_live/show.html.heex diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 5679bbf1d..edee60d24 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -26,6 +26,22 @@ defmodule Atomic.Accounts do Repo.get_by(User, email: email) end + @doc """ + Gets a user by handle. + + ## Examples + + iex> get_user_by_handle("foo_bar") + %User{} + + iex> get_user_by_handle("unknown") + nil + + """ + def get_user_by_handle(handle) when is_binary(handle) do + Repo.get_by(User, handle: handle) + end + @doc """ Gets a user by email and password. diff --git a/lib/atomic_web/live/user_live/show.ex b/lib/atomic_web/live/user_live/show.ex new file mode 100644 index 000000000..2aed9308a --- /dev/null +++ b/lib/atomic_web/live/user_live/show.ex @@ -0,0 +1,29 @@ +defmodule AtomicWeb.UserLive.Show do + use AtomicWeb, :live_view + + alias Atomic.Accounts + + @impl true + def mount(_params, _session, socket) do + {:ok, socket} + end + + @impl true + def handle_params(%{"handle" => user_handle}, _, socket) do + user = Accounts.get_user_by_handle(user_handle) + + entries = [ + %{ + name: gettext("%{name}", name: user.name), + route: Routes.user_show_path(socket, :show, user_handle) + } + ] + + {:noreply, + socket + |> assign(:page_title, user.name) + |> assign(:current_page, :profile) + |> assign(:breadcrumb_entries, entries) + |> assign(:user, user)} + end +end diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex new file mode 100644 index 000000000..3c340a1b9 --- /dev/null +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -0,0 +1,12 @@ +
+
+ + + <%= Atomic.Accounts.extract_initials(@user.name) %> + + +
+

<%= @user.name %>

+

@<%= @user.handle %>

+

<%= @user.email %>

+
\ No newline at end of file diff --git a/lib/atomic_web/router.ex b/lib/atomic_web/router.ex index b9993ec15..7188c9cbf 100644 --- a/lib/atomic_web/router.ex +++ b/lib/atomic_web/router.ex @@ -36,6 +36,8 @@ defmodule AtomicWeb.Router do live "/organizations", OrganizationLive.Index, :index live "/organizations/:organization_id", OrganizationLive.Show, :show + live "/profile/:handle", UserLive.Show, :show + scope "/organizations/:organization_id" do live "/board/", BoardLive.Index, :index live "/board/:id", BoardLive.Show, :show diff --git a/lib/atomic_web/templates/layout/live.html.heex b/lib/atomic_web/templates/layout/live.html.heex index 12c3c873d..630fec9d3 100644 --- a/lib/atomic_web/templates/layout/live.html.heex +++ b/lib/atomic_web/templates/layout/live.html.heex @@ -201,7 +201,7 @@ Calendar <% end %> - <%= live_redirect to: Routes.user_settings_path(@socket, :edit), class: "bg-zinc-200 flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-gray-900" do %> + <%= live_redirect to: Routes.user_show_path(@socket, :show, @current_user.handle), class: "bg-zinc-200 flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-gray-900" do %> <%= Atomic.Accounts.extract_initials(@current_user.name) %> @@ -220,7 +220,7 @@ <%= if @current_organization do %>
  • - <%= live_redirect to: Routes.activity_index_path(@socket, :index, @current_organization), class: "text-zinc-400 hover:text-zinc-500" do %> + <%= live_redirect to: Routes.home_index_path(@socket, :index), class: "text-zinc-400 hover:text-zinc-500" do %> From f952cf056bdea33f5a3e942f234c8f2dbc7db4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 15:31:19 +0100 Subject: [PATCH 03/12] Add unique constraint to handle column --- .../repo/migrations/20221014155230_create_users_auth_tables.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs index 7149ca795..467ff9edf 100644 --- a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs +++ b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs @@ -21,7 +21,7 @@ defmodule Atomic.Repo.Migrations.CreateUsersAuthTables do timestamps() end - create unique_index(:users, [:email]) + create unique_index(:users, [:email, :handle]) create table(:users_tokens, primary_key: false) do add :id, :binary_id, primary_key: true From c2253148b66c9018a3d30f97c858b5c0fa689784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 17:31:48 +0100 Subject: [PATCH 04/12] Profile page --- .../live/membership_live/index.html.heex | 6 +- lib/atomic_web/live/user_live/show.ex | 5 +- lib/atomic_web/live/user_live/show.html.heex | 61 ++++++++++++++++--- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/lib/atomic_web/live/membership_live/index.html.heex b/lib/atomic_web/live/membership_live/index.html.heex index 96955f2a1..e5c3b1785 100644 --- a/lib/atomic_web/live/membership_live/index.html.heex +++ b/lib/atomic_web/live/membership_live/index.html.heex @@ -30,7 +30,11 @@ <%= for membership <- @memberships do %> <%= membership.number %> - <%= membership.user.name %> + + <%= live_redirect to: Routes.user_show_path(@socket, :show, membership.user.handle) do %> + <%= membership.user.name %> + <% end %> + <%= membership.user.email %> Phone number <%= display_date(membership.inserted_at) %> <%= display_time(membership.inserted_at) %> diff --git a/lib/atomic_web/live/user_live/show.ex b/lib/atomic_web/live/user_live/show.ex index 2aed9308a..a0ab39fb3 100644 --- a/lib/atomic_web/live/user_live/show.ex +++ b/lib/atomic_web/live/user_live/show.ex @@ -12,6 +12,8 @@ defmodule AtomicWeb.UserLive.Show do def handle_params(%{"handle" => user_handle}, _, socket) do user = Accounts.get_user_by_handle(user_handle) + organizations = Accounts.get_user_organizations(user) + entries = [ %{ name: gettext("%{name}", name: user.name), @@ -24,6 +26,7 @@ defmodule AtomicWeb.UserLive.Show do |> assign(:page_title, user.name) |> assign(:current_page, :profile) |> assign(:breadcrumb_entries, entries) - |> assign(:user, user)} + |> assign(:user, user) + |> assign(:organizations, organizations)} end end diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex index 3c340a1b9..c97d139c7 100644 --- a/lib/atomic_web/live/user_live/show.html.heex +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -1,12 +1,53 @@
    -
    - - - <%= Atomic.Accounts.extract_initials(@user.name) %> - - +
    +
    +
    +

    + <%= @user.name %> +

    +

    @<%= @user.handle %>

    +
    +
    + + + <%= Atomic.Accounts.extract_initials(@user.name) %> + + +
    +
    + +

    + <%= gettext("Organizations") %> +

    + +
    + <%= for {organization, index} <- Enum.with_index(@organizations) do %> + <%= live_redirect to: Routes.organization_show_path(@socket, :show, organization) do %> +
    +
    + <%= if organization.logo do %> + + + + <% else %> + + + <%= Atomic.Accounts.extract_initials(organization.name) %> + + + <% end %> +
    +
    + + <%= organization.name %> + + + <%= Atomic.Organizations.get_role(@user.id, organization.id) %> + +
    +
    + <% end %> + <% end %> +
    -

    <%= @user.name %>

    -

    @<%= @user.handle %>

    -

    <%= @user.email %>

    -
    \ No newline at end of file +
    From 01042e91bd6d453edfa30e7b24818538cd3973dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 17:38:36 +0100 Subject: [PATCH 05/12] Follow suggestion regarding user table constraints --- .../migrations/20221014155230_create_users_auth_tables.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs index 467ff9edf..ec56d9826 100644 --- a/priv/repo/migrations/20221014155230_create_users_auth_tables.exs +++ b/priv/repo/migrations/20221014155230_create_users_auth_tables.exs @@ -21,7 +21,8 @@ defmodule Atomic.Repo.Migrations.CreateUsersAuthTables do timestamps() end - create unique_index(:users, [:email, :handle]) + create unique_index(:users, [:email]) + create unique_index(:users, [:handle]) create table(:users_tokens, primary_key: false) do add :id, :binary_id, primary_key: true From 1ecf979efa991aa0cc46083931404612afc3d370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 17:47:45 +0100 Subject: [PATCH 06/12] Fix tests and run formatter --- lib/atomic/accounts/user.ex | 4 +- lib/atomic_web/live/user_live/show.ex | 12 +-- lib/atomic_web/live/user_live/show.html.heex | 96 ++++++++++---------- priv/repo/seeds/accounts.exs | 2 +- test/atomic/accounts_test.exs | 2 +- test/support/factories/accounts_factory.ex | 1 + 6 files changed, 60 insertions(+), 57 deletions(-) diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index f230a8dab..80fd0c87d 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -84,7 +84,9 @@ defmodule Atomic.Accounts.User do defp validate_handle(changeset) do changeset |> validate_required([:handle]) - |> validate_format(:handle, ~r/^[a-z0-9_]+$/, message: "must only contain lowercase characters, numbers, and underscores") + |> validate_format(:handle, ~r/^[a-z0-9_]+$/, + message: "must only contain lowercase characters, numbers, and underscores" + ) |> validate_length(:handle, min: 3, max: 30) |> unsafe_validate_unique(:handle, Atomic.Repo) |> unique_constraint(:handle) diff --git a/lib/atomic_web/live/user_live/show.ex b/lib/atomic_web/live/user_live/show.ex index a0ab39fb3..ceade3f3c 100644 --- a/lib/atomic_web/live/user_live/show.ex +++ b/lib/atomic_web/live/user_live/show.ex @@ -22,11 +22,11 @@ defmodule AtomicWeb.UserLive.Show do ] {:noreply, - socket - |> assign(:page_title, user.name) - |> assign(:current_page, :profile) - |> assign(:breadcrumb_entries, entries) - |> assign(:user, user) - |> assign(:organizations, organizations)} + socket + |> assign(:page_title, user.name) + |> assign(:current_page, :profile) + |> assign(:breadcrumb_entries, entries) + |> assign(:user, user) + |> assign(:organizations, organizations)} end end diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex index c97d139c7..6a3f33238 100644 --- a/lib/atomic_web/live/user_live/show.html.heex +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -1,53 +1,53 @@
    -
    -
    -
    -

    - <%= @user.name %> -

    -

    @<%= @user.handle %>

    -
    -
    - - - <%= Atomic.Accounts.extract_initials(@user.name) %> - - -
    -
    +
    +
    +
    +

    + <%= @user.name %> +

    +

    @<%= @user.handle %>

    +
    +
    + + + <%= Atomic.Accounts.extract_initials(@user.name) %> + + +
    +
    -

    - <%= gettext("Organizations") %> -

    +

    + <%= gettext("Organizations") %> +

    -
    - <%= for {organization, index} <- Enum.with_index(@organizations) do %> - <%= live_redirect to: Routes.organization_show_path(@socket, :show, organization) do %> -
    -
    - <%= if organization.logo do %> - - - - <% else %> - - - <%= Atomic.Accounts.extract_initials(organization.name) %> - - - <% end %> -
    -
    - - <%= organization.name %> - - - <%= Atomic.Organizations.get_role(@user.id, organization.id) %> - -
    -
    - <% end %> - <% end %> -
    +
    + <%= for {organization, index} <- Enum.with_index(@organizations) do %> + <%= live_redirect to: Routes.organization_show_path(@socket, :show, organization) do %> +
    +
    + <%= if organization.logo do %> + + + + <% else %> + + + <%= Atomic.Accounts.extract_initials(organization.name) %> + + + <% end %> +
    +
    + + <%= organization.name %> + + + <%= Atomic.Organizations.get_role(@user.id, organization.id) %> + +
    +
    + <% end %> + <% end %>
    +
    diff --git a/priv/repo/seeds/accounts.exs b/priv/repo/seeds/accounts.exs index 1b64737da..7e06fd0db 100644 --- a/priv/repo/seeds/accounts.exs +++ b/priv/repo/seeds/accounts.exs @@ -111,7 +111,7 @@ defmodule Atomic.Repo.Seeds.Accounts do for character <- characters do email = (character |> String.downcase() |> String.replace(~r/\s*/, "")) <> "@mail.pt" - handle = (character |> String.downcase() |> String.replace(~r/\s/, "_")) + handle = character |> String.downcase() |> String.replace(~r/\s/, "_") user = %{ "name" => character, diff --git a/test/atomic/accounts_test.exs b/test/atomic/accounts_test.exs index 5ea1c2fe9..15f8520ee 100644 --- a/test/atomic/accounts_test.exs +++ b/test/atomic/accounts_test.exs @@ -93,7 +93,7 @@ defmodule Atomic.AccountsTest do describe "change_user_registration/2" do test "returns a changeset" do assert %Ecto.Changeset{} = changeset = Accounts.change_user_registration(%User{}) - assert changeset.required == [:password, :email] + assert changeset.required == [:password, :handle, :email] end test "allows fields to be set" do diff --git a/test/support/factories/accounts_factory.ex b/test/support/factories/accounts_factory.ex index edef51bf5..f53abebc3 100644 --- a/test/support/factories/accounts_factory.ex +++ b/test/support/factories/accounts_factory.ex @@ -13,6 +13,7 @@ defmodule Atomic.Factories.AccountFactory do %User{ name: Faker.Person.name(), email: Faker.Internet.email(), + handle: Faker.Internet.user_name(), role: Enum.random(@roles), hashed_password: Bcrypt.hash_pwd_salt("password1234") } From bb8e984d006e334cfb94e8feca0dd3c71eea5da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 20:03:36 +0100 Subject: [PATCH 07/12] Fix and add tests, change handle requirements --- lib/atomic/accounts/user.ex | 4 ++-- test/atomic/accounts_test.exs | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index 80fd0c87d..2253c993b 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -84,8 +84,8 @@ defmodule Atomic.Accounts.User do defp validate_handle(changeset) do changeset |> validate_required([:handle]) - |> validate_format(:handle, ~r/^[a-z0-9_]+$/, - message: "must only contain lowercase characters, numbers, and underscores" + |> validate_format(:handle, ~r/^[a-zA-Z0-9_.]+$/, + message: "must only contain alphanumeric characters, numbers, underscores and periods" ) |> validate_length(:handle, min: 3, max: 30) |> unsafe_validate_unique(:handle, Atomic.Repo) diff --git a/test/atomic/accounts_test.exs b/test/atomic/accounts_test.exs index 15f8520ee..0e934e6ee 100644 --- a/test/atomic/accounts_test.exs +++ b/test/atomic/accounts_test.exs @@ -53,19 +53,27 @@ defmodule Atomic.AccountsTest do } = errors_on(changeset) end - test "validates email and password when given" do - {:error, changeset} = Accounts.register_user(%{email: "not valid", password: "not valid"}) + test "validates email, handle and password when given" do + {:error, changeset} = + Accounts.register_user(%{email: "not valid", handle: "not valid", password: "not valid"}) assert %{ email: ["must have the @ sign and no spaces"], + handle: [ + "must only contain alphanumeric characters, numbers, underscores and periods" + ], password: ["should be at least 12 character(s)"] } = errors_on(changeset) end - test "validates maximum values for email and password for security" do + test "validates maximum values for email, handle and password for security" do too_long = String.duplicate("db", 100) - {:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long}) + + {:error, changeset} = + Accounts.register_user(%{email: too_long, handle: too_long, password: too_long}) + assert "should be at most 160 character(s)" in errors_on(changeset).email + assert "should be at most 30 character(s)" in errors_on(changeset).handle assert "should be at most 72 character(s)" in errors_on(changeset).password end @@ -84,6 +92,7 @@ defmodule Atomic.AccountsTest do {:ok, user} = Accounts.register_user(user_attrs) assert user.email == user_attrs.email + assert user.handle == user_attrs.handle assert is_binary(user.hashed_password) assert is_nil(user.confirmed_at) assert is_nil(user.password) @@ -99,11 +108,12 @@ defmodule Atomic.AccountsTest do test "allows fields to be set" do email = Faker.Internet.email() password = valid_user_password() + handle = Faker.Internet.user_name() changeset = Accounts.change_user_registration( %User{}, - params_for(:user, %{email: email, password: password}) + params_for(:user, %{email: email, handle: handle, password: password}) ) assert changeset.valid? From 3f07c1aa92cd258f9be6df7f62578407aa51fa17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Thu, 17 Aug 2023 22:52:51 +0100 Subject: [PATCH 08/12] Implement suggestions --- lib/atomic/accounts/user.ex | 5 ++++- lib/atomic_web/templates/user_registration/new.html.heex | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index 2253c993b..7974ace7e 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -85,7 +85,10 @@ defmodule Atomic.Accounts.User do changeset |> validate_required([:handle]) |> validate_format(:handle, ~r/^[a-zA-Z0-9_.]+$/, - message: "must only contain alphanumeric characters, numbers, underscores and periods" + message: + Gettext.gettext( + "must only contain alphanumeric characters, numbers, underscores and periods" + ) ) |> validate_length(:handle, min: 3, max: 30) |> unsafe_validate_unique(:handle, Atomic.Repo) diff --git a/lib/atomic_web/templates/user_registration/new.html.heex b/lib/atomic_web/templates/user_registration/new.html.heex index 28e0ee17b..73c5c0923 100644 --- a/lib/atomic_web/templates/user_registration/new.html.heex +++ b/lib/atomic_web/templates/user_registration/new.html.heex @@ -17,6 +17,14 @@ ) %> <%= error_tag(f, :email) %> + <%= label(f, :handle, class: "sr-only") %> + <%= text_input(f, :handle, + required: true, + placeholder: gettext("Username"), + class: "mt-1 relative block lg:w-96 w-full appearance-none rounded border border-zinc-300 px-3 py-2 text-zinc-900 placeholder-zinc-500 focus:z-10 focus:border-indigo-400 focus:outline-none sm:text-sm" + ) %> + <%= error_tag(f, :handle) %> + <%= label(f, :password, class: "sr-only") %> <%= password_input(f, :password, required: true, From 9ac0cba3ba53f90dc33de1470193b400c808c5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Fri, 18 Aug 2023 19:35:46 +0100 Subject: [PATCH 09/12] Add unit tests --- lib/atomic/accounts.ex | 13 +++++++++++++ lib/atomic/accounts/user.ex | 15 +++++++++++++++ test/atomic/accounts_test.exs | 17 +++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index edee60d24..e33ddb211 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -213,6 +213,19 @@ defmodule Atomic.Accounts do User.email_changeset(user, attrs) end + @doc """ + Returns an `%Ecto.Changeset{}` for changing the user handle. + + ## Examples + + iex> change_user_handle(user) + %Ecto.Changeset{data: %User{}} + + """ + def change_user_handle(user, attrs \\ %{}) do + User.handle_changeset(user, attrs) + end + @doc """ Emulates that the email will change without actually changing it in the database. diff --git a/lib/atomic/accounts/user.ex b/lib/atomic/accounts/user.ex index 7974ace7e..577d6c9b9 100644 --- a/lib/atomic/accounts/user.ex +++ b/lib/atomic/accounts/user.ex @@ -135,6 +135,21 @@ defmodule Atomic.Accounts.User do end end + @doc """ + A user changeset for changing the handle. + + It requires the handle to change otherwise an error is added. + """ + def handle_changeset(user, attrs) do + user + |> cast(attrs, [:handle]) + |> validate_handle() + |> case do + %{changes: %{handle: _}} = changeset -> changeset + %{} = changeset -> add_error(changeset, :handle, "did not change") + end + end + @doc """ A user changeset for changing the password. diff --git a/test/atomic/accounts_test.exs b/test/atomic/accounts_test.exs index 0e934e6ee..b662d1f79 100644 --- a/test/atomic/accounts_test.exs +++ b/test/atomic/accounts_test.exs @@ -87,6 +87,16 @@ defmodule Atomic.AccountsTest do assert "has already been taken" in errors_on(changeset).email end + test "validates handle uniqueness" do + %{handle: handle} = insert(:user) + {:error, changeset} = Accounts.register_user(%{handle: handle}) + assert "has already been taken" in errors_on(changeset).handle + + # Now try with the upper cased handle too, to check that handle case is ignored. + {:error, changeset} = Accounts.register_user(%{handle: String.upcase(handle)}) + assert "has already been taken" in errors_on(changeset).handle + end + test "registers users with a hashed password" do user_attrs = params_for(:user) |> Map.put(:password, valid_user_password()) {:ok, user} = Accounts.register_user(user_attrs) @@ -130,6 +140,13 @@ defmodule Atomic.AccountsTest do end end + describe "change_user_handle/2" do + test "returns a user changeset" do + assert %Ecto.Changeset{} = changeset = Accounts.change_user_handle(%User{}) + assert changeset.required == [:handle] + end + end + describe "apply_user_email/3" do setup do %{user: insert(:user)} From bebbb020c9fbae4849c665321829e0dd3f693bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Sat, 19 Aug 2023 15:31:01 +0100 Subject: [PATCH 10/12] Add user settings button to profile page --- lib/atomic_web/live/user_live/show.ex | 6 +++++- lib/atomic_web/live/user_live/show.html.heex | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/atomic_web/live/user_live/show.ex b/lib/atomic_web/live/user_live/show.ex index ceade3f3c..5aba3fefd 100644 --- a/lib/atomic_web/live/user_live/show.ex +++ b/lib/atomic_web/live/user_live/show.ex @@ -12,6 +12,8 @@ defmodule AtomicWeb.UserLive.Show do def handle_params(%{"handle" => user_handle}, _, socket) do user = Accounts.get_user_by_handle(user_handle) + is_current_user = Map.has_key?(socket.assigns, :current_user) and socket.assigns.current_user.id == user.id + organizations = Accounts.get_user_organizations(user) entries = [ @@ -27,6 +29,8 @@ defmodule AtomicWeb.UserLive.Show do |> assign(:current_page, :profile) |> assign(:breadcrumb_entries, entries) |> assign(:user, user) - |> assign(:organizations, organizations)} + |> assign(:organizations, organizations) + |> assign(:is_current_user, is_current_user) + } end end diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex index 6a3f33238..a09228563 100644 --- a/lib/atomic_web/live/user_live/show.html.heex +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -2,9 +2,18 @@
    -

    - <%= @user.name %> -

    +
    +

    + <%= @user.name %> +

    + <%= if @is_current_user do %> + <%= live_redirect to: Routes.user_settings_path(@socket, :edit) do %> +
    + +
    + <% end %> + <% end %> +

    @<%= @user.handle %>

    From 45d37a7c505c45aa904c13f39061f6873669f60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Sat, 19 Aug 2023 15:34:19 +0100 Subject: [PATCH 11/12] Run formatter --- lib/atomic_web/live/user_live/show.ex | 6 +++--- lib/atomic_web/live/user_live/show.html.heex | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/atomic_web/live/user_live/show.ex b/lib/atomic_web/live/user_live/show.ex index 5aba3fefd..70343455a 100644 --- a/lib/atomic_web/live/user_live/show.ex +++ b/lib/atomic_web/live/user_live/show.ex @@ -12,7 +12,8 @@ defmodule AtomicWeb.UserLive.Show do def handle_params(%{"handle" => user_handle}, _, socket) do user = Accounts.get_user_by_handle(user_handle) - is_current_user = Map.has_key?(socket.assigns, :current_user) and socket.assigns.current_user.id == user.id + is_current_user = + Map.has_key?(socket.assigns, :current_user) and socket.assigns.current_user.id == user.id organizations = Accounts.get_user_organizations(user) @@ -30,7 +31,6 @@ defmodule AtomicWeb.UserLive.Show do |> assign(:breadcrumb_entries, entries) |> assign(:user, user) |> assign(:organizations, organizations) - |> assign(:is_current_user, is_current_user) - } + |> assign(:is_current_user, is_current_user)} end end diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex index a09228563..d916b9dab 100644 --- a/lib/atomic_web/live/user_live/show.html.heex +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -9,7 +9,7 @@ <%= if @is_current_user do %> <%= live_redirect to: Routes.user_settings_path(@socket, :edit) do %>
    - +
    <% end %> <% end %> From 50ad3e24b6ef09762ed513a636d4ede3c740515c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lobo?= Date: Sat, 19 Aug 2023 16:44:00 +0100 Subject: [PATCH 12/12] Update edit button --- lib/atomic_web/live/user_live/show.html.heex | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/atomic_web/live/user_live/show.html.heex b/lib/atomic_web/live/user_live/show.html.heex index d916b9dab..6f60411c1 100644 --- a/lib/atomic_web/live/user_live/show.html.heex +++ b/lib/atomic_web/live/user_live/show.html.heex @@ -6,13 +6,6 @@

    <%= @user.name %>

    - <%= if @is_current_user do %> - <%= live_redirect to: Routes.user_settings_path(@socket, :edit) do %> -
    - -
    - <% end %> - <% end %>

    @<%= @user.handle %>

    @@ -58,5 +51,12 @@ <% end %> <% end %>
    + <%= if @is_current_user do %> +
    + <%= live_patch to: Routes.user_settings_path(@socket, :edit), class: "inline-flex px-6 py-2 text-sm font-medium text-orange-500 bg-white border-2 border-orange-500 rounded-md shadow-sm hover:bg-orange-500 hover:text-white" do %> + Edit + <% end %> +
    + <% end %>