422 lines
12 KiB
Elixir
422 lines
12 KiB
Elixir
defmodule Cannery.Accounts do
|
|
@moduledoc """
|
|
The Accounts context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Cannery.{Repo}
|
|
alias Cannery.Accounts.{User, UserToken, UserNotifier}
|
|
|
|
## Database getters
|
|
|
|
@doc """
|
|
Gets a user by email.
|
|
|
|
## Examples
|
|
|
|
iex> get_user_by_email("foo@example.com")
|
|
%User{}
|
|
|
|
iex> get_user_by_email("unknown@example.com")
|
|
nil
|
|
|
|
"""
|
|
@spec get_user_by_email(String.t()) :: User.t() | nil
|
|
def get_user_by_email(email) when is_binary(email) do
|
|
Repo.get_by(User, email: email)
|
|
end
|
|
|
|
@doc """
|
|
Gets a user by email and password.
|
|
|
|
## Examples
|
|
|
|
iex> get_user_by_email_and_password("foo@example.com", "correct_password")
|
|
%User{}
|
|
|
|
iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
|
|
nil
|
|
|
|
"""
|
|
@spec get_user_by_email_and_password(String.t(), String.t()) ::
|
|
User.t() | nil
|
|
def get_user_by_email_and_password(email, password)
|
|
when is_binary(email) and is_binary(password) do
|
|
user = Repo.get_by(User, email: email)
|
|
if User.valid_password?(user, password), do: user
|
|
end
|
|
|
|
@doc """
|
|
Gets a single user.
|
|
|
|
Raises `Ecto.NoResultsError` if the User does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_user!(123)
|
|
%User{}
|
|
|
|
iex> get_user!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
@spec get_user!(Ecto.UUID.t()) :: User.t()
|
|
def get_user!(id), do: Repo.get!(User, id)
|
|
|
|
@spec list_users_by_role(atom()) :: [User.t()]
|
|
def list_users_by_role(role) do
|
|
Repo.all(from u in User, where: u.role == ^role)
|
|
end
|
|
|
|
@spec list_all_users(boolean()) :: [User.t()]
|
|
def list_all_users(confirmed_users_only \\ true) do
|
|
if confirmed_users_only do
|
|
from u in User, where: u.confirmed_at
|
|
else
|
|
User
|
|
end
|
|
|> Repo.all()
|
|
end
|
|
|
|
## User registration
|
|
|
|
@doc """
|
|
Registers a user.
|
|
|
|
## Examples
|
|
|
|
iex> register_user(%{field: value})
|
|
{:ok, %User{}}
|
|
|
|
iex> register_user(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
@spec register_user(map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
def register_user(attrs) do
|
|
# if no registered users, make first user an admin
|
|
attrs =
|
|
if Repo.one!(from u in User, select: count(u.id), distinct: true) == 0,
|
|
do: attrs |> Map.put("role", "admin"),
|
|
else: attrs
|
|
|
|
%User{} |> User.registration_changeset(attrs) |> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking user changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_user_registration(user)
|
|
%Ecto.Changeset{data: %User{}}
|
|
|
|
"""
|
|
@spec change_user_registration(User.t(), map()) :: Ecto.Changeset.t()
|
|
def change_user_registration(user, attrs \\ %{}) do
|
|
User.registration_changeset(user, attrs, hash_password: false)
|
|
end
|
|
|
|
## Settings
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for changing the user email.
|
|
|
|
## Examples
|
|
|
|
iex> change_user_email(user)
|
|
%Ecto.Changeset{data: %User{}}
|
|
|
|
"""
|
|
@spec change_user_email(User.t(), map()) :: Ecto.Changeset.t()
|
|
def change_user_email(user, attrs \\ %{}) do
|
|
User.email_changeset(user, attrs)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for changing the user role.
|
|
|
|
## Examples
|
|
|
|
iex> change_user_role(user)
|
|
%Ecto.Changeset{data: %User{}}
|
|
|
|
"""
|
|
@spec change_user_role(User.t(), atom()) :: Ecto.Changeset.t()
|
|
def change_user_role(user, role) do
|
|
User.role_changeset(user, role)
|
|
end
|
|
|
|
@doc """
|
|
Emulates that the email will change without actually changing
|
|
it in the database.
|
|
|
|
## Examples
|
|
|
|
iex> apply_user_email(user, "valid password", %{email: ...})
|
|
{:ok, %User{}}
|
|
|
|
iex> apply_user_email(user, "invalid password", %{email: ...})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
@spec apply_user_email(User.t(), String.t(), map()) ::
|
|
{:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
def apply_user_email(user, password, attrs) do
|
|
user
|
|
|> User.email_changeset(attrs)
|
|
|> User.validate_current_password(password)
|
|
|> Ecto.Changeset.apply_action(:update)
|
|
end
|
|
|
|
@doc """
|
|
Updates the user email using the given token.
|
|
|
|
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(), UserToken.t()) :: {:ok, any()} | :error
|
|
def update_user_email(user, token) do
|
|
context = "change:#{user.email}"
|
|
|
|
with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
|
|
%UserToken{sent_to: email} <- Repo.one(query),
|
|
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
|
|
:ok
|
|
else
|
|
_ -> :error
|
|
end
|
|
end
|
|
|
|
defp user_email_multi(user, email, context) do
|
|
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
|
|
|
|
Ecto.Multi.new()
|
|
|> Ecto.Multi.update(:user, changeset)
|
|
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context]))
|
|
end
|
|
|
|
@doc """
|
|
Delivers the update email instructions to the given user.
|
|
|
|
## Examples
|
|
|
|
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1))
|
|
{:ok, %{to: ..., body: ...}}
|
|
|
|
"""
|
|
@spec deliver_update_email_instructions(User.t(), String.t(), function) ::
|
|
{:ok, any()} | {:error, atom()}
|
|
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}")
|
|
|
|
Repo.insert!(user_token)
|
|
UserNotifier.deliver_update_email_instructions(user, update_email_url_fun.(encoded_token))
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for changing the user password.
|
|
|
|
## Examples
|
|
|
|
iex> change_user_password(user)
|
|
%Ecto.Changeset{data: %User{}}
|
|
|
|
"""
|
|
@spec change_user_password(User.t(), map()) :: Ecto.Changeset.t()
|
|
def change_user_password(user, attrs \\ %{}) do
|
|
User.password_changeset(user, attrs, hash_password: false)
|
|
end
|
|
|
|
@doc """
|
|
Updates the user password.
|
|
|
|
## Examples
|
|
|
|
iex> update_user_password(user, "valid password", %{password: ...})
|
|
{:ok, %User{}}
|
|
|
|
iex> update_user_password(user, "invalid password", %{password: ...})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
@spec update_user_password(User.t(), String.t(), map()) ::
|
|
{:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
def update_user_password(user, password, attrs) do
|
|
changeset =
|
|
user
|
|
|> User.password_changeset(attrs)
|
|
|> User.validate_current_password(password)
|
|
|
|
Ecto.Multi.new()
|
|
|> Ecto.Multi.update(:user, changeset)
|
|
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all))
|
|
|> Repo.transaction()
|
|
|> case do
|
|
{:ok, %{user: user}} -> {:ok, user}
|
|
{:error, :user, changeset, _} -> {:error, changeset}
|
|
end
|
|
end
|
|
|
|
@spec delete_user!(User.t()) :: User.t()
|
|
def delete_user!(user) do
|
|
user |> Repo.delete!()
|
|
end
|
|
|
|
## Session
|
|
|
|
@doc """
|
|
Generates a session token.
|
|
"""
|
|
@spec generate_user_session_token(User.t()) :: UserToken.t()
|
|
def generate_user_session_token(user) do
|
|
{token, user_token} = UserToken.build_session_token(user)
|
|
Repo.insert!(user_token)
|
|
token
|
|
end
|
|
|
|
@doc """
|
|
Gets the user with the given signed token.
|
|
"""
|
|
@spec get_user_by_session_token(UserToken.t()) :: User.t()
|
|
def get_user_by_session_token(token) do
|
|
{:ok, query} = UserToken.verify_session_token_query(token)
|
|
Repo.one(query)
|
|
end
|
|
|
|
@doc """
|
|
Deletes the signed token with the given context.
|
|
"""
|
|
@spec delete_session_token(UserToken.t()) :: :ok
|
|
def delete_session_token(token) do
|
|
Repo.delete_all(UserToken.token_and_context_query(token, "session"))
|
|
:ok
|
|
end
|
|
|
|
@doc """
|
|
Returns a boolean if registration is allowed or not
|
|
"""
|
|
@spec allow_registration?() :: boolean()
|
|
def allow_registration?() do
|
|
Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration] == "public" or
|
|
list_users_by_role(:admin) |> Enum.empty?()
|
|
end
|
|
|
|
## Confirmation
|
|
|
|
@doc """
|
|
Delivers the confirmation email instructions to the given user.
|
|
|
|
## Examples
|
|
|
|
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1))
|
|
{:ok, %{to: ..., body: ...}}
|
|
|
|
iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :confirm, &1))
|
|
{:error, :already_confirmed}
|
|
|
|
"""
|
|
@spec deliver_user_confirmation_instructions(User.t(), function) ::
|
|
{:ok, any()} | {:error, atom()}
|
|
def deliver_user_confirmation_instructions(user, confirmation_url_fun)
|
|
when is_function(confirmation_url_fun, 1) do
|
|
if user.confirmed_at do
|
|
{:error, :already_confirmed}
|
|
else
|
|
{encoded_token, user_token} = UserToken.build_email_token(user, "confirm")
|
|
Repo.insert!(user_token)
|
|
UserNotifier.deliver_confirmation_instructions(user, confirmation_url_fun.(encoded_token))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Confirms a user by the given token.
|
|
|
|
If the token matches, the user account is marked as confirmed
|
|
and the token is deleted.
|
|
"""
|
|
@spec confirm_user(UserToken.t()) :: {:ok, User.t()} | atom()
|
|
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
|
|
end
|
|
end
|
|
|
|
defp confirm_user_multi(user) do
|
|
Ecto.Multi.new()
|
|
|> Ecto.Multi.update(:user, User.confirm_changeset(user))
|
|
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, ["confirm"]))
|
|
end
|
|
|
|
## Reset password
|
|
|
|
@doc """
|
|
Delivers the reset password email to the given user.
|
|
|
|
## Examples
|
|
|
|
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
|
|
{:ok, %{to: ..., body: ...}}
|
|
|
|
"""
|
|
@spec deliver_user_reset_password_instructions(User.t(), function()) ::
|
|
{:ok, any()} | {:error, atom()}
|
|
def deliver_user_reset_password_instructions(user, reset_password_url_fun)
|
|
when is_function(reset_password_url_fun, 1) do
|
|
{encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
|
|
Repo.insert!(user_token)
|
|
UserNotifier.deliver_reset_password_instructions(user, reset_password_url_fun.(encoded_token))
|
|
end
|
|
|
|
@doc """
|
|
Gets the user by reset password token.
|
|
|
|
## Examples
|
|
|
|
iex> get_user_by_reset_password_token("validtoken")
|
|
%User{}
|
|
|
|
iex> get_user_by_reset_password_token("invalidtoken")
|
|
nil
|
|
|
|
"""
|
|
@spec get_user_by_reset_password_token(UserToken.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
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Resets the user password.
|
|
|
|
## Examples
|
|
|
|
iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
|
|
{:ok, %User{}}
|
|
|
|
iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
@spec reset_user_password(User.t(), map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
|
|
def reset_user_password(user, attrs) do
|
|
Ecto.Multi.new()
|
|
|> Ecto.Multi.update(:user, User.password_changeset(user, attrs))
|
|
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, :all))
|
|
|> Repo.transaction()
|
|
|> case do
|
|
{:ok, %{user: user}} -> {:ok, user}
|
|
{:error, :user, changeset, _} -> {:error, changeset}
|
|
end
|
|
end
|
|
end
|