forked from shibao/cannery
add accounts doctests
This commit is contained in:
parent
6dbadc58ae
commit
737484c36e
@ -16,14 +16,15 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_user_by_email("foo@example.com")
|
||||
%User{}
|
||||
iex> register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %User{} <- get_user_by_email("foo@example.com"), do: :passed
|
||||
:passed
|
||||
|
||||
iex> get_user_by_email("unknown@example.com")
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec get_user_by_email(String.t()) :: User.t() | nil
|
||||
@spec get_user_by_email(email :: String.t()) :: User.t() | nil
|
||||
def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email)
|
||||
|
||||
@doc """
|
||||
@ -31,14 +32,15 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_user_by_email_and_password("foo@example.com", "correct_password")
|
||||
%User{}
|
||||
iex> register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %User{} <- get_user_by_email_and_password("foo@example.com", "valid_password"), do: :passed
|
||||
:passed
|
||||
|
||||
iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec get_user_by_email_and_password(String.t(), String.t()) ::
|
||||
@spec get_user_by_email_and_password(email :: String.t(), password :: String.t()) ::
|
||||
User.t() | nil
|
||||
def get_user_by_email_and_password(email, password)
|
||||
when is_binary(email) and is_binary(password) do
|
||||
@ -53,10 +55,11 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_user!(123)
|
||||
%User{}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> get_user!(user.id)
|
||||
user
|
||||
|
||||
iex> get_user!(456)
|
||||
> get_user!()
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@ -68,13 +71,15 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_users_by_role(%User{id: 123, role: :admin})
|
||||
[admin: [%User{}], user: [%User{}, %User{}]]
|
||||
iex> {:ok, user1} = register_user(%{email: "foo1@example.com", password: "valid_password"})
|
||||
iex> {:ok, user2} = register_user(%{email: "foo2@example.com", password: "valid_password"})
|
||||
iex> with %{admin: [^user1], user: [^user2]} <- list_all_users_by_role(user1), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec list_all_users_by_role(User.t()) :: %{String.t() => [User.t()]}
|
||||
@spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]}
|
||||
def list_all_users_by_role(%User{role: :admin}) do
|
||||
Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn user -> user.role end)
|
||||
Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn %{role: role} -> role end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -82,13 +87,13 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_users_by_role(%User{id: 123, role: :admin})
|
||||
[%User{}]
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with [^user] <- list_users_by_role(:admin), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec list_users_by_role(:admin | :user) :: [User.t()]
|
||||
def list_users_by_role(role) do
|
||||
role = role |> to_string()
|
||||
@spec list_users_by_role(:admin) :: [User.t()]
|
||||
def list_users_by_role(:admin = role) do
|
||||
Repo.all(from u in User, where: u.role == ^role, order_by: u.email)
|
||||
end
|
||||
|
||||
@ -99,22 +104,30 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> register_user(%{field: value})
|
||||
{:ok, %User{}}
|
||||
iex> with {:ok, %User{email: "foo@example.com"}} <-
|
||||
...> register_user(%{email: "foo@example.com", password: "valid_password"}),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
iex> register_user(%{field: bad_value})
|
||||
{:error, %Changeset{}}
|
||||
iex> with {:error, %Changeset{}} <- register_user(%{email: "foo@example"}), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec register_user(map()) :: {:ok, User.t()} | {:error, Changeset.t(User.new_user())}
|
||||
@spec register_user(attrs :: map()) :: {:ok, User.t()} | {:error, User.changeset()}
|
||||
def register_user(attrs) do
|
||||
# if no registered users, make first user an admin
|
||||
role =
|
||||
if Repo.one!(from u in User, select: count(u.id), distinct: true) == 0,
|
||||
do: "admin",
|
||||
else: "user"
|
||||
Multi.new()
|
||||
|> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true))
|
||||
|> Multi.insert(:add_user, fn %{users_count: count} ->
|
||||
# if no registered users, make first user an admin
|
||||
role = if count == 0, do: :admin, else: :user
|
||||
|
||||
%User{} |> User.registration_changeset(attrs |> Map.put("role", role)) |> Repo.insert()
|
||||
User.registration_changeset(attrs) |> User.role_changeset(role)
|
||||
end)
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
{:ok, %{add_user: user}} -> {:ok, user}
|
||||
{:error, :add_user, changeset, _changes_so_far} -> {:error, changeset}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -122,16 +135,17 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_registration(user)
|
||||
%Changeset{data: %User{}}
|
||||
iex> with %Changeset{} <- change_user_registration(), do: :passed
|
||||
:passed
|
||||
|
||||
iex> with %Changeset{} <- change_user_registration(%{password: "hi"}), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec change_user_registration(User.t() | User.new_user()) ::
|
||||
Changeset.t(User.t() | User.new_user())
|
||||
@spec change_user_registration(User.t() | User.new_user(), map()) ::
|
||||
Changeset.t(User.t() | User.new_user())
|
||||
def change_user_registration(user, attrs \\ %{}),
|
||||
do: User.registration_changeset(user, attrs, hash_password: false)
|
||||
@spec change_user_registration() :: User.changeset()
|
||||
@spec change_user_registration(attrs :: map()) :: User.changeset()
|
||||
def change_user_registration(attrs \\ %{}),
|
||||
do: User.registration_changeset(attrs, hash_password: false)
|
||||
|
||||
## Settings
|
||||
|
||||
@ -140,11 +154,12 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_email(user)
|
||||
%Changeset{data: %User{}}
|
||||
iex> with %Changeset{} <- change_user_email(%User{email: "foo@example.com"}), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec change_user_email(User.t(), map()) :: Changeset.t(User.t())
|
||||
@spec change_user_email(User.t()) :: User.changeset()
|
||||
@spec change_user_email(User.t(), attrs :: map()) :: User.changeset()
|
||||
def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs)
|
||||
|
||||
@doc """
|
||||
@ -152,11 +167,11 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_role(user)
|
||||
%Changeset{data: %User{}}
|
||||
iex> with %Changeset{} <- change_user_role(%User{}, :user), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec change_user_role(User.t(), atom()) :: Changeset.t(User.t())
|
||||
@spec change_user_role(User.t(), User.role()) :: User.changeset()
|
||||
def change_user_role(user, role), do: User.role_changeset(user, role)
|
||||
|
||||
@doc """
|
||||
@ -165,15 +180,21 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> apply_user_email(user, "valid password", %{email: ...})
|
||||
{:ok, %User{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:ok, %User{}} <-
|
||||
...> apply_user_email(user, "valid_password", %{email: "new_email@account.com"}),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
iex> apply_user_email(user, "invalid password", %{email: ...})
|
||||
{:error, %Changeset{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:error, %Changeset{}} <-
|
||||
...> apply_user_email(user, "invalid password", %{email: "new_email@account"}),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec apply_user_email(User.t(), String.t(), map()) ::
|
||||
{:ok, User.t()} | {:error, Changeset.t(User.t())}
|
||||
@spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) ::
|
||||
{:ok, User.t()} | {:error, User.changeset()}
|
||||
def apply_user_email(user, password, attrs) do
|
||||
user
|
||||
|> User.email_changeset(attrs)
|
||||
@ -187,7 +208,7 @@ defmodule Lokal.Accounts do
|
||||
If the token matches, the user email is updated and the token is deleted.
|
||||
The confirmed_at date is also updated to the current time.
|
||||
"""
|
||||
@spec update_user_email(User.t(), String.t()) :: :ok | :error
|
||||
@spec update_user_email(User.t(), token :: String.t()) :: :ok | :error
|
||||
def update_user_email(user, token) do
|
||||
context = "change:#{user.email}"
|
||||
|
||||
@ -196,11 +217,11 @@ defmodule Lokal.Accounts do
|
||||
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
|
||||
:ok
|
||||
else
|
||||
_ -> :error
|
||||
_error_tuple -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@spec user_email_multi(User.t(), String.t(), String.t()) :: Multi.t()
|
||||
@spec user_email_multi(User.t(), email :: String.t(), context :: String.t()) :: Multi.t()
|
||||
defp user_email_multi(user, email, context) do
|
||||
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
|
||||
|
||||
@ -214,11 +235,16 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1))
|
||||
{:ok, %{to: ..., body: ...}}
|
||||
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %Oban.Job{
|
||||
...> args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}
|
||||
...> } <- deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec deliver_update_email_instructions(User.t(), String.t(), function) :: Job.t()
|
||||
@spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) ::
|
||||
Job.t()
|
||||
def deliver_update_email_instructions(user, current_email, update_email_url_fun)
|
||||
when is_function(update_email_url_fun, 1) do
|
||||
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
|
||||
@ -231,11 +257,11 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_password(user)
|
||||
%Changeset{data: %User{}}
|
||||
iex> with %Changeset{} <- change_user_password(%User{}), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec change_user_password(User.t(), map()) :: Changeset.t(User.t())
|
||||
@spec change_user_password(User.t(), attrs :: map()) :: User.changeset()
|
||||
def change_user_password(user, attrs \\ %{}),
|
||||
do: User.password_changeset(user, attrs, hash_password: false)
|
||||
|
||||
@ -244,15 +270,24 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_user_password(user, "valid password", %{password: ...})
|
||||
{:ok, %User{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:ok, %User{}} <-
|
||||
...> reset_user_password(user, %{
|
||||
...> password: "new password",
|
||||
...> password_confirmation: "new password"
|
||||
...> }),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
iex> update_user_password(user, "invalid password", %{password: ...})
|
||||
{:error, %Changeset{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:error, %Changeset{}} <-
|
||||
...> update_user_password(user, "invalid password", %{password: "123"}),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec update_user_password(User.t(), String.t(), map()) ::
|
||||
{:ok, User.t()} | {:error, Changeset.t(User.t())}
|
||||
@spec update_user_password(User.t(), String.t(), attrs :: map()) ::
|
||||
{:ok, User.t()} | {:error, User.changeset()}
|
||||
def update_user_password(user, password, attrs) do
|
||||
changeset =
|
||||
user
|
||||
@ -265,20 +300,20 @@ defmodule Lokal.Accounts do
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
{:ok, %{user: user}} -> {:ok, user}
|
||||
{:error, :user, changeset, _} -> {:error, changeset}
|
||||
{:error, :user, changeset, _changes_so_far} -> {:error, changeset}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for changing the user locale.
|
||||
Returns an `Ecto.Changeset.t()` for changing the user locale.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_locale(user)
|
||||
%Changeset{data: %User{}}
|
||||
iex> with %Changeset{} <- change_user_locale(%User{}), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec change_user_locale(User.t()) :: Changeset.t(User.t())
|
||||
@spec change_user_locale(User.t()) :: User.changeset()
|
||||
def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale)
|
||||
|
||||
@doc """
|
||||
@ -286,15 +321,13 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_user_locale(user, "valid locale")
|
||||
{:ok, %User{}}
|
||||
|
||||
iex> update_user_password(user, "invalid locale")
|
||||
{:error, %Changeset{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:ok, %User{}} <- update_user_locale(user, "en_US"), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec update_user_locale(User.t(), locale :: String.t()) ::
|
||||
{:ok, User.t()} | {:error, Changeset.t(User.t())}
|
||||
{:ok, User.t()} | {:error, User.changeset()}
|
||||
def update_user_locale(user, locale),
|
||||
do: user |> User.locale_changeset(locale) |> Repo.update()
|
||||
|
||||
@ -303,14 +336,16 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_user!(user_to_delete, %User{id: 123, role: :admin})
|
||||
%User{}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %User{} <- delete_user!(user, %User{id: 123, role: :admin}), do: :passed
|
||||
:passed
|
||||
|
||||
iex> delete_user!(%User{id: 123}, %User{id: 123})
|
||||
%User{}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %User{} <- delete_user!(user, user), do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec delete_user!(User.t(), User.t()) :: User.t()
|
||||
@spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t()
|
||||
def delete_user!(user, %User{role: :admin}), do: user |> Repo.delete!()
|
||||
def delete_user!(%User{id: user_id} = user, %User{id: user_id}), do: user |> Repo.delete!()
|
||||
|
||||
@ -329,7 +364,7 @@ defmodule Lokal.Accounts do
|
||||
@doc """
|
||||
Gets the user with the given signed token.
|
||||
"""
|
||||
@spec get_user_by_session_token(String.t()) :: User.t()
|
||||
@spec get_user_by_session_token(token :: String.t()) :: User.t()
|
||||
def get_user_by_session_token(token) do
|
||||
{:ok, query} = UserToken.verify_session_token_query(token)
|
||||
Repo.one(query)
|
||||
@ -338,7 +373,7 @@ defmodule Lokal.Accounts do
|
||||
@doc """
|
||||
Deletes the signed token with the given context.
|
||||
"""
|
||||
@spec delete_session_token(String.t()) :: :ok
|
||||
@spec delete_session_token(token :: String.t()) :: :ok
|
||||
def delete_session_token(token) do
|
||||
Repo.delete_all(UserToken.token_and_context_query(token, "session"))
|
||||
:ok
|
||||
@ -349,19 +384,45 @@ defmodule Lokal.Accounts do
|
||||
"""
|
||||
@spec allow_registration?() :: boolean()
|
||||
def allow_registration? do
|
||||
Application.get_env(:lokal, LokalWeb.Endpoint)[:registration] == "public" or
|
||||
Application.get_env(:Lokal, LokalWeb.Endpoint)[:registration] == "public" or
|
||||
list_users_by_role(:admin) |> Enum.empty?()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if user is an admin
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> is_admin?(user)
|
||||
true
|
||||
|
||||
iex> is_admin?(%User{id: Ecto.UUID.generate()})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec is_admin?(User.t()) :: boolean()
|
||||
def is_admin?(%User{id: user_id}) do
|
||||
Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin)
|
||||
|> is_nil()
|
||||
Repo.exists?(from u in User, where: u.id == ^user_id, where: u.role == :admin)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks to see if user has the admin role
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> is_already_admin?(user)
|
||||
true
|
||||
|
||||
iex> is_already_admin?(%User{})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec is_already_admin?(User.t() | nil) :: boolean()
|
||||
def is_already_admin?(%User{role: :admin}), do: true
|
||||
def is_already_admin?(_invalid_user), do: false
|
||||
|
||||
## Confirmation
|
||||
|
||||
@doc """
|
||||
@ -369,10 +430,16 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1))
|
||||
{:ok, %{to: ..., body: ...}}
|
||||
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %Oban.Job{
|
||||
...> args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}
|
||||
...> } <- deliver_user_confirmation_instructions(user, fn _token -> "example url" end),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :confirm, &1))
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> user = user |> User.confirm_changeset() |> Repo.update!()
|
||||
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end)
|
||||
{:error, :already_confirmed}
|
||||
|
||||
"""
|
||||
@ -394,14 +461,14 @@ defmodule Lokal.Accounts do
|
||||
If the token matches, the user account is marked as confirmed
|
||||
and the token is deleted.
|
||||
"""
|
||||
@spec confirm_user(String.t()) :: {:ok, User.t()} | atom()
|
||||
@spec confirm_user(token :: String.t()) :: {:ok, User.t()} | :error
|
||||
def confirm_user(token) do
|
||||
with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"),
|
||||
%User{} = user <- Repo.one(query),
|
||||
{:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do
|
||||
{:ok, user}
|
||||
else
|
||||
_ -> :error
|
||||
_error_tuple -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@ -419,8 +486,12 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
|
||||
{:ok, %{to: ..., body: ...}}
|
||||
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with %Oban.Job{args: %{
|
||||
...> email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}
|
||||
...> } <- deliver_user_reset_password_instructions(user, fn _token -> "example url" end),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t()
|
||||
@ -436,20 +507,23 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_user_by_reset_password_token("validtoken")
|
||||
%User{}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> {encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
|
||||
iex> Repo.insert!(user_token)
|
||||
iex> with %User{} <- get_user_by_reset_password_token(encoded_token), do: :passed
|
||||
:passed
|
||||
|
||||
iex> get_user_by_reset_password_token("invalidtoken")
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec get_user_by_reset_password_token(String.t()) :: User.t() | nil
|
||||
@spec get_user_by_reset_password_token(token :: String.t()) :: User.t() | nil
|
||||
def get_user_by_reset_password_token(token) do
|
||||
with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"),
|
||||
%User{} = user <- Repo.one(query) do
|
||||
user
|
||||
else
|
||||
_ -> nil
|
||||
_error_tuple -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@ -458,14 +532,24 @@ defmodule Lokal.Accounts do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
|
||||
{:ok, %User{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:ok, %User{}} <-
|
||||
...> reset_user_password(user, %{
|
||||
...> password: "new password",
|
||||
...> password_confirmation: "new password"
|
||||
...> }),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
|
||||
{:error, %Changeset{}}
|
||||
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
|
||||
iex> with {:error, %Changeset{}} <-
|
||||
...> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}),
|
||||
...> do: :passed
|
||||
:passed
|
||||
|
||||
"""
|
||||
@spec reset_user_password(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t(User.t())}
|
||||
@spec reset_user_password(User.t(), attrs :: map()) ::
|
||||
{:ok, User.t()} | {:error, User.changeset()}
|
||||
def reset_user_password(user, attrs) do
|
||||
Multi.new()
|
||||
|> Multi.update(:user, User.password_changeset(user, attrs))
|
||||
@ -473,7 +557,7 @@ defmodule Lokal.Accounts do
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
{:ok, %{user: user}} -> {:ok, user}
|
||||
{:error, :user, changeset, _} -> {:error, changeset}
|
||||
{:error, :user, changeset, _changes_so_far} -> {:error, changeset}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -9,6 +9,14 @@ defmodule Lokal.Accounts.User do
|
||||
alias Ecto.{Changeset, UUID}
|
||||
alias Lokal.{Accounts.User, Invites.Invite}
|
||||
|
||||
@derive {Jason.Encoder,
|
||||
only: [
|
||||
:id,
|
||||
:email,
|
||||
:confirmed_at,
|
||||
:role,
|
||||
:locale
|
||||
]}
|
||||
@derive {Inspect, except: [:password]}
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
@ -31,14 +39,16 @@ defmodule Lokal.Accounts.User do
|
||||
password: String.t(),
|
||||
hashed_password: String.t(),
|
||||
confirmed_at: NaiveDateTime.t(),
|
||||
role: atom(),
|
||||
invites: [Invite.t()],
|
||||
role: role(),
|
||||
locale: String.t() | nil,
|
||||
invites: [Invite.t()],
|
||||
inserted_at: NaiveDateTime.t(),
|
||||
updated_at: NaiveDateTime.t()
|
||||
}
|
||||
@type new_user :: %User{}
|
||||
@type id :: UUID.t()
|
||||
@type changeset :: Changeset.t(t() | new_user())
|
||||
@type role :: :admin | :user
|
||||
|
||||
@doc """
|
||||
A user changeset for registration.
|
||||
@ -57,26 +67,24 @@ defmodule Lokal.Accounts.User do
|
||||
validations on a LiveView form), this option can be set to `false`.
|
||||
Defaults to `true`.
|
||||
"""
|
||||
@spec registration_changeset(t() | new_user(), attrs :: map()) :: Changeset.t(t() | new_user())
|
||||
@spec registration_changeset(t() | new_user(), attrs :: map(), opts :: keyword()) ::
|
||||
Changeset.t(t() | new_user())
|
||||
def registration_changeset(user, attrs, opts \\ []) do
|
||||
user
|
||||
|> cast(attrs, [:email, :password, :role, :locale])
|
||||
@spec registration_changeset(attrs :: map()) :: changeset()
|
||||
@spec registration_changeset(attrs :: map(), opts :: keyword()) :: changeset()
|
||||
def registration_changeset(attrs, opts \\ []) do
|
||||
%User{}
|
||||
|> cast(attrs, [:email, :password, :locale])
|
||||
|> validate_email()
|
||||
|> validate_password(opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
A user changeset for role.
|
||||
|
||||
"""
|
||||
@spec role_changeset(t(), role :: atom()) :: Changeset.t(t())
|
||||
@spec role_changeset(t() | new_user() | changeset(), role()) :: changeset()
|
||||
def role_changeset(user, role) do
|
||||
user |> cast(%{"role" => role}, [:role])
|
||||
user |> change(role: role)
|
||||
end
|
||||
|
||||
@spec validate_email(Changeset.t(t() | new_user())) :: Changeset.t(t() | new_user())
|
||||
@spec validate_email(changeset()) :: changeset()
|
||||
defp validate_email(changeset) do
|
||||
changeset
|
||||
|> validate_required([:email])
|
||||
@ -88,8 +96,8 @@ defmodule Lokal.Accounts.User do
|
||||
|> unique_constraint(:email)
|
||||
end
|
||||
|
||||
@spec validate_password(Changeset.t(t() | new_user()), opts :: keyword()) ::
|
||||
Changeset.t(t() | new_user())
|
||||
@spec validate_password(changeset(), opts :: keyword()) ::
|
||||
changeset()
|
||||
defp validate_password(changeset, opts) do
|
||||
changeset
|
||||
|> validate_required([:password])
|
||||
@ -100,8 +108,7 @@ defmodule Lokal.Accounts.User do
|
||||
|> maybe_hash_password(opts)
|
||||
end
|
||||
|
||||
@spec maybe_hash_password(Changeset.t(t() | new_user()), opts :: keyword()) ::
|
||||
Changeset.t(t() | new_user())
|
||||
@spec maybe_hash_password(changeset(), opts :: keyword()) :: changeset()
|
||||
defp maybe_hash_password(changeset, opts) do
|
||||
hash_password? = Keyword.get(opts, :hash_password, true)
|
||||
password = get_change(changeset, :password)
|
||||
@ -120,7 +127,7 @@ defmodule Lokal.Accounts.User do
|
||||
|
||||
It requires the email to change otherwise an error is added.
|
||||
"""
|
||||
@spec email_changeset(t(), attrs :: map()) :: Changeset.t(t())
|
||||
@spec email_changeset(t(), attrs :: map()) :: changeset()
|
||||
def email_changeset(user, attrs) do
|
||||
user
|
||||
|> cast(attrs, [:email])
|
||||
@ -143,8 +150,8 @@ defmodule Lokal.Accounts.User do
|
||||
validations on a LiveView form), this option can be set to `false`.
|
||||
Defaults to `true`.
|
||||
"""
|
||||
@spec password_changeset(t(), attrs :: map()) :: Changeset.t(t())
|
||||
@spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: Changeset.t(t())
|
||||
@spec password_changeset(t(), attrs :: map()) :: changeset()
|
||||
@spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: changeset()
|
||||
def password_changeset(user, attrs, opts \\ []) do
|
||||
user
|
||||
|> cast(attrs, [:password])
|
||||
@ -155,7 +162,7 @@ defmodule Lokal.Accounts.User do
|
||||
@doc """
|
||||
Confirms the account by setting `confirmed_at`.
|
||||
"""
|
||||
@spec confirm_changeset(t() | Changeset.t(t())) :: Changeset.t(t())
|
||||
@spec confirm_changeset(t() | changeset()) :: changeset()
|
||||
def confirm_changeset(user_or_changeset) do
|
||||
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||
user_or_changeset |> change(confirmed_at: now)
|
||||
@ -173,7 +180,7 @@ defmodule Lokal.Accounts.User do
|
||||
Bcrypt.verify_pass(password, hashed_password)
|
||||
end
|
||||
|
||||
def valid_password?(_, _) do
|
||||
def valid_password?(_invalid_user, _invalid_password) do
|
||||
Bcrypt.no_user_verify()
|
||||
false
|
||||
end
|
||||
@ -181,7 +188,7 @@ defmodule Lokal.Accounts.User do
|
||||
@doc """
|
||||
Validates the current password otherwise adds an error to the changeset.
|
||||
"""
|
||||
@spec validate_current_password(Changeset.t(t()), String.t()) :: Changeset.t(t())
|
||||
@spec validate_current_password(changeset(), String.t()) :: changeset()
|
||||
def validate_current_password(changeset, password) do
|
||||
if valid_password?(changeset.data, password),
|
||||
do: changeset,
|
||||
@ -191,7 +198,7 @@ defmodule Lokal.Accounts.User do
|
||||
@doc """
|
||||
A changeset for changing the user's locale
|
||||
"""
|
||||
@spec locale_changeset(t() | Changeset.t(t()), locale :: String.t() | nil) :: Changeset.t(t())
|
||||
@spec locale_changeset(t() | changeset(), locale :: String.t() | nil) :: changeset()
|
||||
def locale_changeset(user_or_changeset, locale) do
|
||||
user_or_changeset
|
||||
|> cast(%{"locale" => locale}, [:locale])
|
||||
|
@ -2,7 +2,6 @@ defmodule LokalWeb.UserRegistrationController do
|
||||
use LokalWeb, :controller
|
||||
import LokalWeb.Gettext
|
||||
alias Lokal.{Accounts, Invites}
|
||||
alias Lokal.Accounts.User
|
||||
alias LokalWeb.{Endpoint, HomeLive}
|
||||
|
||||
def new(conn, %{"invite" => invite_token}) do
|
||||
@ -30,7 +29,7 @@ defmodule LokalWeb.UserRegistrationController do
|
||||
# renders new user registration page
|
||||
defp render_new(conn, invite \\ nil) do
|
||||
render(conn, "new.html",
|
||||
changeset: Accounts.change_user_registration(%User{}),
|
||||
changeset: Accounts.change_user_registration(),
|
||||
invite: invite,
|
||||
page_title: gettext("Register")
|
||||
)
|
||||
|
@ -63,7 +63,7 @@ msgstr ""
|
||||
msgid "Reconnecting..."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:35
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:34
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Register"
|
||||
msgstr ""
|
||||
|
@ -63,7 +63,7 @@ msgstr ""
|
||||
msgid "Reconnecting..."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:35
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:34
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Register"
|
||||
msgstr ""
|
||||
|
@ -140,14 +140,14 @@ msgstr ""
|
||||
msgid "Reset password link is invalid or it has expired."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:25
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:56
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:24
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:55
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Sorry, public registration is disabled"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:15
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:46
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:14
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:45
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Sorry, this invite was not found or expired"
|
||||
msgstr ""
|
||||
@ -178,22 +178,22 @@ msgstr ""
|
||||
msgid "You must confirm your account and log in to access this page."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:130
|
||||
#: lib/lokal/accounts/user.ex:137
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "did not change"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:151
|
||||
#: lib/lokal/accounts/user.ex:158
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "does not match password"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:188
|
||||
#: lib/lokal/accounts/user.ex:195
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "is not valid"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:84
|
||||
#: lib/lokal/accounts/user.ex:92
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "must have the @ sign and no spaces"
|
||||
msgstr ""
|
||||
|
@ -60,7 +60,7 @@ msgstr ""
|
||||
msgid "Password updated successfully."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:74
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:73
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Please check your email to verify your account"
|
||||
msgstr ""
|
||||
|
@ -137,14 +137,14 @@ msgstr ""
|
||||
msgid "Reset password link is invalid or it has expired."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:25
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:56
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:24
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:55
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Sorry, public registration is disabled"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:15
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:46
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:14
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:45
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Sorry, this invite was not found or expired"
|
||||
msgstr ""
|
||||
@ -175,22 +175,22 @@ msgstr ""
|
||||
msgid "You must confirm your account and log in to access this page."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:130
|
||||
#: lib/lokal/accounts/user.ex:137
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "did not change"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:151
|
||||
#: lib/lokal/accounts/user.ex:158
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "does not match password"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:188
|
||||
#: lib/lokal/accounts/user.ex:195
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "is not valid"
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal/accounts/user.ex:84
|
||||
#: lib/lokal/accounts/user.ex:92
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "must have the @ sign and no spaces"
|
||||
msgstr ""
|
||||
|
@ -60,7 +60,7 @@ msgstr ""
|
||||
msgid "Password updated successfully."
|
||||
msgstr ""
|
||||
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:74
|
||||
#: lib/lokal_web/controllers/user_registration_controller.ex:73
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Please check your email to verify your account"
|
||||
msgstr ""
|
||||
|
@ -10,6 +10,8 @@ defmodule Lokal.AccountsTest do
|
||||
|
||||
@moduletag :accounts_test
|
||||
|
||||
doctest Accounts, import: true
|
||||
|
||||
describe "get_user_by_email/1" do
|
||||
test "does not return the user if the email does not exist" do
|
||||
refute Accounts.get_user_by_email("unknown@example.com")
|
||||
@ -104,7 +106,7 @@ defmodule Lokal.AccountsTest do
|
||||
|
||||
describe "change_user_registration/2" do
|
||||
test "returns a changeset" do
|
||||
assert %Changeset{} = changeset = Accounts.change_user_registration(%User{})
|
||||
assert %Changeset{} = changeset = Accounts.change_user_registration()
|
||||
assert changeset.required == [:password, :email]
|
||||
end
|
||||
|
||||
@ -112,8 +114,7 @@ defmodule Lokal.AccountsTest do
|
||||
email = unique_user_email()
|
||||
password = valid_user_password()
|
||||
|
||||
changeset =
|
||||
Accounts.change_user_registration(%User{}, %{"email" => email, "password" => password})
|
||||
changeset = Accounts.change_user_registration(%{"email" => email, "password" => password})
|
||||
|
||||
assert changeset.valid?
|
||||
assert get_change(changeset, :email) == email
|
||||
|
@ -3,7 +3,7 @@ defmodule Lokal.Fixtures do
|
||||
This module defines test helpers for creating entities
|
||||
"""
|
||||
|
||||
alias Lokal.{Accounts, Accounts.User, Email}
|
||||
alias Lokal.{Accounts, Accounts.User, Email, Repo}
|
||||
|
||||
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
|
||||
def valid_user_password, do: "hello world!"
|
||||
@ -26,11 +26,12 @@ defmodule Lokal.Fixtures do
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
"email" => unique_user_email(),
|
||||
"password" => valid_user_password(),
|
||||
"role" => "admin"
|
||||
"password" => valid_user_password()
|
||||
})
|
||||
|> Accounts.register_user()
|
||||
|> unwrap_ok_tuple()
|
||||
|> User.role_changeset(:admin)
|
||||
|> Repo.update!()
|
||||
end
|
||||
|
||||
def extract_user_token(fun) do
|
||||
|
Loading…
Reference in New Issue
Block a user