diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 5679bbf1d..e33ddb211 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. @@ -197,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 a2ec48297..577d6c9b9 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,20 @@ defmodule Atomic.Accounts.User do |> unique_constraint(:email) end + defp validate_handle(changeset) do + changeset + |> validate_required([:handle]) + |> validate_format(:handle, ~r/^[a-zA-Z0-9_.]+$/, + 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) + |> unique_constraint(:handle) + end + defp validate_password(changeset, opts) do changeset |> validate_required([:password]) @@ -119,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/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 %>
@<%= @user.handle %>
+