Add user accounts, roles, and admin gating

- Scaffold password auth (email or username) with phx.gen.auth; no magic links
- Add role (member/moderator/admin/super_admin) and plan (free/premium) enums
- Gate /admin behind admin-or-above via plug + on_mount; hide the nav link
- Add mix accounts.promote for CLI role changes
- Disable email confirmation by confirming accounts at registration

Closes #3
This commit is contained in:
2026-11-03 15:48:50 +01:00
parent 3b527d21ed
commit 7cb7298236
35 changed files with 3464 additions and 9 deletions
+65
View File
@@ -35,4 +35,69 @@ defmodule ChesstrainerWeb.ConnCase do
Chesstrainer.DataCase.setup_sandbox(tags)
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
@doc """
Setup helper that registers and logs in users.
setup :register_and_log_in_user
It stores an updated connection and a registered user in the
test context.
"""
def register_and_log_in_user(%{conn: conn} = context) do
user = Chesstrainer.AccountsFixtures.user_fixture()
scope = Chesstrainer.Accounts.Scope.for_user(user)
opts =
context
|> Map.take([:token_authenticated_at])
|> Enum.into([])
%{conn: log_in_user(conn, user, opts), user: user, scope: scope}
end
@doc """
Setup helper that registers and logs in an admin user.
setup :register_and_log_in_admin
It stores an updated connection and an admin user in the test context.
"""
def register_and_log_in_admin(%{conn: conn} = context) do
{:ok, user} =
Chesstrainer.Accounts.update_user_role(
Chesstrainer.AccountsFixtures.user_fixture(),
:admin
)
scope = Chesstrainer.Accounts.Scope.for_user(user)
opts =
context
|> Map.take([:token_authenticated_at])
|> Enum.into([])
%{conn: log_in_user(conn, user, opts), user: user, scope: scope}
end
@doc """
Logs the given `user` into the `conn`.
It returns an updated `conn`.
"""
def log_in_user(conn, user, opts \\ []) do
token = Chesstrainer.Accounts.generate_user_session_token(user)
maybe_set_token_authenticated_at(token, opts[:token_authenticated_at])
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> Plug.Conn.put_session(:user_token, token)
end
defp maybe_set_token_authenticated_at(_token, nil), do: nil
defp maybe_set_token_authenticated_at(token, authenticated_at) do
Chesstrainer.AccountsFixtures.override_token_authenticated_at(token, authenticated_at)
end
end
@@ -0,0 +1,72 @@
defmodule Chesstrainer.AccountsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Chesstrainer.Accounts` context.
"""
import Ecto.Query
alias Chesstrainer.Accounts
alias Chesstrainer.Accounts.Scope
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def unique_username, do: "user#{System.unique_integer([:positive])}"
def valid_user_password, do: "hello world!"
def valid_user_attributes(attrs \\ %{}) do
Enum.into(attrs, %{
email: unique_user_email(),
username: unique_username(),
password: valid_user_password()
})
end
def user_fixture(attrs \\ %{}) do
{:ok, user} =
attrs
|> valid_user_attributes()
|> Accounts.register_user()
user
end
def user_scope_fixture do
user = user_fixture()
user_scope_fixture(user)
end
def user_scope_fixture(user) do
Scope.for_user(user)
end
def set_password(user) do
{:ok, {user, _expired_tokens}} =
Accounts.update_user_password(user, %{password: valid_user_password()})
user
end
def extract_user_token(fun) do
{:ok, captured_email} = fun.(&"[TOKEN]#{&1}[TOKEN]")
[_, token | _] = String.split(captured_email.text_body, "[TOKEN]")
token
end
def override_token_authenticated_at(token, authenticated_at) when is_binary(token) do
Chesstrainer.Repo.update_all(
from(t in Accounts.UserToken,
where: t.token == ^token
),
set: [authenticated_at: authenticated_at]
)
end
def offset_user_token(token, amount_to_add, unit) do
dt = DateTime.add(DateTime.utc_now(:second), amount_to_add, unit)
Chesstrainer.Repo.update_all(
from(ut in Accounts.UserToken, where: ut.token == ^token),
set: [inserted_at: dt, authenticated_at: dt]
)
end
end