mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
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.
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
defmodule Chesstrainer.Accounts.PolicyTest do
|
||||
use Chesstrainer.DataCase, async: true
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.Policy
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
|
||||
describe "can?/2" do
|
||||
test "members and moderators have no admin capabilities" do
|
||||
for role <- [:member, :moderator], capability <- Policy.capabilities() do
|
||||
refute Policy.can?(scope_for(role), capability)
|
||||
end
|
||||
end
|
||||
|
||||
test "admins and super admins have every admin capability" do
|
||||
for role <- [:admin, :super_admin], capability <- Policy.capabilities() do
|
||||
assert Policy.can?(scope_for(role), capability)
|
||||
end
|
||||
end
|
||||
|
||||
test "a nil scope has no capabilities" do
|
||||
refute Policy.can?(nil, :manage_users)
|
||||
end
|
||||
end
|
||||
|
||||
describe "can_manage_user?/2" do
|
||||
test "an admin can manage members and moderators" do
|
||||
scope = scope_for(:admin)
|
||||
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:member))
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:moderator))
|
||||
end
|
||||
|
||||
test "an admin cannot manage admins, super admins, or themselves" do
|
||||
admin = user_with_role(:admin)
|
||||
scope = Scope.for_user(admin)
|
||||
|
||||
refute Policy.can_manage_user?(scope, admin)
|
||||
refute Policy.can_manage_user?(scope, user_with_role(:admin))
|
||||
refute Policy.can_manage_user?(scope, user_with_role(:super_admin))
|
||||
end
|
||||
|
||||
test "a super admin can manage everyone except themselves" do
|
||||
super = user_with_role(:super_admin)
|
||||
scope = Scope.for_user(super)
|
||||
|
||||
refute Policy.can_manage_user?(scope, super)
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:member))
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:moderator))
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:admin))
|
||||
assert Policy.can_manage_user?(scope, user_with_role(:super_admin))
|
||||
end
|
||||
|
||||
test "moderators and members cannot manage anyone" do
|
||||
for role <- [:member, :moderator] do
|
||||
scope = scope_for(role)
|
||||
|
||||
refute Policy.can_manage_user?(scope, user_with_role(:member))
|
||||
refute Policy.can_manage_user?(scope, user_with_role(:moderator))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "can_view_user?/2" do
|
||||
test "an admin can view lower-ranked users and themselves" do
|
||||
admin = user_with_role(:admin)
|
||||
scope = Scope.for_user(admin)
|
||||
|
||||
assert Policy.can_view_user?(scope, admin)
|
||||
assert Policy.can_view_user?(scope, user_with_role(:member))
|
||||
assert Policy.can_view_user?(scope, user_with_role(:moderator))
|
||||
refute Policy.can_view_user?(scope, user_with_role(:admin))
|
||||
refute Policy.can_view_user?(scope, user_with_role(:super_admin))
|
||||
end
|
||||
|
||||
test "a super admin can view everyone" do
|
||||
super = user_with_role(:super_admin)
|
||||
scope = Scope.for_user(super)
|
||||
|
||||
assert Policy.can_view_user?(scope, super)
|
||||
assert Policy.can_view_user?(scope, user_with_role(:member))
|
||||
assert Policy.can_view_user?(scope, user_with_role(:moderator))
|
||||
assert Policy.can_view_user?(scope, user_with_role(:admin))
|
||||
assert Policy.can_view_user?(scope, user_with_role(:super_admin))
|
||||
end
|
||||
end
|
||||
|
||||
describe "can_assign_role?/2" do
|
||||
test "admins can assign member and moderator only" do
|
||||
scope = scope_for(:admin)
|
||||
|
||||
assert Policy.can_assign_role?(scope, :member)
|
||||
assert Policy.can_assign_role?(scope, :moderator)
|
||||
refute Policy.can_assign_role?(scope, :admin)
|
||||
refute Policy.can_assign_role?(scope, :super_admin)
|
||||
end
|
||||
|
||||
test "super admins can assign every role" do
|
||||
scope = scope_for(:super_admin)
|
||||
|
||||
for role <- [:member, :moderator, :admin, :super_admin] do
|
||||
assert Policy.can_assign_role?(scope, role)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "visible_roles/1" do
|
||||
test "admins only see members and moderators" do
|
||||
assert Policy.visible_roles(scope_for(:admin)) == [:member, :moderator]
|
||||
end
|
||||
|
||||
test "super admins see every role" do
|
||||
assert Policy.visible_roles(scope_for(:super_admin)) ==
|
||||
[:member, :moderator, :admin, :super_admin]
|
||||
end
|
||||
|
||||
test "members and moderators see no users" do
|
||||
assert Policy.visible_roles(scope_for(:member)) == []
|
||||
assert Policy.visible_roles(scope_for(:moderator)) == []
|
||||
end
|
||||
end
|
||||
|
||||
defp scope_for(role) do
|
||||
role
|
||||
|> user_with_role()
|
||||
|> Scope.for_user()
|
||||
end
|
||||
|
||||
defp user_with_role(:member), do: user_fixture()
|
||||
|
||||
defp user_with_role(role) do
|
||||
{:ok, user} = Accounts.update_user_role(user_fixture(), role)
|
||||
user
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user