mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* 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.
121 lines
3.4 KiB
Elixir
121 lines
3.4 KiB
Elixir
defmodule ChesstrainerWeb.Admin.UserLive.New do
|
|
use ChesstrainerWeb, :live_view
|
|
|
|
alias Chesstrainer.Accounts
|
|
alias Chesstrainer.Accounts.Policy
|
|
alias Chesstrainer.Accounts.Role
|
|
alias Chesstrainer.Accounts.User
|
|
|
|
@plan_options [{"Free", "free"}, {"Premium", "premium"}]
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<Layouts.app flash={@flash}>
|
|
<Layouts.admin_nav current_scope={@current_scope} />
|
|
|
|
<.header>
|
|
Invite user
|
|
<:subtitle>
|
|
We create the account and email a link so the recipient can choose a password.
|
|
The invite is confirmed immediately, so no separate confirmation step is needed.
|
|
</:subtitle>
|
|
</.header>
|
|
|
|
<.form for={@form} id="invite-user-form" phx-change="validate" phx-submit="save">
|
|
<.input
|
|
field={@form[:email]}
|
|
type="email"
|
|
label="Email"
|
|
autocomplete="off"
|
|
required
|
|
/>
|
|
<.input
|
|
field={@form[:username]}
|
|
type="text"
|
|
label="Username"
|
|
autocomplete="off"
|
|
required
|
|
/>
|
|
<.input
|
|
field={@form[:role]}
|
|
type="select"
|
|
label="Role"
|
|
options={@role_options}
|
|
/>
|
|
<.input
|
|
field={@form[:plan]}
|
|
type="select"
|
|
label="Plan"
|
|
options={@plan_options}
|
|
/>
|
|
|
|
<footer>
|
|
<.button phx-disable-with="Sending..." variant="primary">Send invitation</.button>
|
|
<.button navigate={~p"/admin/users"}>Cancel</.button>
|
|
</footer>
|
|
</.form>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Invite user")
|
|
|> assign(:role_options, role_options(socket.assigns.current_scope))
|
|
|> assign(:plan_options, @plan_options)
|
|
|> assign(
|
|
:form,
|
|
to_form(%{"email" => "", "username" => "", "role" => "member", "plan" => "free"},
|
|
as: :user
|
|
)
|
|
)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("validate", %{"user" => user_params}, socket) do
|
|
changeset =
|
|
%User{}
|
|
|> User.invite_changeset(user_params)
|
|
|> User.admin_changeset(user_params)
|
|
|> Map.put(:action, :validate)
|
|
|
|
{:noreply, assign(socket, :form, to_form(changeset, action: :validate))}
|
|
end
|
|
|
|
def handle_event("save", %{"user" => user_params}, socket) do
|
|
case Accounts.invite_user_as_admin(
|
|
socket.assigns.current_scope,
|
|
user_params,
|
|
&url(~p"/users/reset-password/#{&1}")
|
|
) do
|
|
{:ok, user} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "Invitation sent to #{user.email}.")
|
|
|> push_navigate(to: ~p"/admin/users")}
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
{:noreply, assign(socket, :form, to_form(changeset, action: :insert))}
|
|
|
|
{:error, :unauthorized} ->
|
|
{:noreply, put_flash(socket, :error, "You are not authorized to assign that role.")}
|
|
|
|
{:error, _reason} ->
|
|
{:noreply,
|
|
put_flash(socket, :error, "The invitation could not be sent. Please try again.")}
|
|
end
|
|
end
|
|
|
|
defp role_options(scope) do
|
|
for role <- Role.values(),
|
|
Policy.can_assign_role?(scope, role),
|
|
do: {role_label(role), role}
|
|
end
|
|
|
|
defp role_label(:super_admin), do: "Super admin"
|
|
defp role_label(role), do: role |> Atom.to_string() |> String.capitalize()
|
|
end
|