rustango 0.58.0

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
//! Operator-facing admin console: form login plus a sidebar layout for
//! operator routes.
//!
//! It is kept separate from `rustango-admin` so the operator UI can
//! change without touching the per-tenant admin look.
//!
//! ## Logging
//!
//! Two `tracing` streams. The subscriber the app installs decides the
//! format.
//!
//! **Actions** — one event per mutation, on target [`ACTION_TARGET`]:
//!
//! ```text
//! event=operator_action action=host_add operator_id=1
//!   entity=rustango_orgs entity_id=acme fields=hostname,tenant_slug
//! ```
//!
//! The same function emits the event and writes the audit row, so the
//! log and the durable trail agree. If the row fails to write you get an
//! `ERROR` with `event=operator_action_unrecorded`: the action happened
//! but was not recorded. `fields` lists the *names* that changed, never
//! the values, because logs travel further than the database does.
//!
//! **Requests** — one event per request from [`crate::access_log`]
//! (method, path, status, duration, IP), with `next` and `token`
//! redacted out of query strings.
//!
//! Filter console activity by target:
//!
//! ```text
//! RUST_LOG=warn,rustango::tenancy::operator_console::action=info
//! ```
//!
//! ## Routes
//!
//! * `GET  /login`               — form HTML
//! * `POST /login`               — verify credentials, set cookie, redirect
//! * `POST /logout`              — clear cookie
//! * `GET  /`                    — welcome page (rustango image + intro)
//! * `GET  /operators`           — list of operators (read-only)
//! * `GET  /orgs`                — list of orgs (read-only)
//! * `GET  /orgs/{slug}/edit`    — edit form (only when built with [`router_with_pools`])
//! * `POST /orgs/{slug}/edit`    — submit edit (only when built with [`router_with_pools`])
//! * `GET  /__static__/rustango.png` — embedded asset
//!
//! Creating tenants and operators still runs through the `Cli` verbs:
//! they do work (CREATE SCHEMA, migrations, password hashing) that does
//! not fit one HTTP form. The edit routes cover the knobs an operator
//! changes live: display name, host pattern, port, path prefix, active
//! flag, and `database_url` for database-mode tenants. Only
//! `database_url` rebuilds a pool — see [`org_edit_submit`].
//!
//! ## Wiring
//!
//! ```ignore
//! let console = crate::tenancy::operator_console::router(
//!     registry.clone(),
//!     SessionSecret::from_env_or_random(),
//! );
//! let app = axum::Router::new().merge(console);
//! ```

/// Tenant creation from the console, and its progress view.
/// Mounted only by [`router_with_provisioning`].
mod audit;
mod decommission;
mod hosts;
mod migrate;
mod operators;
mod provisioning;

use crate::core::Column as _;
// `ConsoleState.registry` is `crate::sql::Pool`, the backend-erasing enum,
// so every query here works on any backend.
use crate::sql::FetcherPool;
use crate::storage::BoxedStorage;
use axum::body::Body;
use axum::extract::{Form, Multipart, Query, State};
use axum::http::{header, HeaderMap, HeaderValue, Response, StatusCode, Uri};
use axum::middleware::{self, Next};
use axum::response::{Html, IntoResponse, Redirect};
use axum::routing::{get, post};
use axum::{Extension, Router};
use cookie::time::Duration as CookieDuration;
use cookie::{Cookie, SameSite};
use serde::Deserialize;
use std::sync::Arc;
use tera::{Context, Tera};

// `session` (HMAC-signed cookies, SessionSecret) lives one level up at
// `tenancy::session` so other callers can reach it too.
pub use super::session::{self, SessionPayload, SessionSecret, SessionSecretError};
use super::session::{COOKIE_NAME, SESSION_TTL_SECS};

use super::auth;
use super::branding::{self, BrandAssetKind};
#[allow(unused_imports)] // referenced only by rustdoc links
use super::pools::TenantPools;

const RUSTANGO_PNG: &[u8] = include_bytes!("../static/rustango.png");

/// Tracing target for operator actions.
///
/// It has its own target so a deployment can route or silence console
/// activity on its own:
/// `RUST_LOG=rustango::tenancy::operator_console::action=info`.
/// Request logs come from [`crate::access_log`] under a separate
/// `rustango::access_log` target.
pub const ACTION_TARGET: &str = "rustango::tenancy::operator_console::action";

#[derive(Clone)]
struct ConsoleState {
    /// Backend-erasing registry pool, so one code path serves
    /// PG / MySQL / SQLite.
    registry: crate::sql::Pool,
    /// Pool cache. When `Some`, the `/orgs/{slug}/edit` routes are
    /// mounted: rotating `database_url` must drop that org's cached
    /// `TenantPool` so the next request rebuilds against the new URL.
    /// With `None` (the [`router`] entry point) the console is read-only.
    pools: Option<Arc<dyn crate::tenancy::TenantPoolInvalidator>>,
    /// When `Some`, the console can **create** tenants — see
    /// [`provisioning`]. Kept apart from `pools` because creating a
    /// tenant takes a database URL and connects to it, so a deployment
    /// opts in through [`router_with_provisioning`] instead of getting
    /// it along with the edit routes.
    provisioner: Option<Arc<dyn crate::tenancy::provision::TenantProvisioner>>,
    session_secret: Arc<SessionSecret>,
    tera: Arc<Tera>,
    /// Storage for per-tenant brand assets (logo, favicon). Defaults to
    /// `LocalStorage` under `./var/brand`; set `RUSTANGO_BRAND_STORAGE_DIR`
    /// to change it.
    brand_storage: BoxedStorage,
    /// The console's own brand, read from env at boot and put into every
    /// render context so a deployment can rebrand without editing
    /// templates.
    op_brand: Arc<OpBrand>,
    /// Tenant-side session secret. When `Some`, the console exposes
    /// `POST /orgs/{slug}/impersonate`, which mints a tenant-bound
    /// `TenantSessionPayload` with `imp = Some(operator_id)` so an
    /// operator can open the tenant admin without a tenant password.
    /// `Server::Builder::serve` wires it; other mount points use the
    /// `_with_impersonation` constructors.
    tenant_session_secret: Option<Arc<SessionSecret>>,
    /// URL on the tenant admin where impersonation lands the browser to
    /// redeem a signed handoff token. Mirrors
    /// [`super::routes::RouteConfig::impersonation_handoff_url`].
    /// Default `/_impersonation_handoff`.
    tenant_handoff_url: String,
}

/// Operator console branding, resolved once at boot. Per-tenant
/// branding lives on `Org` instead.
///
/// Highest priority first:
/// 1. `RUSTANGO_OPERATOR_*` env vars
/// 2. `[brand]` in `config/<env>_settings.toml`
/// 3. Hardcoded defaults
#[derive(Debug, Clone)]
struct OpBrand {
    name: String,
    tagline: Option<String>,
    logo_url: String,
    primary_color: Option<String>,
    theme_mode: String,
}

impl OpBrand {
    /// Hardcoded fallback values applied first.
    fn defaults() -> Self {
        Self {
            name: "Rustango".to_owned(),
            tagline: None,
            logo_url: "/__static__/rustango.png".to_owned(),
            primary_color: None,
            theme_mode: "auto".to_owned(),
        }
    }

    /// Resolve console branding: defaults, then `Settings.brand` from
    /// TOML, then env vars, so an env override needs no config push.
    /// A missing config file is skipped without error.
    fn from_env() -> Self {
        let mut out = Self::defaults();
        #[cfg(feature = "config")]
        if let Ok(s) = crate::config::Settings::load_from_env() {
            Self::apply_brand_settings(&mut out, &s.brand);
        }
        Self::apply_env_overrides(&mut out);
        out
    }

    #[cfg(feature = "config")]
    fn apply_brand_settings(out: &mut Self, b: &crate::config::BrandSettings) {
        if let Some(n) = b.name.as_deref().filter(|s| !s.is_empty()) {
            out.name = n.to_owned();
        }
        if let Some(t) = b.tagline.as_deref().filter(|s| !s.is_empty()) {
            out.tagline = Some(t.to_owned());
        }
        if let Some(u) = b.logo_url.as_deref().filter(|s| !s.is_empty()) {
            out.logo_url = u.to_owned();
        }
        if let Some(hex) = b
            .primary_color
            .as_deref()
            .and_then(branding::validate_hex_color)
        {
            out.primary_color = Some(hex);
        }
        if let Some(mode) = b
            .theme_mode
            .as_deref()
            .and_then(branding::validate_theme_mode)
        {
            out.theme_mode = mode.to_owned();
        }
    }

    fn apply_env_overrides(out: &mut Self) {
        if let Ok(v) = std::env::var("RUSTANGO_OPERATOR_BRAND_NAME") {
            if !v.is_empty() {
                out.name = v;
            }
        }
        if let Ok(v) = std::env::var("RUSTANGO_OPERATOR_TAGLINE") {
            if !v.is_empty() {
                out.tagline = Some(v);
            }
        }
        if let Ok(v) = std::env::var("RUSTANGO_OPERATOR_LOGO_URL") {
            if !v.is_empty() {
                out.logo_url = v;
            }
        }
        if let Some(hex) = std::env::var("RUSTANGO_OPERATOR_PRIMARY_COLOR")
            .ok()
            .as_deref()
            .and_then(branding::validate_hex_color)
        {
            out.primary_color = Some(hex);
        }
        if let Some(mode) = std::env::var("RUSTANGO_OPERATOR_THEME_MODE")
            .ok()
            .as_deref()
            .and_then(branding::validate_theme_mode)
        {
            out.theme_mode = mode.to_owned();
        }
    }
}

/// [`router_with_pools`] plus the routes that **create** tenants:
/// `GET`/`POST /orgs/new`, `POST /orgs/test-connection`, and the
/// provisioning run view with its SSE stream.
///
/// It is a separate constructor, not a flag, because creating a tenant
/// takes a database URL from a form and connects to it. A deployment
/// says yes to that on purpose.
///
/// Build the provisioner with
/// [`crate::tenancy::provision::Provisioner::new`], which closes over
/// the pools, the registry URL and the migrations directory:
///
/// ```ignore
/// let provisioner = tenancy::provision::Provisioner::new(
///     pools.clone(), registry_url.clone(), "migrations",
/// ).erased();
/// let console = operator_console::router_with_provisioning(
///     registry, pools.into_invalidator(), provisioner, secret,
/// );
/// ```
///
/// ## Authorization
///
/// Any operator who can reach the console can use these routes. There
/// is no per-operator permission model; the choice of router *is* the
/// authorization. [`router`] is read-only, [`router_with_pools`] can
/// edit, this one can also create tenants.
#[must_use]
pub fn router_with_provisioning(
    registry: impl Into<crate::sql::Pool>,
    pools: Arc<dyn crate::tenancy::TenantPoolInvalidator>,
    provisioner: Arc<dyn crate::tenancy::provision::TenantProvisioner>,
    secret: SessionSecret,
) -> Router {
    router_inner(
        registry.into(),
        Some(pools),
        Some(provisioner),
        secret,
        branding::default_brand_storage(),
        None,
        default_tenant_handoff_url(),
    )
}

/// Build the read-only operator-console `axum::Router`. Mount it at the
/// apex host; the console expects to live at the root, not under a path
/// prefix.
///
/// Use [`router_with_pools`] to let operators edit org config (display
/// name, host pattern, port, path prefix, active flag, `database_url`)
/// from the UI. That variant needs a [`TenantPools`] handle so it can
/// evict the cached pool when `database_url` changes.
///
/// Brand uploads go to [`branding::default_brand_storage`]. For S3, R2,
/// B2, MinIO or a CDN-fronted bucket, use [`router_with_brand_storage`].
#[must_use]
pub fn router(registry: impl Into<crate::sql::Pool>, secret: SessionSecret) -> Router {
    router_inner(
        registry.into(),
        None,
        None,
        secret,
        branding::default_brand_storage(),
        None,
        default_tenant_handoff_url(),
    )
}

/// Like [`router`], but also exposes `GET`/`POST /orgs/{slug}/edit`.
/// The supplied pools handle evicts the cached pool when `database_url`
/// changes, so fixing a stale connection URL needs no redeploy.
#[must_use]
pub fn router_with_pools(
    registry: impl Into<crate::sql::Pool>,
    pools: Arc<dyn crate::tenancy::TenantPoolInvalidator>,
    secret: SessionSecret,
) -> Router {
    router_inner(
        registry.into(),
        Some(pools),
        None,
        secret,
        branding::default_brand_storage(),
        None,
        default_tenant_handoff_url(),
    )
}

/// Like [`router_with_pools`], plus operator-as-superuser tenant
/// impersonation. Pass the same `tenant_session_secret` your
/// `TenantAdminBuilder` uses, or the handoff token will not verify.
///
/// The handoff goes through a URL token, not a cookie: the console
/// mints a signed token and redirects to
/// `<sub>.<apex><tenant_handoff_url>?token=<...>`; the tenant admin
/// redeems it and sets a host-scoped cookie. No impersonation cookie is
/// set on the operator-console origin.
///
/// `Server::Builder::serve` calls this for you. Other mount points swap
/// `router_with_pools` for this variant.
#[must_use]
pub fn router_with_impersonation(
    registry: impl Into<crate::sql::Pool>,
    pools: Arc<dyn crate::tenancy::TenantPoolInvalidator>,
    secret: SessionSecret,
    brand_storage: BoxedStorage,
    tenant_session_secret: SessionSecret,
    tenant_handoff_url: String,
) -> Router {
    router_inner(
        registry.into(),
        Some(pools),
        None,
        secret,
        brand_storage,
        Some(tenant_session_secret),
        tenant_handoff_url,
    )
}

/// Like [`router`], but takes any [`BoxedStorage`] for brand assets:
/// `S3Storage` (AWS, R2, B2, MinIO), a `LocalStorage` with
/// `with_base_url` behind a CDN, or your own `Storage` impl.
///
/// If the backend returns a URL from `Storage::url`, `<img src>` points
/// straight at it. Otherwise the built-in
/// `/__brand__/{slug}/{filename}` handler serves the file.
///
/// `pools = Some(...)` mounts the org-edit and brand-upload routes;
/// `None` keeps the console read-only.
#[must_use]
pub fn router_with_brand_storage(
    registry: impl Into<crate::sql::Pool>,
    pools: Option<Arc<dyn crate::tenancy::TenantPoolInvalidator>>,
    secret: SessionSecret,
    brand_storage: BoxedStorage,
) -> Router {
    router_inner(
        registry.into(),
        pools,
        None,
        secret,
        brand_storage,
        None,
        default_tenant_handoff_url(),
    )
}

/// Handoff URL used by the constructors that do not take one.
/// Same value as [`super::routes::RouteConfig`]'s default.
fn default_tenant_handoff_url() -> String {
    super::routes::RouteConfig::default().impersonation_handoff_url
}

/// Every knob at once. The named constructors above are shorthands
/// over this one; use it for a combination they do not cover, such as
/// impersonation **and** provisioning together.
///
/// `pools` unlocks the edit routes, `provisioner` the create routes and
/// `tenant_session_secret` impersonation. `None` leaves those routes
/// unmounted.
#[must_use]
pub fn router_full(
    registry: impl Into<crate::sql::Pool>,
    pools: Option<Arc<dyn crate::tenancy::TenantPoolInvalidator>>,
    provisioner: Option<Arc<dyn crate::tenancy::provision::TenantProvisioner>>,
    secret: SessionSecret,
    brand_storage: BoxedStorage,
    tenant_session_secret: Option<SessionSecret>,
    tenant_handoff_url: String,
) -> Router {
    router_inner(
        registry.into(),
        pools,
        provisioner,
        secret,
        brand_storage,
        tenant_session_secret,
        tenant_handoff_url,
    )
}

fn router_inner(
    registry: crate::sql::Pool,
    pools: Option<Arc<dyn crate::tenancy::TenantPoolInvalidator>>,
    provisioner: Option<Arc<dyn crate::tenancy::provision::TenantProvisioner>>,
    secret: SessionSecret,
    brand_storage: BoxedStorage,
    tenant_session_secret: Option<SessionSecret>,
    tenant_handoff_url: String,
) -> Router {
    let mut tera = Tera::default();
    tera.add_raw_templates([
        (
            "_theme_tokens.html",
            include_str!("../../styles/theme_tokens.html"),
        ),
        (
            "_op_styles.html",
            include_str!("../templates/_op_styles.html"),
        ),
        (
            "_op_pager.html",
            include_str!("../templates/_op_pager.html"),
        ),
        (
            "_theme_toggle.html",
            include_str!("../../admin/templates/_theme_toggle.html"),
        ),
        (
            "op_layout.html",
            include_str!("../templates/op_layout.html"),
        ),
        ("op_login.html", include_str!("../templates/op_login.html")),
        (
            "op_welcome.html",
            include_str!("../templates/op_welcome.html"),
        ),
        (
            "op_operators.html",
            include_str!("../templates/op_operators.html"),
        ),
        ("op_orgs.html", include_str!("../templates/op_orgs.html")),
        (
            "op_orgs_edit.html",
            include_str!("../templates/op_orgs_edit.html"),
        ),
        (
            "op_org_hosts.html",
            include_str!("../templates/op_org_hosts.html"),
        ),
        (
            "op_change_password.html",
            include_str!("../templates/op_change_password.html"),
        ),
        (
            "op_sso_shared.html",
            include_str!("../templates/op_sso_shared.html"),
        ),
        (
            "op_orgs_new.html",
            include_str!("../templates/op_orgs_new.html"),
        ),
        (
            "op_provision_run.html",
            include_str!("../templates/op_provision_run.html"),
        ),
        (
            "op_provision_runs.html",
            include_str!("../templates/op_provision_runs.html"),
        ),
        ("op_audit.html", include_str!("../templates/op_audit.html")),
    ])
    .expect("operator-console templates parse");
    let edit_enabled = pools.is_some();
    let impersonation_enabled = tenant_session_secret.is_some() && pools.is_some();
    let provisioning_enabled = provisioner.is_some();
    let state = ConsoleState {
        registry,
        pools,
        provisioner,
        session_secret: Arc::new(secret),
        tera: Arc::new(tera),
        brand_storage,
        op_brand: Arc::new(OpBrand::from_env()),
        tenant_session_secret: tenant_session_secret.map(Arc::new),
        tenant_handoff_url,
    };

    // Public routes (login + static asset + brand asset) skip the
    // auth gate. Brand assets are public images and need to be
    // reachable from un-authenticated tenant pages.
    let public = Router::new()
        .route("/login", get(login_form).post(login_submit))
        .route("/logout", post(logout));
    // Cacheable GET-only assets stay outside the CSRF layers, so no
    // shared cache stores a `Set-Cookie` with them.
    let assets = Router::new()
        .route("/__static__/rustango.png", get(static_rustango_png))
        .route("/__brand__/{slug}/{filename}", get(serve_brand_asset));

    // Authenticated routes: the middleware injects an `Extension<auth::Operator>`.
    let mut private = Router::new()
        .route("/", get(welcome))
        .route("/operators", get(operators::operators_list))
        .route("/orgs", get(orgs_list))
        // Read-only, so it is not behind the edit gate: a read-only
        // console is exactly where "what happened?" still needs
        // answering, and every operator can already see everything the
        // log describes.
        .route("/audit", get(audit::audit_list))
        .route(
            "/change-password",
            get(change_password_form).post(change_password_submit),
        );
    // Registry-wide shared SSO providers (admin-sso): every operator sees
    // the list; changing it needs an editable console.
    #[cfg(feature = "admin-sso")]
    {
        private = private.route("/sso-shared", get(sso_shared_list));
        if edit_enabled {
            private = private
                .route("/sso-shared", post(sso_shared_create))
                .route("/sso-shared/{id}/delete", post(sso_shared_delete))
                .route(
                    "/sso-shared/{id}/email-link",
                    post(sso_shared_set_email_link),
                );
        }
    }
    if edit_enabled {
        private = private
            // Who can sign in to this console. `edit_enabled` is really
            // the switch between a read-only console and one that can
            // change things, and creating an operator is a change.
            .route("/operators", post(operators::operator_create))
            // Open every tenant pool up front, e.g. after a deploy or a
            // credential rotation. Gated on pools, not the provisioner:
            // warming needs no migrations directory.
            .route(
                "/orgs/prewarm",
                get(orgs_post_only_redirect).post(prewarm_pools),
            )
            .route(
                "/operators/{id}/active",
                get(op_post_only_redirect).post(operators::operator_set_active),
            )
            .route(
                "/operators/{id}/reset-password",
                get(op_post_only_redirect).post(operators::operator_reset_password),
            )
            .route(
                "/orgs/{slug}/edit",
                get(org_edit_form).post(org_edit_submit),
            )
            // POST takes the multipart upload; GET bounces back to the
            // edit form so a manual URL hit does not 405.
            .route(
                "/orgs/{slug}/edit/branding",
                get(org_post_only_redirect).post(org_edit_branding),
            )
            // Taking a tenant out of service. Behind the edit gate:
            // `purge` is the most destructive thing this console can
            // do, and a read-only console must not offer it.
            .route(
                "/orgs/{slug}/deactivate",
                get(org_post_only_redirect).post(decommission::deactivate),
            )
            .route(
                "/orgs/{slug}/purge",
                get(org_post_only_redirect).post(decommission::purge),
            )
            // Probing an existing tenant, as opposed to the create
            // form's probe of a URL being typed.
            .route(
                "/orgs/{slug}/test-connection",
                post(provisioning::test_tenant_connection),
            )
            // Extra hostnames a tenant answers on. Binding a hostname
            // changes which tenant serves that traffic, so it sits
            // behind the edit gate too. Three routes, not one submit,
            // because each acts on a single row.
            .route("/orgs/{slug}/hosts", get(hosts::org_hosts_view))
            .route(
                "/orgs/{slug}/hosts/add",
                get(org_post_only_redirect).post(hosts::org_hosts_add),
            )
            .route(
                "/orgs/{slug}/hosts/remove",
                get(org_post_only_redirect).post(hosts::org_hosts_remove),
            )
            .route(
                "/orgs/{slug}/hosts/toggle",
                get(org_post_only_redirect).post(hosts::org_hosts_toggle),
            );
    }
    if provisioning_enabled {
        // Creating a tenant, and watching it happen. Mounted only when
        // the deployment supplied a provisioner, because this takes a
        // database URL and connects to it.
        private = private
            .route(
                "/orgs/new",
                get(provisioning::org_new_form).post(provisioning::org_new_submit),
            )
            .route("/orgs/test-connection", post(provisioning::test_connection))
            // Migrations ride with provisioning: both need the
            // provisioner's migrations directory and its registry URL.
            .route(
                "/orgs/migrate",
                get(op_post_only_redirect).post(migrate::migrate_all),
            )
            .route(
                "/orgs/{slug}/migrate",
                get(org_post_only_redirect).post(migrate::migrate_one),
            )
            // The index has to come before the `{run_id}` route it
            // shares a prefix with, and be a distinct path: a run id is
            // numeric, so `/orgs/provision` cannot be confused for one.
            .route("/orgs/provision", get(provisioning::provision_runs_index))
            .route(
                "/orgs/provision/{run_id}",
                get(provisioning::provision_run_view),
            )
            .route(
                "/orgs/provision/{run_id}/stream",
                get(provisioning::provision_run_stream),
            );
    }
    if impersonation_enabled {
        // Operator-as-superuser tenant admin login: mint a handoff
        // token and redirect to the tenant admin. Every mint writes an
        // audit row, so a session traces back to an operator id.
        // GET bounces back, as with branding above.
        private = private.route(
            "/orgs/{slug}/impersonate",
            get(org_post_only_redirect).post(org_impersonate),
        );
    }
    let private = private.route_layer(middleware::from_fn_with_state(
        state.clone(),
        require_session,
    ));

    // One event per request (method, path, status, duration, IP) from
    // the same middleware the admin uses. `next` is redacted because
    // the login bounce carries the whole attempted URL, and `token`
    // because the impersonation handoff puts one in the query string.
    use crate::access_log::AccessLogRouterExt as _;
    // CSRF on every console POST, login included (#1710): a tenant
    // subdomain is same-site with the apex, so `SameSite=Lax` does not
    // stop it. The layer checks token and Origin first; inside it,
    // `csrf_context` mints the token `render` puts in each form and sets
    // its cookie, so the layer sees it set and adds no second one.
    // Origin is the check that stops a tenant host: it can plant the
    // cookie, and so knows the token.
    public
        .merge(private)
        .route_layer(middleware::from_fn(
            crate::admin::csrf_context::csrf_context,
        ))
        .route_layer(crate::forms::csrf::layer())
        .merge(assets)
        .with_state(state)
        .access_log(
            crate::access_log::AccessLogLayer::new()
                .redact_additional("next")
                .redact_additional("token"),
        )
}

/// Rows per page, for every list the console renders. One number for
/// all of them, so a deployment with three tenants and one with
/// thousands behave the same way.
pub(super) const PAGE_SIZE: usize = 50;

/// One page of a console list.
///
/// A thin adapter over [`crate::pagination::Paginator`]: it counts the
/// rows, asks the paginator for the page, and flattens the result into
/// template context.
///
/// Use it instead of doing the arithmetic per view. A huge `?page=`
/// overflows a naive `(page - 1) * size`; `Paginator::get_page` clamps
/// instead.
pub(super) struct Paged {
    pub(super) offset: i64,
    pub(super) limit: i64,
    number: usize,
    total: usize,
    num_pages: usize,
    has_previous: bool,
    has_next: bool,
    marks: Vec<serde_json::Value>,
}

impl Paged {
    /// From a total the caller already knows — a filtered count, say.
    pub(super) fn from_total(total: i64, requested: Option<i64>) -> Self {
        let total = usize::try_from(total).unwrap_or(0);
        let paginator = crate::pagination::Paginator::new(total, PAGE_SIZE);
        // `get_page` clamps rather than erroring: a page number out of
        // range is a stale link, not something to show a 500 for.
        let page = paginator.get_page(requested.unwrap_or(1));
        let marks = paginator
            .get_elided_page_range(page.number, 3, 2)
            .into_iter()
            .map(|mark| match mark {
                crate::pagination::PageMark::Number(n) => serde_json::json!({"number": n}),
                crate::pagination::PageMark::Ellipsis => serde_json::json!({"ellipsis": true}),
            })
            .collect();
        Self {
            offset: i64::try_from(page.offset()).unwrap_or(i64::MAX),
            limit: i64::try_from(page.limit()).unwrap_or(i64::MAX),
            number: page.number,
            total,
            num_pages: paginator.num_pages(),
            has_previous: page.has_previous(),
            has_next: page.has_next(),
            marks,
        }
    }

    /// Counting the model's rows first, for the lists with no filter.
    pub(super) async fn of_model(
        pool: &crate::sql::Pool,
        model: &'static crate::core::ModelSchema,
        requested: Option<i64>,
    ) -> Result<Self, crate::sql::ExecError> {
        let total = count_where(pool, model, crate::core::WhereExpr::default()).await?;
        Ok(Self::from_total(total, requested))
    }

    /// What `_op_pager.html` needs.
    ///
    /// Key names match `template_views::ListView`'s so one partial
    /// renders both. `base` is the path the links point at; `suffix`
    /// carries active filters so paging does not drop them.
    pub(super) fn inject(&self, ctx: &mut Context, base: &str, suffix: &str) {
        ctx.insert("page", &self.number);
        ctx.insert("total_pages", &self.num_pages);
        ctx.insert("total", &self.total);
        ctx.insert("has_prev", &self.has_previous);
        ctx.insert("has_next", &self.has_next);
        ctx.insert("page_marks", &self.marks);
        ctx.insert("pager_base", base);
        ctx.insert("query_suffix", suffix);
    }
}

/// Which nav entry to highlight for a path.
///
/// Only the generic-view path needs this; the hand-written handlers
/// set `section` themselves.
#[cfg(feature = "template_views")]
fn nav_section(path: &str) -> &'static str {
    match path.split('/').nth(1).unwrap_or("") {
        "orgs" => "orgs",
        "operators" => "operators",
        "audit" => "audit",
        "sso-shared" => "sso",
        "change-password" => "change_password",
        _ => "home",
    }
}

/// `SELECT COUNT(*)` over one model, optionally filtered.
///
/// Separate from [`Paged`] because a list view sometimes needs a count
/// that is *not* its page count — the operator list pages its rows but
/// still has to know how many operators are active in total, which a
/// page of rows cannot tell it.
pub(super) async fn count_where(
    pool: &crate::sql::Pool,
    model: &'static crate::core::ModelSchema,
    where_clause: crate::core::WhereExpr,
) -> Result<i64, crate::sql::ExecError> {
    let count = crate::core::CountQuery {
        model,
        where_clause,
        search: None,
    };
    crate::sql::count_rows_pool(pool, &count).await
}

/// The `?page=` parameter, for the list views that take nothing else.
#[derive(Deserialize)]
pub(super) struct PageQuery {
    #[serde(default)]
    pub(super) page: Option<i64>,
}

/// A page plus the outcome of whatever redirected here.
#[derive(Deserialize)]
pub(super) struct ListQuery {
    #[serde(default)]
    pub(super) page: Option<i64>,
    #[serde(default)]
    pub(super) error: Option<String>,
    #[serde(default)]
    pub(super) notice: Option<String>,
}

/// Render a template, or return a 500 that names it.
///
/// Prefer this to `.unwrap_or_default()`, which turns a template error
/// into a blank `200` with no clue what went wrong.
fn render(state: &ConsoleState, template: &str, ctx: &Context) -> Response<Body> {
    // Every console form posts this back; `csrf::layer()` checks it (#1710).
    let mut ctx = ctx.clone();
    // `csrf_token` is for scripts that send `X-CSRF-Token` (fetch, and
    // the multipart branding form, whose body the layer cannot read).
    if let Some(token) = crate::admin::session::current_csrf_token() {
        ctx.insert("csrf_input", &crate::forms::csrf::csrf_input_html(&token));
        ctx.insert("csrf_token", &token);
    }
    match state.tera.render(template, &ctx) {
        Ok(html) => Html(html).into_response(),
        Err(e) => {
            let mut detail = e.to_string();
            let mut source = std::error::Error::source(&e);
            while let Some(s) = source {
                detail.push_str(": ");
                detail.push_str(&s.to_string());
                source = s.source();
            }
            tracing::error!(
                target: "rustango::tenancy::operator_console",
                template,
                error = %detail,
                "operator console template failed to render"
            );
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("could not render {template}: {detail}"),
            )
                .into_response()
        }
    }
}

/// Put the console branding keys into a render context. Doing it in
/// one place keeps `op_layout.html` and `op_login.html` in sync.
fn inject_op_brand(ctx: &mut Context, brand: &OpBrand) {
    // Show the "Shared SSO" nav entry only when the admin-sso feature is
    // compiled in (its routes exist only then).
    ctx.insert("sso_console", &cfg!(feature = "admin-sso"));
    ctx.insert("brand_name", &brand.name);
    ctx.insert("brand_tagline", &brand.tagline);
    ctx.insert("brand_logo_url", &brand.logo_url);
    ctx.insert("theme_mode", &brand.theme_mode);
    ctx.insert(
        "brand_css",
        &branding::build_op_brand_css(brand.primary_color.as_deref()),
    );
}

// ----------------------------- session middleware

async fn require_session(
    State(state): State<ConsoleState>,
    headers: HeaderMap,
    uri: Uri,
    mut req: axum::http::Request<Body>,
    next: Next,
) -> Response<Body> {
    let payload = crate::cookies::cookie_from_headers(&headers, COOKIE_NAME)
        .and_then(|v| session::decode(&state.session_secret, v).ok());
    // A 303 back from login turns the original POST into a GET, which
    // then 405s on POST-only routes. Trim `next` down to the parent GET
    // URL first. Unsaved form data is lost either way, but the operator
    // does not land on a 405 page.
    let method = req.method().clone();
    let raw_next = uri.path_and_query().map(|p| p.as_str()).unwrap_or("/");
    let safe_next = sanitize_next_for_method(&method, raw_next);
    let Some(payload) = payload else {
        return redirect_to_login(&safe_next).into_response();
    };
    match auth::Operator::objects()
        .where_(auth::Operator::id.eq(payload.oid))
        .fetch(&state.registry)
        .await
    {
        Ok(rows) => {
            let Some(op) = rows.into_iter().next().filter(|o| o.active) else {
                return redirect_to_login(&safe_next).into_response();
            };
            // Drop sessions issued before the last password change.
            if !session::survives_password_change(
                &state.session_secret,
                &payload.pwf,
                payload.iat,
                &op.password_hash,
                op.password_changed_at,
            ) {
                return redirect_to_login(&safe_next).into_response();
            }
            // Chrome for `op_layout.html`, so a generic view mounted
            // here renders with the console layout. Gated because
            // `tenancy` does not depend on `template_views`.
            #[cfg(feature = "template_views")]
            {
                let mut chrome = Context::new();
                inject_op_brand(&mut chrome, &state.op_brand);
                chrome.insert("operator_username", &op.username);
                chrome.insert("section", &nav_section(uri.path()));
                chrome.insert("edit_enabled", &state.pools.is_some());
                chrome.insert("provisioning_enabled", &state.provisioner.is_some());
                req.extensions_mut()
                    .insert(crate::template_views::ExtraContext(chrome));
            }
            req.extensions_mut().insert(op);
            next.run(req).await
        }
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e, "registry lookup");
            (StatusCode::INTERNAL_SERVER_ERROR, "registry lookup failed").into_response()
        }
    }
}

/// GET handler for POST-only sub-form routes. It bounces back to the
/// parent edit form, so a bare GET lands somewhere useful instead of
/// on a 405 page.
async fn org_post_only_redirect(
    axum::extract::Path(slug): axum::extract::Path<String>,
) -> Redirect {
    Redirect::to(&format!("/orgs/{slug}/edit"))
}

/// The same bounce for the operator routes, which are keyed by id
/// rather than slug and land back on the one list page.
async fn op_post_only_redirect() -> Redirect {
    Redirect::to("/operators")
}

async fn orgs_post_only_redirect() -> Redirect {
    Redirect::to("/orgs")
}

/// Open a pool for every active database-mode tenant.
///
/// Run it after a deploy, a registry restart or a credential rotation.
/// It moves the connect cost off the first request to each tenant, and
/// it shows an unreachable tenant before a user finds it.
async fn prewarm_pools(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
) -> Response<Body> {
    let Some(pools) = state.pools.as_ref() else {
        return orgs_notice("pool management is not available on this console", true);
    };
    let report = match pools.prewarm().await {
        Ok(r) => r,
        Err(e) => return orgs_notice(&format!("pre-warm failed: {e}"), true),
    };

    let mut detail = serde_json::Map::new();
    detail.insert("action".into(), serde_json::json!("pools.prewarm"));
    detail.insert("warmed".into(), serde_json::json!(report.warmed));
    detail.insert("failed".into(), serde_json::json!(report.failed));
    emit_registry_audit(
        &state.registry,
        "rustango_orgs",
        "*",
        op.id.get().copied().unwrap_or(0),
        "prewarm",
        detail,
    )
    .await;

    // A failure here is per-tenant and already logged by the pool layer;
    // say how many so the operator knows to go looking.
    let mut msg = format!(
        "warmed {} of {} active database-mode tenant(s)",
        report.warmed, report.total_active
    );
    if report.failed > 0 {
        msg.push_str(&format!(" — {} failed, see the logs", report.failed));
    }
    if report.skipped_cap > 0 {
        msg.push_str(&format!(
            " — {} skipped, the pool cache is at its cap",
            report.skipped_cap
        ));
    }
    orgs_notice(&msg, report.failed > 0)
}

/// PRG back to the tenant list with a message.
fn orgs_notice(msg: &str, is_error: bool) -> Response<Body> {
    let key = if is_error { "error" } else { "notice" };
    Redirect::to(&format!("/orgs?{key}={}", urlencoding_lite(msg))).into_response()
}

/// Rewrite a `next` path so the bounce back from `/login` lands on a
/// URL that answers GET.
///
/// GET and HEAD keep the path as-is. Anything else strips back to the
/// nearest known parent with a GET handler, or to `/` when the path is
/// not recognised.
fn sanitize_next_for_method(method: &axum::http::Method, path: &str) -> String {
    if method == axum::http::Method::GET || method == axum::http::Method::HEAD {
        return path.to_owned();
    }
    // Drop query string so the rewrite is path-only.
    let path_only = path.split('?').next().unwrap_or(path);
    // Known operator-console POST-only routes → parent GET URL.
    // Pattern matching is intentionally explicit (a regex would
    // hide the route taxonomy here).
    if let Some(rest) = path_only.strip_prefix("/orgs/") {
        // /orgs/{slug}/edit/branding → /orgs/{slug}/edit
        // /orgs/{slug}/impersonate    → /orgs/{slug}/edit
        // /orgs/{slug}/edit          → /orgs/{slug}/edit (GET form)
        if let Some(slug_end) = rest.find('/') {
            let slug = &rest[..slug_end];
            return format!("/orgs/{slug}/edit");
        }
    }
    // Unknown POST path — fall back to the welcome page so the
    // operator isn't stranded.
    "/".to_owned()
}

fn redirect_to_login(next_path: &str) -> Response<Body> {
    let next = if next_path == "/login" || next_path.starts_with("/logout") {
        "/".to_string()
    } else {
        next_path.to_string()
    };
    let location = format!("/login?next={}", urlencoding_lite(&next));
    Redirect::to(&location).into_response()
}

// ----------------------------- /login

#[derive(Deserialize)]
struct LoginQuery {
    #[serde(default)]
    next: Option<String>,
    #[serde(default)]
    error: Option<String>,
}

async fn login_form(
    State(state): State<ConsoleState>,
    Query(q): Query<LoginQuery>,
) -> Response<Body> {
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("next", &q.next.unwrap_or_else(|| "/".into()));
    ctx.insert("error", &q.error);
    render(&state, "op_login.html", &ctx)
}

#[derive(Deserialize)]
struct LoginSubmit {
    username: String,
    password: String,
    #[serde(default)]
    next: Option<String>,
}

async fn login_submit(
    State(state): State<ConsoleState>,
    ip: crate::login_throttle::ClientIp,
    headers: axum::http::HeaderMap,
    Form(form): Form<LoginSubmit>,
) -> Response<Body> {
    use crate::login_throttle::{LoginRefused, LoginScope};
    use crate::signals::auth::{
        meta_from_headers, send_user_logged_in, send_user_login_failed, AuthFailureReason,
        UserLoggedInContext, UserLoginFailedContext,
    };
    let meta = meta_from_headers(&headers, Some("/login"));
    let next = sanitize_next(form.next.as_deref());

    // Rate limits and the account lock, before the lookup (#1609).
    let mut attempt = match crate::login_throttle::shared()
        .begin(&LoginScope::Operator, &ip, &form.username)
        .await
    {
        Ok(a) => a,
        Err(refused) => return refused.into_response(),
    };
    let found = match auth::find_operator(&state.registry, &form.username).await {
        Ok(op) => op,
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "login failed").into_response();
        }
    };
    if let Some(op) = &found {
        if let Err(refused) = attempt.resolve(&op.username).await {
            return refused.into_response();
        }
    }

    let principal = match auth::check_operator_password(found, &form.password).await {
        Ok(Some(op)) => op,
        Ok(None) => {
            attempt.failed().await;
            send_user_login_failed(UserLoginFailedContext {
                source: "operator",
                attempted_username: Some(form.username.clone()),
                reason: AuthFailureReason::InvalidCredentials,
                request: meta,
            })
            .await;
            return Redirect::to(&format!(
                "/login?error=Invalid+credentials&next={}",
                urlencoding_lite(&next)
            ))
            .into_response();
        }
        Err(super::TenancyError::Busy) => return LoginRefused::Busy.into_response(),
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "login failed").into_response();
        }
    };
    let oid = principal.id.get().copied().unwrap_or_default();
    attempt.succeeded().await;
    let payload = SessionPayload::new(
        oid,
        SESSION_TTL_SECS,
        session::PasswordFingerprint::of(&state.session_secret, &principal.password_hash),
    );
    let cookie_value = session::encode(&state.session_secret, &payload);
    let cookie = Cookie::build((COOKIE_NAME, cookie_value))
        .path("/")
        .http_only(true)
        .same_site(SameSite::Lax)
        // Audit H2 — Secure on the prod tier (HTTPS); off in dev so
        // local plain-HTTP login still works.
        .secure(crate::session::secure_cookies())
        .max_age(CookieDuration::seconds(SESSION_TTL_SECS))
        .build();
    let mut resp = Redirect::to(&next).into_response();
    resp.headers_mut().append(
        header::SET_COOKIE,
        HeaderValue::from_str(&cookie.to_string()).expect("cookie is ascii"),
    );
    send_user_logged_in(UserLoggedInContext {
        source: "operator",
        user_id: oid,
        username: form.username.clone(),
        // Operator principals are uniformly "superuser-equivalent" at
        // the framework boundary — they're the people minting tenant
        // sessions. There's no separate flag on the Operator row.
        is_superuser: true,
        request: meta,
    })
    .await;
    resp
}

async fn logout(
    State(state): State<ConsoleState>,
    headers: axum::http::HeaderMap,
) -> Response<Body> {
    use crate::signals::auth::{meta_from_headers, send_user_logged_out, UserLoggedOutContext};
    // Best-effort: decode the session cookie so the signal carries
    // operator_id. Receivers tolerate `None`.
    let oid = decode_operator_session(&headers, &state.session_secret);
    let meta = meta_from_headers(&headers, Some("/logout"));
    let clear = Cookie::build((COOKIE_NAME, ""))
        .path("/")
        .http_only(true)
        .same_site(SameSite::Lax)
        // Match the Secure attribute used when the cookie was set so the
        // browser reliably clears it (audit H2).
        .secure(crate::session::secure_cookies())
        .max_age(CookieDuration::seconds(0))
        .build();
    let mut resp = Redirect::to("/login").into_response();
    resp.headers_mut().append(
        header::SET_COOKIE,
        HeaderValue::from_str(&clear.to_string()).expect("cookie is ascii"),
    );
    send_user_logged_out(UserLoggedOutContext {
        source: "operator",
        user_id: oid,
        username: None,
        request: meta,
    })
    .await;
    resp
}

/// Best-effort: decode the operator session cookie to recover the
/// operator id for audit signals. `None` on any error.
fn decode_operator_session(headers: &axum::http::HeaderMap, secret: &SessionSecret) -> Option<i64> {
    let val = crate::cookies::cookie_from_headers(headers, COOKIE_NAME)?;
    session::decode(secret, val).ok().map(|p| p.oid)
}

// ----------------------------- views

/// `GET /change-password` — render the operator self-serve
/// change-password form (#77, v0.29). Lives behind
/// `require_session` so unauthenticated requests bounce to login.
async fn change_password_form(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> Response<Body> {
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "change_password");
    ctx.insert("operator_username", &op.username);
    ctx.insert("error", &params.get("error"));
    ctx.insert("success", &params.get("ok"));
    render(&state, "op_change_password.html", &ctx)
}

#[derive(Debug, serde::Deserialize)]
struct OpChangePasswordForm {
    current_password: String,
    new_password: String,
    #[serde(default)]
    confirm_password: String,
}

/// `POST /change-password`: check the current password, hash the new
/// one, save it and bump `password_changed_at`.
///
/// `require_session` rejects cookies minted under the old password,
/// so this request is the last one the current cookie can serve; the
/// next click goes to login.
async fn change_password_submit(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Form(form): Form<OpChangePasswordForm>,
) -> Response<Body> {
    let redir = |query: &str| -> Response<Body> {
        Redirect::to(&format!("/change-password?{query}")).into_response()
    };
    let redir_err = |msg: &str| redir(&format!("error={}", crate::url_codec::url_encode(msg)));

    if form.current_password.is_empty() || form.new_password.is_empty() {
        return redir_err("All fields are required.");
    }
    if !form.confirm_password.is_empty() && form.confirm_password != form.new_password {
        return redir_err("New password and confirmation did not match.");
    }
    if form.new_password == form.current_password {
        return redir_err("New password must differ from the current password.");
    }
    if form.new_password.chars().count() < 8 {
        return redir_err("New password must be at least 8 characters.");
    }

    let op_id = op.id.get().copied().unwrap_or(0);
    if op_id <= 0 {
        return redir_err("Session is missing an operator id; please log in again.");
    }

    // `op` from the extension is a snapshot taken in `require_session`.
    // Re-read the live row so we do not overwrite a change another
    // operator just made.
    let mut op_row: auth::Operator = match auth::Operator::objects()
        .where_(auth::Operator::id.eq(op_id))
        .fetch(&state.registry)
        .await
    {
        Ok(rows) => match rows.into_iter().next() {
            Some(r) => r,
            None => {
                return redir_err("Your account no longer exists; please log in again.");
            }
        },
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e, "change-password lookup");
            return (StatusCode::INTERNAL_SERVER_ERROR, "lookup failed").into_response();
        }
    };
    let ok =
        match super::password::verify_async(&form.current_password, &op_row.password_hash).await {
            Ok(ok) => ok,
            Err(super::TenancyError::Busy) => {
                return crate::login_throttle::LoginRefused::Busy.into_response()
            }
            Err(_) => false,
        };
    if !ok {
        return redir_err("Current password did not match.");
    }
    let new_hash = match super::password::hash_async(&form.new_password).await {
        Ok(h) => h,
        Err(super::TenancyError::Busy) => {
            return crate::login_throttle::LoginRefused::Busy.into_response()
        }
        Err(e) => return redir_err(&format!("hash failed: {e}")),
    };
    op_row.password_hash = new_hash;
    op_row.password_changed_at = Some(chrono::Utc::now());
    if let Err(e) = op_row.save_pool(&state.registry).await {
        tracing::warn!(target: "rustango::tenancy::operator_console", error = %e, "change-password update");
        return (StatusCode::INTERNAL_SERVER_ERROR, "update failed").into_response();
    }
    redir("ok=Password+updated")
}

async fn welcome(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
) -> Response<Body> {
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "home");
    ctx.insert("operator_username", &op.username);
    render(&state, "op_welcome.html", &ctx)
}

async fn orgs_list(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    Query(q): Query<ListQuery>,
) -> Response<Body> {
    // Paged: this fetched every tenant, which is fine for the three a
    // demo has and not for the thousands a real registry holds.
    // Ordered so the page boundaries are stable between requests — an
    // unordered `LIMIT` may hand back the same row on two pages.
    use crate::core::Model as _;
    let paged = match Paged::of_model(&state.registry, super::Org::SCHEMA, q.page).await {
        Ok(p) => p,
        Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
    };
    let rows: Vec<super::Org> = match super::Org::objects()
        .order_by(&[("slug", false)])
        .limit(paged.limit)
        .offset(paged.offset)
        .fetch(&state.registry)
        .await
    {
        Ok(r) => r,
        Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
    };
    let view: Vec<_> = rows
        .into_iter()
        .map(|o| {
            serde_json::json!({
                "slug": o.slug,
                "display_name": o.display_name,
                "storage_mode": o.storage_mode,
                "backend_kind": o.backend_kind,
                "host_pattern": o.host_pattern,
                "active": o.active,
                "created_at": o.created_at.format("%Y-%m-%d %H:%M UTC").to_string(),
            })
        })
        .collect();
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "orgs");
    ctx.insert("operator_username", &op.username);
    ctx.insert("orgs", &view);
    ctx.insert("edit_enabled", &state.pools.is_some());
    // Drives the "New tenant" button.
    ctx.insert("provisioning_enabled", &state.provisioner.is_some());
    ctx.insert("error", &q.error);
    ctx.insert("notice", &q.notice);
    paged.inject(&mut ctx, "/orgs", "");
    render(&state, "op_orgs.html", &ctx)
}

// ---- Shared SSO providers (registry-wide, `admin-sso`) --------------
// A provider defined here (`SharedSsoProvider`, registry scope) is
// offered on every tenant's login page. Each tenant manages its own
// providers (`SsoProvider`) from its own admin. Both lists merge at
// login; the tenant one wins on a slug clash.
#[cfg(feature = "admin-sso")]
#[derive(serde::Deserialize)]
struct SharedSsoForm {
    slug: String,
    label: String,
    kind: String,
    issuer_url: Option<String>,
    client_id: String,
    client_secret: String,
    scopes: Option<String>,
    sort_order: Option<i32>,
    enabled: Option<String>,
    allow_email_link: Option<String>,
}

#[cfg(feature = "admin-sso")]
async fn sso_shared_list(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
) -> Response<Body> {
    let mut rows: Vec<super::sso::SharedSsoProvider> =
        match super::sso::SharedSsoProvider::objects()
            .fetch(&state.registry)
            .await
        {
            Ok(r) => r,
            Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
        };
    rows.sort_by_key(|p| p.sort_order);
    let view: Vec<_> = rows
        .into_iter()
        .map(|p| {
            serde_json::json!({
                "id": p.id.get().copied().unwrap_or_default(),
                "slug": p.slug,
                "label": p.label,
                "kind": p.kind,
                "issuer_url": p.issuer_url,
                "client_id": p.client_id,
                "enabled": p.enabled,
                "allow_email_link": p.allow_email_link,
                "sort_order": p.sort_order,
            })
        })
        .collect();
    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "sso");
    ctx.insert("operator_username", &op.username);
    ctx.insert("providers", &view);
    ctx.insert("edit_enabled", &state.pools.is_some());
    render(&state, "op_sso_shared.html", &ctx)
}

#[cfg(feature = "admin-sso")]
async fn sso_shared_create(
    State(state): State<ConsoleState>,
    Extension(_op): Extension<auth::Operator>,
    Form(form): Form<SharedSsoForm>,
) -> Response<Body> {
    let mut row = super::sso::SharedSsoProvider {
        id: crate::sql::Auto::Unset,
        slug: form.slug.trim().to_owned(),
        label: form.label,
        kind: form.kind.trim().to_owned(),
        issuer_url: form.issuer_url.filter(|s| !s.trim().is_empty()),
        client_id: form.client_id,
        client_secret: crate::casts::Cast::new(form.client_secret),
        enabled: form.enabled.as_deref() == Some("on"),
        sort_order: form.sort_order.unwrap_or(0),
        scopes: form.scopes.filter(|s| !s.trim().is_empty()),
        allow_email_link: form.allow_email_link.as_deref() == Some("on"),
        created_at: crate::sql::Auto::Unset,
        updated_at: crate::sql::Auto::Unset,
    };
    if let Err(e) = row.insert_pool(&state.registry).await {
        return (
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("create failed: {e}"),
        )
            .into_response();
    }
    Redirect::to("/sso-shared").into_response()
}

/// The wanted `allow_email_link` value, `on` or `off`.
#[cfg(feature = "admin-sso")]
#[derive(serde::Deserialize)]
struct EmailLinkForm {
    allow_email_link: String,
}

/// Set `allow_email_link` in place (only that column), so the id and its links survive.
#[cfg(feature = "admin-sso")]
async fn sso_shared_set_email_link(
    State(state): State<ConsoleState>,
    Extension(_op): Extension<auth::Operator>,
    axum::extract::Path(id): axum::extract::Path<i64>,
    Form(form): Form<EmailLinkForm>,
) -> Response<Body> {
    use crate::sql::UpdaterPool as _;
    let allow = match form.allow_email_link.as_str() {
        "on" => true,
        "off" => false,
        _ => {
            return (
                StatusCode::BAD_REQUEST,
                "allow_email_link must be on or off",
            )
                .into_response()
        }
    };
    match super::sso::SharedSsoProvider::objects()
        .filter("id", id)
        .update()
        .set("allow_email_link", allow)
        .execute_pool(&state.registry)
        .await
    {
        Ok(1) => Redirect::to("/sso-shared").into_response(),
        Ok(_) => (StatusCode::NOT_FOUND, "no such shared provider").into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("update failed: {e}"),
        )
            .into_response(),
    }
}

#[cfg(feature = "admin-sso")]
async fn sso_shared_delete(
    State(state): State<ConsoleState>,
    Extension(_op): Extension<auth::Operator>,
    axum::extract::Path(id): axum::extract::Path<i64>,
) -> Response<Body> {
    let row = super::sso::SharedSsoProvider::objects()
        .filter("id", id)
        .fetch(&state.registry)
        .await
        .ok()
        .and_then(|v| v.into_iter().next());
    if let Some(r) = row {
        let _ = r.delete_pool(&state.registry).await;
    }
    Redirect::to("/sso-shared").into_response()
}

// ----------------------------- /orgs/{slug}/edit
//
// Built on the admin form pipeline:
// [`crate::admin::render::render_input`] for the `<input>` HTML,
// [`crate::admin::render::render_value_for_input`] for the prefill, and
// [`crate::forms::collect_values`] to parse the submit against
// `Org::SCHEMA` with per-field checks.
//
// Three things are specific to this form: the lock list below,
// `database_url` masking, and calling [`TenantPools::invalidate`] when
// `database_url` changes so the next request rebuilds the pool.

/// `Org` fields that are display-only on the edit form.
///
/// `logo_path` and `favicon_path` come from the upload sub-form
/// instead. `backend_kind` is locked because switching backends would
/// leave the tenant's data on the old driver; use the
/// `migrate-tenant-storage` verb for that.
const LOCKED_ORG_FIELDS: &[&str] = &[
    "id",
    "slug",
    "storage_mode",
    "backend_kind",
    "schema_name",
    "created_at",
    "logo_path",
    "favicon_path",
];

/// `database_url` is editable but special: empty submit means "keep
/// current", and we never render the existing value into the input.
const DATABASE_URL_FIELD: &str = "database_url";

#[derive(Deserialize, Default)]
struct OrgEditQuery {
    #[serde(default)]
    error: Option<String>,
    #[serde(default)]
    notice: Option<String>,
}

async fn org_edit_form(
    State(state): State<ConsoleState>,
    axum::extract::Path(slug): axum::extract::Path<String>,
    Extension(op): Extension<auth::Operator>,
    Query(q): Query<OrgEditQuery>,
) -> Response<Body> {
    use crate::admin::render;
    use crate::core::Model as _;

    // Fetch via the ORM (bi-dialect) instead of `SELECT *` + PgRow.
    let rows: Vec<super::Org> = match super::Org::objects()
        .where_(super::Org::slug.eq(slug.clone()))
        .fetch(&state.registry)
        .await
    {
        Ok(r) => r,
        Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
    };
    let Some(org_row) = rows.into_iter().next() else {
        return (StatusCode::NOT_FOUND, format!("org `{slug}` not found")).into_response();
    };
    // Serialize once to a JSON object so the per-field renderer can
    // read each column by name without us hand-rolling a per-field
    // match. Backend-agnostic — no PgRow.
    let row_json = serde_json::to_value(&org_row).unwrap_or_else(|_| serde_json::json!({}));

    // Build per-field render contexts. Iterating `Org::SCHEMA.fields`
    // means new columns added to Org show up automatically — the
    // template doesn't need a manual update.
    let mut editable_rows: Vec<serde_json::Value> = Vec::new();
    let mut locked_rows: Vec<serde_json::Value> = Vec::new();
    for field in super::Org::SCHEMA.scalar_fields() {
        let prefill = render::render_value_for_input_json(&row_json, field);
        if LOCKED_ORG_FIELDS.contains(&field.name) {
            locked_rows.push(serde_json::json!({
                "name": field.name,
                "value": prefill,
            }));
            continue;
        }
        // database_url: mask the prefill, supply a placeholder, and
        // surface the secret-reference shape separately.
        let (prefill_for_input, helper) = if field.name == DATABASE_URL_FIELD {
            let hint = if prefill.starts_with("env:") || prefill.starts_with("vault:") {
                Some(format!("current: {prefill}"))
            } else if !prefill.is_empty() {
                Some("current: <literal connection URL — masked>".to_owned())
            } else {
                None
            };
            (
                String::new(),
                Some(format!(
                    "{} — leave blank to keep current; new value evicts the cached pool",
                    hint.unwrap_or_else(|| "no value set".to_owned())
                )),
            )
        } else {
            (prefill, None::<String>)
        };
        let input_html = render::render_input(field, &prefill_for_input, false);
        editable_rows.push(serde_json::json!({
            "name": field.name,
            "input": input_html,
            "helper": helper,
        }));
    }

    // Pull current logo / favicon paths off the org row.
    let logo_path: Option<String> = org_row.logo_path.clone();
    let favicon_path: Option<String> = org_row.favicon_path.clone();
    let logo_url = branding::brand_asset_url(&slug, logo_path.as_deref(), &state.brand_storage);
    let favicon_url =
        branding::brand_asset_url(&slug, favicon_path.as_deref(), &state.brand_storage);

    let mut ctx = Context::new();
    inject_op_brand(&mut ctx, &state.op_brand);
    ctx.insert("section", "orgs");
    ctx.insert("operator_username", &op.username);
    ctx.insert("slug", &slug);
    // Drives the "Run migrations" button: migrations ride with the
    // provisioner, which owns the migrations directory.
    ctx.insert("provisioning_enabled", &state.provisioner.is_some());
    ctx.insert("editable_rows", &editable_rows);
    ctx.insert("locked_rows", &locked_rows);
    ctx.insert("logo_url", &logo_url);
    ctx.insert("favicon_url", &favicon_url);
    ctx.insert("error", &q.error);
    ctx.insert("notice", &q.notice);
    // v0.27.8 (#78) — show the "Open admin as superuser →"
    // form only when the operator console was wired with a
    // tenant session secret (i.e. via `router_with_impersonation`).
    ctx.insert(
        "impersonate_enabled",
        &state.tenant_session_secret.is_some(),
    );
    render(&state, "op_orgs_edit.html", &ctx)
}

/// `POST /orgs/{slug}/edit`: parse the form with
/// [`crate::forms::collect_values`] and UPDATE only the columns it
/// supplied.
///
/// Two side effects: changing `database_url` calls
/// [`TenantPools::invalidate`] so the next request rebuilds the pool,
/// and `active = false` makes the resolver return 404 for that tenant.
async fn org_edit_submit(
    State(state): State<ConsoleState>,
    axum::extract::Path(slug): axum::extract::Path<String>,
    Extension(op): Extension<auth::Operator>,
    Form(mut form): Form<std::collections::HashMap<String, String>>,
) -> Response<Body> {
    use crate::core::Model as _;

    let pools = state
        .pools
        .as_ref()
        .expect("edit routes only mounted when pools is Some");

    // A blank `database_url` means "keep the current one". Drop it from
    // the form and add it to the skip list, or `collect_values` reads
    // the missing field as NULL and wipes the stored URL.
    let database_url_supplied = form
        .get(DATABASE_URL_FIELD)
        .is_some_and(|s| !s.trim().is_empty());
    if !database_url_supplied {
        form.remove(DATABASE_URL_FIELD);
    }
    let mut skip: Vec<&str> = LOCKED_ORG_FIELDS.to_vec();
    if !database_url_supplied {
        skip.push(DATABASE_URL_FIELD);
    }
    // An unchecked checkbox is simply absent from the form;
    // `collect_values` already reads a missing bool as `false`.

    let collected = match crate::forms::collect_values(super::Org::SCHEMA, &form, &skip) {
        Ok(v) => v,
        Err(e) => return redirect_with_error(&slug, &e.to_string()),
    };
    if collected.is_empty() {
        return redirect_with_error(&slug, "no editable fields supplied");
    }

    // Fetch existing for change detection (database_url rotation).
    // ORM path so registry-backend stays plug-and-play.
    let existing_orgs: Vec<super::Org> = match super::Org::objects()
        .where_(super::Org::slug.eq(slug.clone()))
        .fetch(&state.registry)
        .await
    {
        Ok(rows) => rows,
        Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
    };
    let Some(existing_org) = existing_orgs.into_iter().next() else {
        return (StatusCode::NOT_FOUND, format!("org `{slug}` not found")).into_response();
    };
    let new_database_url = collected.iter().find_map(|(c, v)| {
        if *c == DATABASE_URL_FIELD {
            match v {
                crate::core::SqlValue::String(s) => Some(s.clone()),
                _ => None,
            }
        } else {
            None
        }
    });
    let database_url_changed = new_database_url
        .as_deref()
        .is_some_and(|new| existing_org.database_url.as_deref() != Some(new));

    // Build the UPDATE through the ORM's `UpdateQuery` IR + run it
    // via `update_pool` so the SQL gets compiled with the right
    // dialect (PG `$N` / MySQL `?` / SQLite `?`) + identifier
    // quoting. Replaces the prior hand-rolled `UPDATE "…" SET … = $N
    // WHERE …` string which was PG-only.
    let assignments: Vec<crate::core::Assignment> = collected
        .iter()
        .map(|(col, val)| crate::core::Assignment {
            column: *col,
            value: val.clone().into(),
        })
        .collect();
    let update_q = crate::core::UpdateQuery {
        model: super::Org::SCHEMA,
        set: assignments,
        where_clause: crate::core::WhereExpr::and_predicates(vec![crate::core::Filter {
            column: "slug",
            op: crate::core::Op::Eq,
            value: crate::core::SqlValue::String(slug.clone()),
        }]),
    };
    if let Err(e) = crate::sql::update_pool(&state.registry, &update_q).await {
        return redirect_with_error(&slug, &format!("update failed: {e}"));
    }

    // Drop the cached `Org` before anything else acts on the write.
    // Resolution serves from that cache now, so without this the pool
    // is evicted and then immediately rebuilt from the stale row — the
    // rotation this handler promises would report success while the
    // next request reconnected with the old credential. `active =
    // false` has the same shape: the tenant would keep serving.
    super::invalidate_org_cache();

    if database_url_changed {
        pools.invalidate(&slug).await;
    }

    // Audit row: operator-side config edits should leave a trail
    // alongside impersonation. We record the columns touched,
    // omitting `database_url` itself even on rotation (it's a
    // credentialed URL — we record the FACT of rotation, not the
    // value).
    let operator_id = op.id.get().copied().unwrap_or(0);
    let mut detail = serde_json::Map::new();
    detail.insert(
        "action".into(),
        serde_json::Value::String("org.edit".into()),
    );
    let touched_cols: Vec<String> = collected
        .iter()
        .filter_map(|(c, _)| {
            if *c == DATABASE_URL_FIELD {
                None
            } else {
                Some((*c).to_owned())
            }
        })
        .collect();
    detail.insert("fields".into(), serde_json::json!(touched_cols));
    if database_url_changed {
        detail.insert("database_url_rotated".into(), serde_json::json!(true));
    }
    emit_op_audit(&state.registry, &slug, operator_id, "edit", detail).await;

    let notice = if database_url_changed {
        format!("updated `{slug}` (pool evicted — next request rebuilds with new URL)")
    } else {
        format!("updated `{slug}`")
    };
    Redirect::to(&format!(
        "/orgs/{}/edit?notice={}",
        urlencoding_lite(&slug),
        urlencoding_lite(&notice),
    ))
    .into_response()
}

fn redirect_with_error(slug: &str, msg: &str) -> Response<Body> {
    Redirect::to(&format!(
        "/orgs/{}/edit?error={}",
        urlencoding_lite(slug),
        urlencoding_lite(msg),
    ))
    .into_response()
}

/// Write one audit row for an operator action on `rustango_orgs`. The
/// changes blob always holds `tenant_slug` and `operator_id`; add more
/// keys through `extra`. A write failure is logged, never fatal.
///
/// `source` reads `operator:<id>:<verb>`, so a later search can tell
/// operator activity from tenant-user activity.
async fn emit_op_audit(
    registry: &crate::sql::Pool,
    slug: &str,
    operator_id: i64,
    verb: &str,
    extra: serde_json::Map<String, serde_json::Value>,
) {
    let mut changes = extra;
    changes.insert(
        "tenant_slug".into(),
        serde_json::Value::String(slug.to_owned()),
    );
    emit_registry_audit(registry, "rustango_orgs", slug, operator_id, verb, changes).await;
}

/// The same trail for an action whose subject is not a tenant, so the
/// entity column names the real table. [`emit_op_audit`] is the
/// `rustango_orgs` shorthand over this.
async fn emit_registry_audit(
    registry: &crate::sql::Pool,
    entity_table: &'static str,
    entity_pk: &str,
    operator_id: i64,
    verb: &str,
    extra: serde_json::Map<String, serde_json::Value>,
) {
    let mut changes = extra;
    changes.insert("operator_id".into(), serde_json::json!(operator_id));

    // Log the action here, not at each call site, so the log and the
    // audit trail always cover the same actions. Field *names* only:
    // logging the whole blob would leak a credential the day a caller
    // puts a value in it.
    let fields: Vec<&str> = changes
        .keys()
        .filter(|k| k.as_str() != "operator_id")
        .map(String::as_str)
        .collect();
    tracing::info!(
        target: ACTION_TARGET,
        event = "operator_action",
        action = verb,
        operator_id,
        entity = entity_table,
        entity_id = entity_pk,
        fields = fields.join(","),
        "operator action",
    );

    let entry = crate::audit::PendingEntry {
        entity_table,
        entity_pk: entity_pk.to_owned(),
        operation: crate::audit::AuditOp::Action,
        source: crate::audit::AuditSource::Custom(format!("operator:{operator_id}:{verb}")),
        changes: serde_json::Value::Object(changes),
    };
    if let Err(e) = crate::audit::emit_one_pool(registry, &entry).await {
        // The action happened but was not recorded. Loud on purpose: a
        // trail with a silent hole in it is worse than a short one.
        tracing::error!(
            target: ACTION_TARGET,
            event = "operator_action_unrecorded",
            action = verb,
            operator_id,
            entity = entity_table,
            entity_id = entity_pk,
            error = %e,
            "operator action was NOT written to the audit log",
        );
    }
}

// ----------------------------- /orgs/{slug}/edit/branding (multipart)
//
// The main edit form is url-encoded and posts the org's scalar config.
// Uploads need multipart, so they get their own sub-form.
// `branding::save_brand_asset` checks each part's content type and
// size. Once the file is stored we update `Org.{logo,favicon}_path`.
async fn org_edit_branding(
    State(state): State<ConsoleState>,
    axum::extract::Path(slug): axum::extract::Path<String>,
    Extension(op): Extension<auth::Operator>,
    mut mp: Multipart,
) -> Response<Body> {
    let mut updates: Vec<(&'static str, Option<String>)> = Vec::new();
    while let Ok(Some(field)) = mp.next_field().await {
        let name = field.name().map(str::to_owned);
        let kind = match name.as_deref() {
            Some("logo") => BrandAssetKind::Logo,
            Some("favicon") => BrandAssetKind::Favicon,
            _ => continue,
        };
        let content_type = field.content_type().map(str::to_owned);
        // An empty file part means "user didn't choose a file" — skip
        // without touching the column. Browsers send the part with
        // a filename of "" and zero bytes when the input is empty.
        let bytes = match field.bytes().await {
            Ok(b) if b.is_empty() => continue,
            Ok(b) => b.to_vec(),
            Err(e) => return redirect_with_error(&slug, &format!("multipart: {e}")),
        };
        match branding::save_brand_asset(
            &slug,
            kind,
            &bytes,
            content_type.as_deref(),
            &state.brand_storage,
        )
        .await
        {
            Ok(filename) => {
                let column = match kind {
                    BrandAssetKind::Logo => "logo_path",
                    BrandAssetKind::Favicon => "favicon_path",
                };
                updates.push((column, Some(filename)));
            }
            Err(branding::BrandError::TooLarge { actual, max }) => {
                return redirect_with_error(
                    &slug,
                    &format!("file too large: {actual} bytes (max {max})"),
                );
            }
            Err(branding::BrandError::UnsupportedContentType(ct)) => {
                return redirect_with_error(
                    &slug,
                    &format!("unsupported file type `{ct}` — use PNG/JPEG/WebP/ICO"),
                );
            }
            Err(e) => return redirect_with_error(&slug, &format!("upload failed: {e}")),
        }
    }
    if updates.is_empty() {
        return redirect_with_error(&slug, "no file chosen");
    }
    // Apply all updates via the ORM's `UpdateQuery` so the SQL is
    // compiled with the right dialect (PG `$N` vs MySQL/SQLite `?`)
    // and identifier quoting per backend.
    use crate::core::Model as _;
    let assignments: Vec<crate::core::Assignment> = updates
        .iter()
        .map(|(col, v)| crate::core::Assignment {
            column: *col,
            value: v
                .as_ref()
                .map(|s| crate::core::SqlValue::String(s.clone()))
                .unwrap_or(crate::core::SqlValue::Null)
                .into(),
        })
        .collect();
    let update_q = crate::core::UpdateQuery {
        model: super::Org::SCHEMA,
        set: assignments,
        where_clause: crate::core::WhereExpr::and_predicates(vec![crate::core::Filter {
            column: "slug",
            op: crate::core::Op::Eq,
            value: crate::core::SqlValue::String(slug.clone()),
        }]),
    };
    if let Err(e) = crate::sql::update_pool(&state.registry, &update_q).await {
        return redirect_with_error(&slug, &format!("update failed: {e}"));
    }
    // Branding lives on the `Org` row that resolution caches, so an
    // upload without this leaves the previous logo rendering until the
    // entry expires.
    super::invalidate_org_cache();

    // Audit row — branding uploads touch the public-facing surface
    // of a tenant; operators iterating during onboarding leave a
    // breadcrumb trail. Records which assets landed (`logo`,
    // `favicon`) without the binary blob.
    let operator_id = op.id.get().copied().unwrap_or(0);
    let assets: Vec<String> = updates
        .iter()
        .map(|(col, _)| match *col {
            "logo_path" => "logo".to_owned(),
            "favicon_path" => "favicon".to_owned(),
            other => other.to_owned(),
        })
        .collect();
    let mut detail = serde_json::Map::new();
    detail.insert(
        "action".into(),
        serde_json::Value::String("org.branding.upload".into()),
    );
    detail.insert("assets".into(), serde_json::json!(assets));
    emit_op_audit(&state.registry, &slug, operator_id, "branding", detail).await;

    let notice = format!("uploaded {} brand asset(s) for `{slug}`", updates.len());
    Redirect::to(&format!(
        "/orgs/{}/edit?notice={}",
        urlencoding_lite(&slug),
        urlencoding_lite(&notice),
    ))
    .into_response()
}

/// `GET /__brand__/{slug}/{filename}` — public asset serve. Validates
/// the slug + filename via the branding module, reads bytes from the
/// brand storage, returns with the correct `Content-Type` and a
/// short cache TTL (operators may iterate during onboarding).
async fn serve_brand_asset(
    State(state): State<ConsoleState>,
    axum::extract::Path((slug, filename)): axum::extract::Path<(String, String)>,
) -> Response<Body> {
    match branding::load_brand_asset(&slug, &filename, &state.brand_storage).await {
        Ok((bytes, ct)) => Response::builder()
            .status(StatusCode::OK)
            .header(header::CONTENT_TYPE, ct)
            .header(header::CACHE_CONTROL, "public, max-age=300")
            .body(Body::from(bytes))
            .expect("response builds"),
        Err(
            branding::BrandError::NotFound
            | branding::BrandError::InvalidSlug
            | branding::BrandError::InvalidFilename,
        ) => (StatusCode::NOT_FOUND, "not found").into_response(),
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e, "brand asset");
            (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
        }
    }
}

// ----------------------------- static asset

async fn static_rustango_png() -> Response<Body> {
    Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "image/png")
        .header(header::CACHE_CONTROL, "public, max-age=86400")
        .body(Body::from(RUSTANGO_PNG))
        .expect("response builds")
}

// ----------------------------- helpers

// The crate's one query-value encoder (#1663); it also escapes `/`.
use crate::url_codec::url_encode as urlencoding_lite;

/// Check a caller-supplied `?next=` before it reaches `Location`.
///
/// Only same-origin relative paths pass, so a post-login redirect
/// cannot be pointed at another site. The check itself lives in
/// [`crate::auth_decorators::safe_next`].
fn sanitize_next(next: Option<&str>) -> String {
    next.and_then(crate::auth_decorators::safe_next)
        .unwrap_or_else(|| "/".to_owned())
}

// ============================================================== /orgs/{slug}/impersonate
//
// Behind the "Open admin as superuser" button on `/orgs/{slug}/edit`.
// The console mints a short-lived signed `HandoffPayload` and redirects
// to `<sub>.<apex><handoff_url>?token=<...>`. The tenant admin redeems
// the token and sets a host-scoped cookie, which browsers accept even
// on `localhost`.
//
// A banner and audit rows on both ends keep impersonation visible.

async fn org_impersonate(
    State(state): State<ConsoleState>,
    Extension(op): Extension<auth::Operator>,
    headers: HeaderMap,
    axum::extract::Path(slug): axum::extract::Path<String>,
) -> Response<Body> {
    let Some(tenant_secret) = state.tenant_session_secret.clone() else {
        // Unreachable: the route is mounted only with a secret.
        return (StatusCode::SERVICE_UNAVAILABLE, "impersonation disabled").into_response();
    };
    // Look up the org so we can refuse an inactive tenant and give the
    // audit row the right context.
    let orgs: Vec<super::Org> = match super::Org::objects()
        .where_(super::Org::slug.eq(slug.clone()))
        .fetch(&state.registry)
        .await
    {
        Ok(rows) => rows,
        Err(e) => {
            tracing::warn!(target: "rustango::tenancy::operator_console", error = %e, "org lookup failed");
            return (StatusCode::INTERNAL_SERVER_ERROR, "registry lookup failed").into_response();
        }
    };
    let Some(org) = orgs.into_iter().next() else {
        return (StatusCode::NOT_FOUND, format!("no tenant `{slug}`")).into_response();
    };
    if !org.active {
        return (
            StatusCode::CONFLICT,
            format!("tenant `{slug}` is inactive — refusing impersonation"),
        )
            .into_response();
    }
    let operator_id = op.id.get().copied().unwrap_or(0);

    // Mint the short-lived URL handoff token. Includes a random
    // single-use `jti` and the slug, both checked at redemption.
    use super::impersonation_handoff as handoff;
    let payload = handoff::HandoffPayload::new(
        operator_id,
        slug.clone(),
        handoff::HANDOFF_TTL_SECS,
        handoff::PasswordFingerprint::of(&tenant_secret, &op.password_hash),
    );
    let token = handoff::mint(&tenant_secret, &payload);

    // Audit row on the operator side. The tenant admin writes its own
    // when an impersonated session first makes a change.
    let mut detail = serde_json::Map::new();
    detail.insert(
        "action".into(),
        serde_json::Value::String("impersonate.start".into()),
    );
    emit_op_audit(&state.registry, &slug, operator_id, "impersonating", detail).await;

    // Build the redirect: tenant subdomain, the handoff path, and the
    // token. Scheme comes from `RUSTANGO_TENANT_SCHEME`, defaulting to
    // http for local dev.
    let scheme = std::env::var("RUSTANGO_TENANT_SCHEME").unwrap_or_else(|_| "http".into());
    let host = if let Some(pat) = org.host_pattern.as_deref().filter(|s| !s.is_empty()) {
        pat.to_owned()
    } else {
        // No host pattern: build `<slug>.<apex>` from env.
        let apex = std::env::var("RUSTANGO_APEX_DOMAIN").unwrap_or_else(|_| "localhost".into());
        format!("{}.{}", slug, apex)
    };
    // Port: `RUSTANGO_TENANT_PORT` wins, for deployments where the
    // listener port differs from the public one. Otherwise reuse the
    // port from the request's Host header, so dev on `:8080` and prod
    // on a standard port both work with no configuration.
    let port_suffix = std::env::var("RUSTANGO_TENANT_PORT")
        .ok()
        .filter(|s| !s.is_empty() && s != "80" && s != "443")
        .map(|p| format!(":{p}"))
        .or_else(|| {
            headers
                .get(header::HOST)
                .and_then(|v| v.to_str().ok())
                .and_then(|h| h.rsplit_once(':').map(|(_, port)| port.to_owned()))
                .filter(|p| !p.is_empty() && p != "80" && p != "443")
                .map(|p| format!(":{p}"))
        })
        .unwrap_or_default();
    let handoff_path = state.tenant_handoff_url.trim_end_matches('/');
    // The token is base64url (`URL_SAFE_NO_PAD`) + a single `.` —
    // every character is already URL-safe, so no escaping needed.
    let redirect_to = format!("{scheme}://{host}{port_suffix}{handoff_path}?token={token}");

    let mut resp = Redirect::to(&redirect_to).into_response();
    // The token in the URL is single-use + short-lived, but
    // `Referrer-Policy: no-referrer` keeps it from leaking to
    // any third-party resource the destination page loads.
    resp.headers_mut().insert(
        header::REFERRER_POLICY,
        HeaderValue::from_static("no-referrer"),
    );
    tracing::info!(
        target: "rustango::tenancy::operator_console",
        slug = %slug,
        operator_id,
        ttl_secs = handoff::HANDOFF_TTL_SECS,
        redirect_to = %redirect_to,
        "minted impersonation handoff token",
    );
    resp
}

/// #1526. These exercise `sanitize_next` — the function the login
/// handler actually calls, whose result reaches `Redirect::to` — and
/// not the public `urls::url_has_allowed_host_and_scheme` helper,
/// which this codebase never calls and which the original fix went
/// into.
#[cfg(test)]
mod sanitize_next_tests {
    use super::sanitize_next;

    #[test]
    fn a_backslash_the_browser_rewrites_is_refused() {
        // Each reaches the network as protocol-relative `//evil…`
        // after the browser's `\` → `/` rewrite (WHATWG URL 4.4),
        // while starting with `/` in the source text.
        for hostile in [
            "/\\evil.example/x",
            "/\\\\evil.example/x",
            "\\/evil.example/x",
            "/%5Cevil.example/x",
        ] {
            assert_eq!(
                sanitize_next(Some(hostile)),
                "/",
                "`{hostile}` must not reach Location",
            );
        }
    }

    #[test]
    fn the_classic_shapes_are_still_refused() {
        for hostile in [
            "//evil.example/x",
            "https://evil.example",
            "javascript:1",
            // The browser strips TAB, CR and LF while parsing a URL
            // (WHATWG URL 4.1), so each of these leaves as the
            // protocol-relative `//evil.example` (#1604 security-001).
            "/\x09/evil.example/x",
            "/\x0d/evil.example/x",
            "/\x0a/evil.example/x",
        ] {
            assert_eq!(sanitize_next(Some(hostile)), "/", "{hostile}");
        }
        assert_eq!(sanitize_next(None), "/");
    }

    #[test]
    fn an_ordinary_path_still_survives() {
        // The control: tightening must not send every operator to `/`
        // after login, which would pass the assertions above.
        for ok in ["/orgs", "/orgs/acme/edit", "/orgs?page=2&q=a"] {
            assert_eq!(sanitize_next(Some(ok)), ok, "{ok} should survive");
        }
    }
}

#[cfg(test)]
mod redirect_to_login_tests {
    use super::redirect_to_login;

    /// `next` is fully encoded and decodes back to the path it came from.
    #[test]
    fn next_round_trips_through_the_location() {
        let resp = redirect_to_login("/orgs/acme edit?tab=1&x=2");
        let loc = resp.headers()[axum::http::header::LOCATION]
            .to_str()
            .unwrap();
        assert_eq!(loc, "/login?next=%2Forgs%2Facme%20edit%3Ftab%3D1%26x%3D2");
        let next = loc.strip_prefix("/login?next=").unwrap();
        assert_eq!(
            crate::url_codec::url_decode(next),
            "/orgs/acme edit?tab=1&x=2"
        );
    }
}

#[cfg(test)]
mod sanitize_next_method_tests {
    use super::sanitize_next_for_method;
    use axum::http::Method;

    // v0.27.10 (#68) — guard against the regression that made
    // a POST → /login?next=… → POST chain land on a 405.

    #[test]
    fn get_passes_through_unchanged() {
        assert_eq!(
            sanitize_next_for_method(&Method::GET, "/orgs/acme/edit"),
            "/orgs/acme/edit"
        );
        assert_eq!(
            sanitize_next_for_method(&Method::HEAD, "/anywhere"),
            "/anywhere"
        );
    }

    #[test]
    fn post_to_branding_rewrites_to_parent_edit() {
        assert_eq!(
            sanitize_next_for_method(&Method::POST, "/orgs/acme/edit/branding"),
            "/orgs/acme/edit"
        );
    }

    #[test]
    fn post_to_impersonate_rewrites_to_parent_edit() {
        assert_eq!(
            sanitize_next_for_method(&Method::POST, "/orgs/acme/impersonate"),
            "/orgs/acme/edit"
        );
    }

    #[test]
    fn post_to_edit_rewrites_to_get_edit_form() {
        assert_eq!(
            sanitize_next_for_method(&Method::POST, "/orgs/acme/edit"),
            "/orgs/acme/edit"
        );
    }

    #[test]
    fn unknown_post_path_falls_back_to_root() {
        assert_eq!(
            sanitize_next_for_method(&Method::POST, "/some/random/post"),
            "/"
        );
    }

    #[test]
    fn query_string_dropped_from_rewrite_target() {
        // Operator submitted a form with extra query — we don't
        // try to preserve it across the rewrite. The point is
        // that they land on a GET-able page, not that we
        // re-execute the form.
        assert_eq!(
            sanitize_next_for_method(&Method::POST, "/orgs/acme/impersonate?return=foo"),
            "/orgs/acme/edit"
        );
    }
}

#[cfg(test)]
mod opbrand_tests {
    use super::OpBrand;

    /// Hardcoded fallback values when nothing is configured.
    #[test]
    fn defaults_match_documented_values() {
        let b = OpBrand::defaults();
        assert_eq!(b.name, "Rustango");
        assert_eq!(b.theme_mode, "auto");
        assert!(b.tagline.is_none());
        assert!(b.primary_color.is_none());
        assert_eq!(b.logo_url, "/__static__/rustango.png");
    }

    /// `BrandSettings` overrides the defaults — but the function
    /// stays pure (no env reads), so the test doesn't need to
    /// poke `std::env::set_var` (forbidden by workspace lint).
    #[cfg(feature = "config")]
    #[test]
    fn apply_brand_settings_overrides_defaults() {
        let mut b = OpBrand::defaults();
        let mut s = crate::config::BrandSettings::default();
        s.name = Some("Acme Operator".into());
        s.tagline = Some("(prod)".into());
        s.primary_color = Some("#ff8800".into());
        s.theme_mode = Some("dark".into());
        OpBrand::apply_brand_settings(&mut b, &s);
        assert_eq!(b.name, "Acme Operator");
        assert_eq!(b.tagline.as_deref(), Some("(prod)"));
        assert_eq!(b.primary_color.as_deref(), Some("#ff8800"));
        assert_eq!(b.theme_mode, "dark");
    }

    /// Empty strings in TOML don't override (different from
    /// "explicitly absent" — a user typing `name = ""` almost
    /// certainly meant the default, not the empty string).
    #[cfg(feature = "config")]
    #[test]
    fn apply_brand_settings_empty_strings_skip() {
        let mut b = OpBrand::defaults();
        let original = b.name.clone();
        let mut s = crate::config::BrandSettings::default();
        s.name = Some(String::new());
        OpBrand::apply_brand_settings(&mut b, &s);
        assert_eq!(b.name, original);
    }

    /// Invalid hex colors are dropped, not propagated. Matches the
    /// `from_env` pre-#87 behavior — bad input falls through to
    /// the default.
    #[cfg(feature = "config")]
    #[test]
    fn apply_brand_settings_rejects_bad_hex() {
        let mut b = OpBrand::defaults();
        let mut s = crate::config::BrandSettings::default();
        s.primary_color = Some("not-a-color".into());
        OpBrand::apply_brand_settings(&mut b, &s);
        assert!(b.primary_color.is_none());
    }

    /// Invalid theme_mode values are dropped — the validator
    /// only accepts `auto` / `light` / `dark`.
    #[cfg(feature = "config")]
    #[test]
    fn apply_brand_settings_rejects_bad_theme_mode() {
        let mut b = OpBrand::defaults();
        let original = b.theme_mode.clone();
        let mut s = crate::config::BrandSettings::default();
        s.theme_mode = Some("midnight".into());
        OpBrand::apply_brand_settings(&mut b, &s);
        assert_eq!(b.theme_mode, original);
    }
}