Files
chess-endgame-trainer/lib/chesstrainer/themes.ex
T
owenrees 624ddab644 Admin panel, login throttling, and Cloudflare edge hardening (#150)
* Document the remote shell for production user management

* add sitemap to footer links

* remove border under theme selection

* Add admin theme and user management (#144)

Add /admin/themes CRUD and /admin/users list/detail with role and plan editing, guarded by a shared Policy module. Include last-super-admin and self-role protections plus structured id-only admin logs.

Also add admin invitations, confirmation resends and password-reset emails, which work in development through the Swoosh mailbox before SES is configured.

* Rate-limit password logins and emit auth events (#142)

Add a two-bucket login throttle (5 failures per login, 10 per IP in a 15-minute window) and a shared client-address helper for controllers and LiveViews. Blocked attempts skip the database and bcrypt.

Log auth.login.failed and auth.login.throttled warnings with boot-scoped HMAC addresses only, and stop repeated confirmation emails on unconfirmed login attempts.

* Harden the production login edge for Cloudflare

Document the layered login defence: the app throttle is primary, Caddy must pass Cloudflare's CF-Connecting-IP to the app, and Cloudflare edge rate limiting covers request floods.

Update the reference Caddyfile to accept only Cloudflare ranges and reject direct-to-origin traffic, and document the exact Cloudflare dashboard rules.

* Document 10-second Cloudflare rate-limit window

Cloudflare may only offer a 10-second period. Keep the edge rule at 10 login POSTs per 10 seconds per IP and rely on the app's 15-minute login/address throttle for credential guessing.

* Document Cloudflare limits for LiveView registration

Registration submits over the shared LiveView socket, so a Cloudflare rule on POST /users/register will not count account creations. Note the app-level throttle/Turnstile options instead.

* Document current Cloudflare free-plan edge controls

Record the production setup: Cloudflare-only Caddy origins, Bot Fight Mode, and the 10-second login flood rule (10 POSTs per 10 seconds, 10-second block). Clarify that the application's 15-minute throttle remains the real credential lockout.

* Auto-refresh Cloudflare CIDRs for Caddy

Move the origin allowlist into a generated cloudflare-ranges.caddy imported by Caddy. Add an hourly systemd timer and script that fetch Cloudflare's v4/v6 lists, reject malformed responses, validate the candidate Caddyfile, and reload Caddy only when the ranges change. Restore the previous file on validation or reload failure.

* Document Cloudflare CIDR refresher setup and operations

Record the installed paths, permissions, timer checks, manual run, update procedure, and the direct-on-VPS setup fallback when the deploy directory is absent.
2026-11-11 21:27:59 +01:00

203 lines
4.1 KiB
Elixir

defmodule Chesstrainer.Themes do
@moduledoc """
The Themes context.
"""
import Ecto.Query, warn: false
alias Chesstrainer.Repo
alias Chesstrainer.Themes.Theme
@doc """
Returns the list of themes.
## Examples
iex> list_themes()
[%Theme{}, ...]
"""
def list_themes do
Repo.all(Theme)
end
@doc """
Lists themes for the admin index with their endgame usage count.
Options:
* `:search` - case-insensitive match on the theme name
* `:category` - one of `:endgame`, `:opening`, `:tactic`, or blank for all
Returns `{theme, usage_count}` tuples sorted by name.
"""
def list_themes_for_admin(opts \\ []) do
search = opts |> Keyword.get(:search, "") |> to_string() |> String.trim()
category = normalize_category(Keyword.get(opts, :category, ""))
query = from(t in Theme, order_by: [asc: t.name])
query =
if search == "" do
query
else
where(query, [t], ilike(t.name, ^"%#{search}%"))
end
query =
if category do
where(query, [t], t.category == ^category)
else
query
end
query
|> join(:left, [t], j in "endgames_themes", on: j.theme_id == t.id)
|> group_by([t], t.id)
|> select([t, j], {t, count(j.endgame_id)})
|> Repo.all()
end
@doc """
Gets a single theme.
Raises `Ecto.NoResultsError` if the Theme does not exist.
## Examples
iex> get_theme!(123)
%Theme{}
iex> get_theme!(456)
** (Ecto.NoResultsError)
"""
def get_theme!(id), do: Repo.get!(Theme, id)
@doc """
Creates a theme.
## Examples
iex> create_theme(%{field: value})
{:ok, %Theme{}}
iex> create_theme(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_theme(attrs) do
%Theme{}
|> Theme.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a theme.
## Examples
iex> update_theme(theme, %{field: new_value})
{:ok, %Theme{}}
iex> update_theme(theme, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_theme(%Theme{} = theme, attrs) do
theme
|> Theme.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a theme.
## Examples
iex> delete_theme(theme)
{:ok, %Theme{}}
iex> delete_theme(theme)
{:error, %Ecto.Changeset{}}
"""
def delete_theme(%Theme{} = theme) do
Repo.delete(theme)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking theme changes.
## Examples
iex> change_theme(theme)
%Ecto.Changeset{data: %Theme{}}
"""
def change_theme(%Theme{} = theme, attrs \\ %{}) do
Theme.changeset(theme, attrs)
end
def search("", _category), do: []
def search(query, category) when is_binary(query) do
case String.trim(query) do
"" ->
[]
query ->
pattern = "%#{query}%"
Theme
|> where([t], ilike(t.name, ^pattern) and t.category == ^category)
|> order_by([t], asc: t.name)
|> limit(20)
|> Repo.all()
end
end
@doc """
Finds a theme by name (trimmed and case-insensitive) for a category, creating it
if it does not exist.
"""
def get_or_create(name, category) when is_binary(name) do
name = String.trim(name)
if name == "" do
{:error, :blank_name}
else
case find_by_name(name, category) do
nil -> create_theme(%{name: name, category: category})
theme -> {:ok, theme}
end
end
end
defp find_by_name(name, category) do
downcased = String.downcase(name)
Repo.one(
from t in Theme,
where: t.category == ^category and fragment("lower(?)", t.name) == ^downcased,
limit: 1
)
end
@doc """
Returns the names of the given themes, sorted.
"""
def names(themes) when is_list(themes) do
themes |> Enum.map(& &1.name) |> Enum.sort()
end
defp normalize_category(category) when category in [:endgame, :opening, :tactic],
do: category
defp normalize_category(category) when is_binary(category) do
Enum.find([:endgame, :opening, :tactic], &(Atom.to_string(&1) == category))
end
defp normalize_category(_category), do: nil
end