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
//! Versioned banlist settings files.

// NOTE: Needed to allow the second-to-last migration to call try_into().

use super::{BANLIST_FILENAME as FILENAME, MIGRATION_UPGRADE_GUARANTEE};
use crate::settings::editable::{EditableSetting, Version};
use authc::Uuid;
use core::convert::{TryFrom, TryInto};
use serde::{Deserialize, Serialize};

/// NOTE: Always replace this with the latest banlist version. Then update the
/// BanlistRaw, the TryFrom<BanlistRaw> for Banlist, the previously most recent
/// module, and add a new module for the latest version!  Please respect the
/// migration upgrade guarantee found in the parent module with any upgrade.
pub use self::v2::*;

/// Versioned settings files, one per version (v0 is only here as an example; we
/// do not expect to see any actual v0 settings files).
#[derive(Deserialize, Serialize)]
pub enum BanlistRaw {
    V0(v0::Banlist),
    V1(v1::Banlist),
    V2(v2::Banlist),
}

impl From<Banlist> for BanlistRaw {
    fn from(value: Banlist) -> Self {
        // Replace variant with that of current latest version.
        Self::V2(value)
    }
}

impl TryFrom<BanlistRaw> for (Version, Banlist) {
    type Error = <Banlist as EditableSetting>::Error;

    fn try_from(value: BanlistRaw) -> Result<Self, <Banlist as EditableSetting>::Error> {
        use BanlistRaw::*;
        Ok(match value {
            // Old versions
            V0(value) => (Version::Old, value.try_into()?),
            V1(value) => (Version::Old, value.try_into()?),
            // Latest version (move to old section using the pattern of other old version when it
            // is no longer latest).
            V2(mut value) => (value.validate()?, value),
        })
    }
}

type Final = Banlist;

impl EditableSetting for Banlist {
    type Error = BanError;
    type Legacy = legacy::Banlist;
    type Setting = BanlistRaw;

    const FILENAME: &'static str = FILENAME;
}

#[derive(Clone, Copy, Debug)]
pub enum BanKind {
    Ban,
    Unban,
}

#[derive(Clone, Copy, Debug)]
pub enum BanErrorKind {
    /// An end date went past a start date.
    InvalidDateRange {
        start_date: chrono::DateTime<chrono::Utc>,
        end_date: chrono::DateTime<chrono::Utc>,
    },
    /// Cannot unban an already-unbanned user.
    AlreadyUnbanned,
    /// Permission denied to perform requested action.
    PermissionDenied(BanKind),
    /// Cannot have a UUID linked IP ban if there is not a corresponding UUID
    /// ban. In this case the corresponding entry in the UUID ban map is missing
    /// completely.
    CorrespondingUuidBanMissing,
    /// Cannot have a UUID linked IP ban if there is not a corresponding UUID
    /// ban. In this case there is a corresponding entry, but it may be an unban
    /// instead of a ban, the expiration date may not match, or the ban info
    /// doesn't match.
    CorrespondingUuidBanMismatch,
    /// Ban info is an optional field to support legacy data, of which IP bans
    /// and their linked UUID bans are not included.
    NonLegacyBanMissingBanInfo,
    /// Multiple active IP bans should not link to the same UUID since that UUID
    /// should also be banned (and thus no IPs can be banned via that user).
    ActiveIpBansShareUuid,
}

#[derive(Debug)]
pub enum BanError {
    Uuid {
        kind: BanErrorKind,
        /// Uuid of affected user
        uuid: Uuid,
        /// Username of affected user (as of ban/unban time).
        username: String,
    },
    // Note, we specifically don't expose the IP address here since this is
    // shown to users of the ban commands.
    Ip {
        kind: BanErrorKind,
        /// Uuid of affected user
        uuid: Option<Uuid>,
        /// `username_when_performed` from the associated uuid ban entry, if the
        /// associated entry is missing (which would cause a validation
        /// error) or there is no associated entry (uuid is None) then
        /// this will be None.
        username_from_uuid_entry: Option<String>,
    },
}

/// NOTE: This isn't serialized so we can place it outside the versioned
/// modules.
///
/// `BanAction` name already taken.
///
/// Note, `IpBan` will also apply a regular ban, while `UnbanIp` will only
/// remove the IP ban.
pub enum BanOperation {
    // We don't use `Ban` struct because the info field is optional for
    // legacy reasons.
    Ban {
        reason: String,
        info: BanInfo,
        /// NOTE: Should always be higher than the `now` date provided to
        /// [`BanList::ban_operation`] , if this is present!
        end_date: Option<chrono::DateTime<chrono::Utc>>,
    },
    BanIp {
        reason: String,
        info: BanInfo,
        /// NOTE: Should always be higher than the `now` date provided to
        /// [`BanList::ban_operation`] , if this is present!
        end_date: Option<chrono::DateTime<chrono::Utc>>,
        ip: NormalizedIpAddr,
    },
    Unban {
        info: BanInfo,
    },
    UnbanIp {
        info: BanInfo,
        /// The Uuid linked to the IP ban (currently no functionality to created
        /// or remove non-uuid linked IP bans even though the model can
        /// support them)
        uuid: Uuid,
    },
}

#[derive(Debug)]
pub enum BanOperationError {
    /// Operation cancelled without performing any changes for some reason.
    NoEffect,
    /// Validation or IO error.
    EditFailed(crate::settings::editable::Error<Final>),
}

mod legacy {
    use super::{v0 as next, Final, MIGRATION_UPGRADE_GUARANTEE};
    use authc::Uuid;
    use core::convert::TryInto;
    use hashbrown::HashMap;
    use serde::{Deserialize, Serialize};

    #[derive(Deserialize, Serialize)]
    pub struct BanRecord {
        pub username_when_banned: String,
        pub reason: String,
    }

    #[derive(Deserialize, Serialize, Default)]
    #[serde(transparent)]
    pub struct Banlist(pub(super) HashMap<Uuid, BanRecord>);

    impl From<Banlist> for Final {
        /// Legacy migrations can be migrated to the latest version through the
        /// process of "chaining" migrations, starting from
        /// `next::Banlist`.
        ///
        /// Note that legacy files are always valid, which is why we implement
        /// From rather than TryFrom.
        fn from(value: Banlist) -> Self {
            next::Banlist::migrate(value)
                .try_into()
                .expect(MIGRATION_UPGRADE_GUARANTEE)
        }
    }
}

/// This module represents a banlist version that isn't actually used.  It is
/// here and part of the migration process to provide an example for how to
/// perform a migration for an old version; please use this as a reference when
/// constructing new migrations.
mod v0 {
    use super::{legacy as prev, v1 as next, Final, MIGRATION_UPGRADE_GUARANTEE};
    use crate::settings::editable::{EditableSetting, Version};
    use authc::Uuid;
    use core::convert::{TryFrom, TryInto};
    use hashbrown::HashMap;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Deserialize, Serialize)]
    pub struct BanRecord {
        pub username_when_banned: String,
        pub reason: String,
    }

    #[derive(Clone, Deserialize, Serialize, Default)]
    #[serde(transparent)]
    pub struct Banlist(pub(super) HashMap<Uuid, BanRecord>);

    impl Banlist {
        /// One-off migration from the previous version.  This must be
        /// guaranteed to produce a valid settings file as long as it is
        /// called with a valid settings file from the previous version.
        pub(super) fn migrate(prev: prev::Banlist) -> Self {
            Banlist(
                prev.0
                    .into_iter()
                    .map(
                        |(
                            uid,
                            prev::BanRecord {
                                username_when_banned,
                                reason,
                            },
                        )| {
                            (uid, BanRecord {
                                username_when_banned,
                                reason,
                            })
                        },
                    )
                    .collect(),
            )
        }

        /// Perform any needed validation on this banlist that can't be done
        /// using parsing.
        ///
        /// The returned version being "Old" indicates the loaded setting has
        /// been modified during validation (this is why validate takes
        /// `&mut self`).
        pub(super) fn validate(&mut self) -> Result<Version, <Final as EditableSetting>::Error> {
            Ok(Version::Latest)
        }
    }

    /// Pretty much every TryFrom implementation except that of the very last
    /// version should look exactly like this.
    impl TryFrom<Banlist> for Final {
        type Error = <Final as EditableSetting>::Error;

        fn try_from(mut value: Banlist) -> Result<Final, Self::Error> {
            value.validate()?;
            Ok(next::Banlist::migrate(value)
                .try_into()
                .expect(MIGRATION_UPGRADE_GUARANTEE))
        }
    }
}

mod v1 {
    use super::{
        v0 as prev, v2 as next, BanError, BanErrorKind, BanKind, Final, MIGRATION_UPGRADE_GUARANTEE,
    };
    use crate::settings::editable::{EditableSetting, Version};
    use authc::Uuid;
    use chrono::{prelude::*, Utc};
    use common::comp::AdminRole;
    use core::ops::Deref;
    use hashbrown::HashMap;
    use serde::{Deserialize, Serialize};
    use tracing::warn;

    /// Important: even if the role we are storing here appears to be identical
    /// to one used in another versioned store (like admin::Role), we *must*
    /// have our own versioned copy!  This ensures that if there's an update
    /// to the role somewhere else, the conversion function between them
    /// will break, letting people make an intelligent decision.
    ///
    /// In particular, *never remove variants from this enum* (or any other enum
    /// in a versioned settings file) without bumping the version and
    /// writing a migration that understands how to properly deal with
    /// existing instances of the old variant (you can delete From instances
    /// for the old variants at this point).  Otherwise, we will lose
    /// compatibility with old settings files, since we won't be able to
    /// deserialize them!
    #[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
    pub enum Role {
        Moderator = 0,
        Admin = 1,
    }

    impl From<AdminRole> for Role {
        fn from(value: AdminRole) -> Self {
            match value {
                AdminRole::Moderator => Self::Moderator,
                AdminRole::Admin => Self::Admin,
            }
        }
    }

    impl From<Role> for AdminRole {
        fn from(value: Role) -> Self {
            match value {
                Role::Moderator => Self::Moderator,
                Role::Admin => Self::Admin,
            }
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    /// NOTE: May not be present if performed from the command line or from a
    /// legacy file.
    pub struct BanInfo {
        pub performed_by: Uuid,
        /// NOTE: May not be up to date, if we allow username changes.
        pub performed_by_username: String,
        /// NOTE: Role of the banning user at the time of the ban.
        pub performed_by_role: Role,
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct Ban {
        pub reason: String,
        /// NOTE: Should only be None for migrations from legacy data.
        pub info: Option<BanInfo>,
        /// NOTE: Should always be higher than start_date, if both are
        /// present!
        pub end_date: Option<DateTime<Utc>>,
    }

    impl Ban {
        /// Returns true if the ban is expired, false otherwise.
        pub fn is_expired(&self, now: DateTime<Utc>) -> bool {
            self.end_date.map_or(false, |end_date| end_date <= now)
        }

        pub fn performed_by_role(&self) -> Role {
            self.info.as_ref().map(|info| info.performed_by_role)
                // We know all legacy bans were performed by an admin, since we had no other roles
                // at the time.
                .unwrap_or(Role::Admin)
        }
    }

    type Unban = BanInfo;

    #[derive(Clone, Deserialize, Serialize)]
    pub enum BanAction {
        Unban(Unban),
        Ban(Ban),
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct BanRecord {
        /// Username of the user upon whom the action was performed, when it was
        /// performed.
        pub username_when_performed: String,
        pub action: BanAction,
        /// NOTE: When migrating from legacy versions, this will just be the
        /// time of the first migration (only applies to BanRecord).
        pub date: DateTime<Utc>,
    }

    impl BanRecord {
        /// Returns true if this record represents an expired ban, false
        /// otherwise.
        fn is_expired(&self, now: DateTime<Utc>) -> bool {
            match &self.action {
                BanAction::Ban(ban) => ban.is_expired(now),
                BanAction::Unban(_) => true,
            }
        }

        /// The history vector in a BanEntry is stored forwards (from oldest
        /// entry to newest), so `prev_record` is the previous entry in
        /// this vector when iterating forwards (by array index).
        ///
        /// Errors are:
        ///
        /// AlreadyUnbanned if an unban comes after anything but a ban.
        ///
        /// Permission(Unban) if an unban attempt is by a user with a lower role
        /// level than the original banning party.
        ///
        /// PermissionDenied(Ban) if a ban length is made shorter by a user with
        /// a role level than the original banning party.
        ///
        /// InvalidDateRange if the end date of the ban exceeds the start date.
        fn validate(&self, prev_record: Option<&BanRecord>) -> Result<(), BanErrorKind> {
            // Check to make sure the actions temporally line up--if they don't, we will
            // prevent warn an administrator (since this may indicate a system
            // clock issue and could require manual editing to resolve).
            // However, we will not actually invalidate the ban list for this, in case
            // this would otherwise prevent people from adding a new ban.
            //
            // We also deliberately leave the bad order intact, in case this reflects
            // history more accurately than the system clock does.
            if let Some(prev_record) = prev_record {
                if prev_record.date > self.date {
                    warn!(
                        "Ban list history is inconsistent, or a just-added ban was behind a \
                         historical entry in the ban
                          record; please investigate the contents of the file (might indicate a \
                         system clock change?)."
                    );
                }
            }
            let ban = match (&self.action, prev_record.map(|record| &record.action)) {
                // A ban is always valid if it follows an unban.
                (BanAction::Ban(ban), None) | (BanAction::Ban(ban), Some(BanAction::Unban(_))) => {
                    ban
                },
                // A ban record following a ban is valid if either the role of the person doing the
                // banning is at least the privilege level of the person who did the ban, or the
                // ban's new end time is at least the previous end time.
                (BanAction::Ban(new_ban), Some(BanAction::Ban(old_ban))) => {
                    match (new_ban.end_date, old_ban.end_date) {
                        // New role ≥ old role
                        _ if new_ban.performed_by_role() >= old_ban.performed_by_role() => new_ban,
                        // Permanent ban retracted to temp ban.
                        (Some(_), None) => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Temp ban retracted to shorter temp ban.
                        (Some(new_date), Some(old_date)) if new_date < old_date => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Anything else (extension to permanent ban, or temp ban extension to
                        // longer temp ban).
                        _ => new_ban,
                    }
                },
                // An unban record is invalid if it does not follow a ban.
                (BanAction::Unban(_), None) | (BanAction::Unban(_), Some(BanAction::Unban(_))) => {
                    return Err(BanErrorKind::AlreadyUnbanned);
                },
                // An unban record following a ban is valid if the role of the person doing the
                // unbanning is at least the privilege level of the person who did the ban.
                (BanAction::Unban(unban), Some(BanAction::Ban(ban))) => {
                    return if unban.performed_by_role >= ban.performed_by_role() {
                        Ok(())
                    } else {
                        Err(BanErrorKind::PermissionDenied(BanKind::Unban))
                    };
                },
            };

            // End date of a ban must be at least as big as the start date.
            if let Some(end_date) = ban.end_date {
                if self.date > end_date {
                    return Err(BanErrorKind::InvalidDateRange {
                        start_date: self.date,
                        end_date,
                    });
                }
            }
            Ok(())
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct BanEntry {
        /// The latest ban record for this user.
        pub current: BanRecord,
        /// Historical ban records for this user, stored in order from oldest to
        /// newest.
        pub history: Vec<BanRecord>,
        /// A *hint* about whether the system thinks this entry is expired,
        /// mostly to make it easier for someone manually going through
        /// a file to see whether an entry is currently in effect or
        /// not.  This is based off the contents of `current`.
        pub expired: bool,
    }

    impl Deref for BanEntry {
        type Target = BanRecord;

        fn deref(&self) -> &Self::Target { &self.current }
    }

    impl BanEntry {
        /// Both validates, and updates the hint bit if it's inconsistent with
        /// reality.
        ///
        /// If we were invalid, returns an error.  Otherwise, returns Ok(v),
        /// where v is Latest if the hint bit was modified, Old
        /// otherwise.
        fn validate(
            &mut self,
            now: DateTime<Utc>,
            uuid: Uuid,
        ) -> Result<Version, <Final as EditableSetting>::Error> {
            let make_error = |current_entry: &BanRecord| {
                let username = current_entry.username_when_performed.clone();
                move |kind| BanError::Uuid {
                    kind,
                    uuid,
                    username,
                }
            };
            // First, go forwards through history (also forwards in terms of the iterator
            // direction), validating each entry in turn.
            let mut prev_entry = None;
            for current_entry in &self.history {
                current_entry
                    .validate(prev_entry)
                    .map_err(make_error(current_entry))?;
                prev_entry = Some(current_entry);
            }

            // History has now been validated, so validate the current entry.
            self.current
                .validate(prev_entry)
                .map_err(make_error(&self.current))?;

            // Make sure the expired hint is correct, and if not indicate that we should
            // resave the file.
            let is_expired = self.current.is_expired(now);
            if self.expired != is_expired {
                self.expired = is_expired;
                Ok(Version::Old)
            } else {
                Ok(Version::Latest)
            }
        }
    }

    #[derive(Clone, Deserialize, Serialize, Default)]
    #[serde(transparent)]
    pub struct Banlist(pub(super) HashMap<Uuid, BanEntry>);

    impl Banlist {
        /// One-off migration from the previous version.  This must be
        /// guaranteed to produce a valid settings file as long as it is
        /// called with a valid settings file from the previous version.
        pub(super) fn migrate(prev: prev::Banlist) -> Self {
            // The ban start date for migrations from legacy is the current one; we could
            // record that they actually have an unknown start date, but this
            // would just complicate the format.
            let date = Utc::now();
            Banlist(
                prev.0
                    .into_iter()
                    .map(
                        |(
                            uuid,
                            prev::BanRecord {
                                username_when_banned,
                                reason,
                            },
                        )| {
                            (uuid, BanEntry {
                                current: BanRecord {
                                    username_when_performed: username_when_banned,
                                    // We only recorded unbans pre-migration.
                                    action: BanAction::Ban(Ban {
                                        reason,
                                        // We don't know who banned this user pre-migration.
                                        info: None,
                                        // All bans pre-migration are of unlimited duration.
                                        end_date: None,
                                    }),
                                    date,
                                },
                                // Old bans never expire, so set the expiration hint to false.
                                expired: false,
                                // There is no known ban history yet.
                                history: Vec::new(),
                            })
                        },
                    )
                    .collect(),
            )
        }

        /// Perform any needed validation on this banlist that can't be done
        /// using parsing.
        ///
        /// The returned version being "Old" indicates the loaded setting has
        /// been modified during validation (this is why validate takes
        /// `&mut self`).
        pub(super) fn validate(&mut self) -> Result<Version, <Final as EditableSetting>::Error> {
            let mut version = Version::Latest;
            let now = Utc::now();
            for (&uuid, value) in self.0.iter_mut() {
                if matches!(value.validate(now, uuid)?, Version::Old) {
                    // Update detected.
                    version = Version::Old;
                }
            }
            Ok(version)
        }
    }

    impl TryFrom<Banlist> for Final {
        type Error = <Final as EditableSetting>::Error;

        #[allow(clippy::useless_conversion)]
        fn try_from(mut value: Banlist) -> Result<Final, Self::Error> {
            value.validate()?;
            Ok(next::Banlist::migrate(value)
                .try_into()
                .expect(MIGRATION_UPGRADE_GUARANTEE))
        }
    }
}

mod v2 {
    use super::{
        v1 as prev, BanError, BanErrorKind, BanKind, BanOperation, BanOperationError, Final,
    };
    use crate::settings::editable::{EditableSetting, Version};
    use authc::Uuid;
    use chrono::{prelude::*, Utc};
    use common::comp::AdminRole;
    use core::{mem, ops::Deref};
    use hashbrown::{hash_map, HashMap};
    use serde::{Deserialize, Serialize};
    use std::net::{IpAddr, Ipv6Addr};
    use tracing::warn;
    /* use super::v3 as next; */

    /// Important: even if the role we are storing here appears to be identical
    /// to one used in another versioned store (like admin::Role), we *must*
    /// have our own versioned copy!  This ensures that if there's an update
    /// to the role somewhere else, the conversion function between them
    /// will break, letting people make an intelligent decision.
    ///
    /// In particular, *never remove variants from this enum* (or any other enum
    /// in a versioned settings file) without bumping the version and
    /// writing a migration that understands how to properly deal with
    /// existing instances of the old variant (you can delete From instances
    /// for the old variants at this point).  Otherwise, we will lose
    /// compatibility with old settings files, since we won't be able to
    /// deserialize them!
    #[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
    pub enum Role {
        Moderator = 0,
        Admin = 1,
    }

    impl From<AdminRole> for Role {
        fn from(value: AdminRole) -> Self {
            match value {
                AdminRole::Moderator => Self::Moderator,
                AdminRole::Admin => Self::Admin,
            }
        }
    }

    impl From<Role> for AdminRole {
        fn from(value: Role) -> Self {
            match value {
                Role::Moderator => Self::Moderator,
                Role::Admin => Self::Admin,
            }
        }
    }

    #[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
    /// NOTE: May not be present if performed from the command line or from a
    /// legacy file.
    pub struct BanInfo {
        pub performed_by: Uuid,
        /// NOTE: May not be up to date, if we allow username changes.
        pub performed_by_username: String,
        /// NOTE: Role of the banning user at the time of the ban.
        pub performed_by_role: Role,
    }

    impl BanInfo {
        fn migrate(
            prev::BanInfo {
                performed_by,
                performed_by_username,
                performed_by_role,
            }: prev::BanInfo,
        ) -> Self {
            Self {
                performed_by,
                performed_by_username,
                performed_by_role: match performed_by_role {
                    prev::Role::Moderator => Role::Moderator,
                    prev::Role::Admin => Role::Admin,
                },
            }
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct Ban {
        pub reason: String,
        /// NOTE: Should only be None for migrations from legacy data.
        pub info: Option<BanInfo>,
        /// NOTE: Should always be higher than the `date` in the record
        /// containing this, if this is present!
        pub end_date: Option<DateTime<Utc>>,
    }

    impl Ban {
        /// Returns true if the ban is expired, false otherwise.
        pub fn is_expired(&self, now: DateTime<Utc>) -> bool {
            self.end_date.map_or(false, |end_date| end_date <= now)
        }

        pub fn performed_by_role(&self) -> Role {
            self.info.as_ref().map(|info| info.performed_by_role)
                // We know all legacy bans were performed by an admin, since we had no other roles
                // at the time.
                .unwrap_or(Role::Admin)
        }

        pub fn info(&self) -> common_net::msg::server::BanInfo {
            common_net::msg::server::BanInfo {
                reason: self.reason.clone(),
                until: self.end_date.map(|date| date.timestamp()),
            }
        }
    }

    type Unban = BanInfo;

    #[derive(Clone, Deserialize, Serialize)]
    pub enum BanAction {
        Unban(Unban),
        Ban(Ban),
    }

    impl BanAction {
        pub fn ban(&self) -> Option<&Ban> {
            match self {
                BanAction::Unban(_) => None,
                BanAction::Ban(ban) => Some(ban),
            }
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct BanRecord {
        /// Username of the user upon whom the action was performed, when it was
        /// performed.
        pub username_when_performed: String,
        pub action: BanAction,
        /// NOTE: When migrating from legacy versions, this will just be the
        /// time of the first migration (only applies to BanRecord).
        pub date: DateTime<Utc>,
    }

    impl BanRecord {
        /// Returns true if this record represents an expired ban, false
        /// otherwise.
        fn is_expired(&self, now: DateTime<Utc>) -> bool {
            match &self.action {
                BanAction::Ban(ban) => ban.is_expired(now),
                BanAction::Unban(_) => true,
            }
        }

        fn migrate(
            prev::BanRecord {
                username_when_performed,
                action,
                date,
            }: prev::BanRecord,
        ) -> Self {
            BanRecord {
                username_when_performed,
                action: match action {
                    prev::BanAction::Ban(prev::Ban {
                        reason,
                        info,
                        end_date,
                    }) => BanAction::Ban(Ban {
                        reason,
                        info: info.map(BanInfo::migrate),
                        end_date,
                    }),
                    prev::BanAction::Unban(info) => BanAction::Unban(BanInfo::migrate(info)),
                },
                date,
            }
        }

        /// The history vector in a BanEntry is stored forwards (from oldest
        /// entry to newest), so `prev_record` is the previous entry in
        /// this vector when iterating forwards (by array index).
        ///
        /// Errors are:
        ///
        /// AlreadyUnbanned if an unban comes after anything but a ban.
        ///
        /// Permission(Unban) if an unban attempt is by a user with a lower role
        /// level than the original banning party.
        ///
        /// PermissionDenied(Ban) if a ban length is made shorter by a user with
        /// a role level than the original banning party.
        ///
        /// InvalidDateRange if the end date of the ban exceeds the start date.
        fn validate(&self, prev_record: Option<&BanRecord>) -> Result<(), BanErrorKind> {
            // Check to make sure the actions temporally line up--if they don't, we will
            // prevent warn an administrator (since this may indicate a system
            // clock issue and could require manual editing to resolve).
            // However, we will not actually invalidate the ban list for this, in case
            // this would otherwise prevent people from adding a new ban.
            //
            // We also deliberately leave the bad order intact, in case this reflects
            // history more accurately than the system clock does.
            if let Some(prev_record) = prev_record {
                if prev_record.date > self.date {
                    warn!(
                        "Ban list history is inconsistent, or a just-added ban was behind a \
                         historical entry in the ban
                          record; please investigate the contents of the file (might indicate a \
                         system clock change?)."
                    );
                }
            }
            let ban = match (&self.action, prev_record.map(|record| &record.action)) {
                // A ban is always valid if it follows an unban.
                (BanAction::Ban(ban), None) | (BanAction::Ban(ban), Some(BanAction::Unban(_))) => {
                    ban
                },
                // A ban record following a ban is valid if either the role of the person doing the
                // banning is at least the privilege level of the person who did the ban, or the
                // ban's new end time is at least the previous end time.
                (BanAction::Ban(new_ban), Some(BanAction::Ban(old_ban))) => {
                    match (new_ban.end_date, old_ban.end_date) {
                        // New role ≥ old role
                        _ if new_ban.performed_by_role() >= old_ban.performed_by_role() => new_ban,
                        // Permanent ban retracted to temp ban.
                        (Some(_), None) => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Temp ban retracted to shorter temp ban.
                        (Some(new_date), Some(old_date)) if new_date < old_date => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Anything else (extension to permanent ban, or temp ban extension to
                        // longer temp ban).
                        _ => new_ban,
                    }
                },
                // An unban record is invalid if it does not follow a ban.
                (BanAction::Unban(_), None) | (BanAction::Unban(_), Some(BanAction::Unban(_))) => {
                    return Err(BanErrorKind::AlreadyUnbanned);
                },
                // An unban record following a ban is valid if the role of the person doing the
                // unbanning is at least the privilege level of the person who did the ban.
                (BanAction::Unban(unban), Some(BanAction::Ban(ban))) => {
                    return if unban.performed_by_role >= ban.performed_by_role() {
                        Ok(())
                    } else {
                        Err(BanErrorKind::PermissionDenied(BanKind::Unban))
                    };
                },
            };

            // End date of a ban must be at least as big as the start date.
            if let Some(end_date) = ban.end_date {
                if self.date > end_date {
                    return Err(BanErrorKind::InvalidDateRange {
                        start_date: self.date,
                        end_date,
                    });
                }
            }
            Ok(())
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct BanEntry {
        /// The latest ban record for this user.
        pub current: BanRecord,
        /// Historical ban records for this user, stored in order from oldest to
        /// newest.
        pub history: Vec<BanRecord>,
        /// A *hint* about whether the system thinks this entry is expired,
        /// mostly to make it easier for someone manually going through
        /// a file to see whether an entry is currently in effect or
        /// not.  This is based off the contents of `current`.
        pub expired: bool,
    }

    impl Deref for BanEntry {
        type Target = BanRecord;

        fn deref(&self) -> &Self::Target { &self.current }
    }

    impl BanEntry {
        fn migrate(
            prev::BanEntry {
                current,
                history,
                expired,
            }: prev::BanEntry,
        ) -> Self {
            Self {
                current: BanRecord::migrate(current),
                history: history.into_iter().map(BanRecord::migrate).collect(),
                expired,
            }
        }

        /// Both validates, and updates the hint bit if it's inconsistent with
        /// reality.
        ///
        /// If we were invalid, returns an error.  Otherwise, returns Ok(v),
        /// where v is Latest if the hint bit was modified, Old
        /// otherwise.
        fn validate(
            &mut self,
            now: DateTime<Utc>,
            uuid: Uuid,
        ) -> Result<Version, <Final as EditableSetting>::Error> {
            let make_error = |kind, current_entry: &BanRecord| BanError::Uuid {
                kind,
                uuid,
                username: current_entry.username_when_performed.clone(),
            };
            // First, go forwards through history (also forwards in terms of the iterator
            // direction), validating each entry in turn.
            let mut prev_entry = None;
            for current_entry in &self.history {
                current_entry
                    .validate(prev_entry)
                    .map_err(|kind| make_error(kind, current_entry))?;
                prev_entry = Some(current_entry);
            }

            // History has now been validated, so validate the current entry.
            self.current
                .validate(prev_entry)
                .map_err(|kind| make_error(kind, &self.current))?;

            // Make sure the expired hint is correct, and if not indicate that we should
            // resave the file.
            let is_expired = self.current.is_expired(now);
            if self.expired != is_expired {
                self.expired = is_expired;
                Ok(Version::Old)
            } else {
                Ok(Version::Latest)
            }
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct IpBanRecord {
        /// Uuid of the user through which this IP ban was applied.
        ///
        /// This is optional to allow for the potenital of non-user-associated
        /// IP bans.
        pub uuid_when_performed: Option<Uuid>,
        pub action: BanAction,
        /// NOTE: When migrating from legacy versions, this will just be the
        /// time of the first migration (only applies to BanRecord).
        pub date: DateTime<Utc>,
    }

    impl IpBanRecord {
        /// Returns true if this record represents an expired ban, false
        /// otherwise.
        fn is_expired(&self, now: DateTime<Utc>) -> bool {
            match &self.action {
                BanAction::Ban(ban) => ban.is_expired(now),
                BanAction::Unban(_) => true,
            }
        }

        /// The history vector in a IpBanEntry is stored forwards (from oldest
        /// entry to newest), so `prev_record` is the previous entry in
        /// this vector when iterating forwards (by array index).
        ///
        /// Errors are:
        ///
        /// AlreadyUnbanned if an unban comes after anything but a ban.
        ///
        /// Permission(Unban) if an unban attempt is by a user with a lower role
        /// level than the original banning party.
        ///
        /// PermissionDenied(Ban) if a ban length is made shorter by a user with
        /// a role level than the original banning party.
        ///
        /// InvalidDateRange if the end date of the ban exceeds the start date.
        fn validate(&self, prev_record: Option<&IpBanRecord>) -> Result<(), BanErrorKind> {
            // Check to make sure the actions temporally line up--if they don't, we will
            // prevent warn an administrator (since this may indicate a system
            // clock issue and could require manual editing to resolve).
            // However, we will not actually invalidate the ban list for this, in case
            // this would otherwise prevent people from adding a new ban.
            //
            // We also deliberately leave the bad order intact, in case this reflects
            // history more accurately than the system clock does.
            if let Some(prev_record) = prev_record {
                if prev_record.date > self.date {
                    warn!(
                        "Ban list history is inconsistent, or a just-added ban was behind a \
                         historical entry in the ban
                          record; please investigate the contents of the file (might indicate a \
                         system clock change?)."
                    );
                }
            }
            let ban = match (&self.action, prev_record.map(|record| &record.action)) {
                // A ban is always valid if it follows an unban.
                (BanAction::Ban(ban), None) | (BanAction::Ban(ban), Some(BanAction::Unban(_))) => {
                    ban
                },
                // A ban record following a ban is valid if either the role of the person doing the
                // banning is at least the privilege level of the person who did the ban, or the
                // ban's new end time is at least the previous end time.
                (BanAction::Ban(new_ban), Some(BanAction::Ban(old_ban))) => {
                    match (new_ban.end_date, old_ban.end_date) {
                        // New role ≥ old role
                        _ if new_ban.performed_by_role() >= old_ban.performed_by_role() => new_ban,
                        // Permanent ban retracted to temp ban.
                        (Some(_), None) => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Temp ban retracted to shorter temp ban.
                        (Some(new_date), Some(old_date)) if new_date < old_date => {
                            return Err(BanErrorKind::PermissionDenied(BanKind::Ban));
                        },
                        // Anything else (extension to permanent ban, or temp ban extension to
                        // longer temp ban).
                        _ => new_ban,
                    }
                },
                // An unban record is invalid if it does not follow a ban.
                (BanAction::Unban(_), None) | (BanAction::Unban(_), Some(BanAction::Unban(_))) => {
                    return Err(BanErrorKind::AlreadyUnbanned);
                },
                // An unban record following a ban is valid if the role of the person doing the
                // unbanning is at least the privilege level of the person who did the ban.
                (BanAction::Unban(unban), Some(BanAction::Ban(ban))) => {
                    return if unban.performed_by_role >= ban.performed_by_role() {
                        Ok(())
                    } else {
                        Err(BanErrorKind::PermissionDenied(BanKind::Unban))
                    };
                },
            };

            // End date of a ban must be at least as big as the start date.
            if let Some(end_date) = ban.end_date {
                if self.date > end_date {
                    return Err(BanErrorKind::InvalidDateRange {
                        start_date: self.date,
                        end_date,
                    });
                }
            }
            Ok(())
        }
    }

    #[derive(Clone, Deserialize, Serialize)]
    pub struct IpBanEntry {
        /// The latest ban record for this IP.
        ///
        /// Note: If this IP is currently banned and the current `BanRecord`
        /// contains a Uuid, then this user must also be banned in the
        /// Uuid ban map. This is enforced by the validation.
        pub current: IpBanRecord,
        /// Historical ban records for this user, stored in order from oldest to
        /// newest.
        pub history: Vec<IpBanRecord>,
        /// A *hint* about whether the system thinks this entry is expired,
        /// mostly to make it easier for someone manually going through
        /// a file to see whether an entry is currently in effect or
        /// not.  This is based off the contents of `current`.
        pub expired: bool,
    }

    impl Deref for IpBanEntry {
        type Target = IpBanRecord;

        fn deref(&self) -> &Self::Target { &self.current }
    }

    impl IpBanEntry {
        /// Both validates, and updates the hint bit if it's inconsistent with
        /// reality.
        ///
        /// If we were invalid, returns an error.  Otherwise, returns Ok(v),
        /// where v is Latest if the hint bit was modified, Old
        /// otherwise.
        fn validate(
            &mut self,
            now: DateTime<Utc>,
            uuid_bans: &HashMap<Uuid, BanEntry>,
        ) -> Result<Version, <Final as EditableSetting>::Error> {
            let make_error = |kind, current_entry: &IpBanRecord| {
                let uuid = current_entry.uuid_when_performed;

                BanError::Ip {
                    kind,
                    uuid,
                    username_from_uuid_entry: uuid
                        .and_then(|u| uuid_bans.get(&u))
                        .map(|e| e.current.username_when_performed.clone()),
                }
            };
            // First, go forwards through history (also forwards in terms of the iterator
            // direction), validating each entry in turn.
            let mut prev_entry = None;
            for current_entry in &self.history {
                current_entry
                    .validate(prev_entry)
                    .map_err(|kind| make_error(kind, current_entry))?;
                prev_entry = Some(current_entry);
            }

            // History has now been validated, so validate the current entry.
            self.current
                .validate(prev_entry)
                .map_err(|kind| make_error(kind, &self.current))?;

            // If the current entry is an unexpired ban and is linked to a uuid,
            // then that uuid must also be banned. These bans must also have the
            // same expiration and have matching `BanInfo`.
            if let Some(uuid) = self.current.uuid_when_performed {
                let uuid_entry = uuid_bans.get(&uuid).ok_or_else(|| {
                    make_error(BanErrorKind::CorrespondingUuidBanMissing, &self.current)
                })?;

                if let BanAction::Ban(ip_ban) = &self.current.action
                    && !self.current.is_expired(now)
                {
                    if let BanAction::Ban(uuid_ban) = &uuid_entry.current.action {
                        let ip_info = ip_ban.info.as_ref().ok_or_else(|| {
                            make_error(BanErrorKind::NonLegacyBanMissingBanInfo, &self.current)
                        })?;
                        let uuid_info = uuid_ban.info.as_ref().ok_or_else(|| {
                            make_error(BanErrorKind::NonLegacyBanMissingBanInfo, &self.current)
                        })?;

                        // Expiration time must match, so that the banned user
                        // cannot join and be banned from another IP address.
                        //
                        // BanInfo must match as well since these bans should
                        // have been performed by the same user.
                        if ip_ban.end_date == uuid_ban.end_date && ip_info == uuid_info {
                            Ok(())
                        } else {
                            Err(make_error(
                                BanErrorKind::CorrespondingUuidBanMismatch,
                                &self.current,
                            ))
                        }
                    } else {
                        Err(make_error(
                            BanErrorKind::CorrespondingUuidBanMismatch,
                            &self.current,
                        ))
                    }?;
                }
            }

            // Make sure the expired hint is correct, and if not indicate that we should
            // resave the file.
            let is_expired = self.current.is_expired(now);
            if self.expired != is_expired {
                self.expired = is_expired;
                Ok(Version::Old)
            } else {
                Ok(Version::Latest)
            }
        }
    }

    /// The last 64 bits of IPv6 addresess may vary a lot even when coming from
    /// the same client, and taking the full IPv6 for IP bans is thus useless.
    ///
    /// This newtype ensures that all the last 64 bits of an IPv6 address are
    /// set to zero to counter this.
    #[derive(Clone, Copy, Debug, Deserialize, Serialize, Hash, PartialEq, Eq)]
    #[serde(transparent)]
    pub struct NormalizedIpAddr(IpAddr);

    impl From<IpAddr> for NormalizedIpAddr {
        fn from(ip: IpAddr) -> Self {
            Self(match ip {
                // Take IPv4 adddresses as-is
                IpAddr::V4(ip) => IpAddr::V4(ip),
                // Ignore the last 64 bits for IPv6 addresses
                IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from_bits(
                    ip.to_bits() & 0xffff_ffff_ffff_ffff_0000_0000_0000_0000_u128,
                )),
            })
        }
    }

    impl Deref for NormalizedIpAddr {
        type Target = IpAddr;

        fn deref(&self) -> &Self::Target { &self.0 }
    }

    #[derive(Clone, Deserialize, Serialize, Default)]
    pub struct Banlist {
        pub(super) uuid_bans: HashMap<Uuid, BanEntry>,
        pub(super) ip_bans: HashMap<NormalizedIpAddr, IpBanEntry>,
    }

    impl Banlist {
        pub fn uuid_bans(&self) -> &HashMap<Uuid, BanEntry> { &self.uuid_bans }

        pub fn ip_bans(&self) -> &HashMap<NormalizedIpAddr, IpBanEntry> { &self.ip_bans }

        /// Attempt to perform the ban operation `operation` for the user with
        /// UUID `uuid` and username `username`, starting from time `now` (the
        /// information about the banning party will be in the `operation`
        /// record), with a settings file maintained at path root `data_dir`.
        ///
        /// Banning an IP via a user will also ban that user's UUID.
        /// Additionally, a regular UUID unban will also produce an IP unban if
        /// a corresponding one is active and linked to the unbanned UUID.
        ///
        /// If trying to unban an already unbanned player, or trying to ban but
        /// the ban status would not immediately change, the "overwrite" boolean
        /// should also be set to true.
        ///
        /// We try to detect duplicates (bans that would have no effect) and
        /// return `Err(BanOperationError::NoEffect)` if such effects are
        /// encountered.
        ///
        /// If the errors outlined above are successfully avoided, we attempt
        /// the edit either succeeding and returning `Ok(())` or returning
        /// `Err(BanOperationError::EditFailed(error))`, which works as follows.
        ///
        /// If the ban was invalid for any reason, then neither the in-memory
        /// banlist nor the on-disk banlist are modified.  If the ban
        /// entry is valid but the file encounters an error that
        /// prevents it from being atomically written to disk, we return an
        /// error but retain the change in memory.  Otherwise, we
        /// complete successfully and atomically write the banlist to
        /// disk.
        ///
        /// Note that the IO operation is only *guaranteed* atomic in the weak
        /// sense that either the whole page is written or it isn't; we
        /// cannot guarantee that the data we read in order to modify
        /// the file was definitely up to date, so we could be missing
        /// information if the file was manually edited or a function
        /// edits it without going through the usual specs resources.
        /// So, please be careful with ad hoc modifications to the file while
        /// the server is running.
        pub fn ban_operation(
            &mut self,
            data_dir: &std::path::Path,
            now: DateTime<Utc>,
            uuid: Uuid,
            username_when_performed: String,
            operation: BanOperation,
            overwrite: bool,
        ) -> Result<Option<common_net::msg::server::BanInfo>, BanOperationError> {
            let make_record = |action| BanRecord {
                username_when_performed,
                action,
                date: now,
            };
            let make_ip_record = |action| IpBanRecord {
                // Note: we may support banning IPs without associated user in the future.
                uuid_when_performed: Some(uuid),
                action,
                date: now,
            };

            // Perform an atomic edit.
            let edit_result = self.edit(data_dir.as_ref(), |banlist| {
                match operation {
                    BanOperation::Ban {
                        reason,
                        info,
                        end_date,
                    } => {
                        let ban = Ban {
                            reason,
                            info: Some(info),
                            end_date,
                        };
                        let frontend_info = ban.info();
                        let action = BanAction::Ban(ban);
                        let ban_record = make_record(action);
                        // NOTE: If there is linked IP ban, `overwrite` based changes may fail. In
                        // the future, we may want to switch this to autoupdate the IP ban.
                        banlist
                            .apply_ban_record(uuid, ban_record, overwrite, now)
                            .map(|_| Some(frontend_info))
                    },
                    BanOperation::BanIp {
                        reason,
                        info,
                        end_date,
                        ip,
                    } => {
                        let ban = Ban {
                            reason,
                            info: Some(info),
                            end_date,
                        };
                        let frontend_info = ban.info();
                        let action = BanAction::Ban(ban);
                        let ban_record = make_record(action.clone());
                        let ip_ban_record = make_ip_record(action);

                        // If a user is able to connect with a banned IP (e.g. a
                        // moderator), and then `overwrite` is used with the IP
                        // ban operation on them, this will result in changing
                        // the user which is linked to this IP ban (the IP will
                        // no longer be unbanned if the previous user is
                        // unbanned). This should not cause any issues with our
                        // validated invariant of having a UUID linked to at
                        // most 1 active IP ban.
                        let ban_effect = banlist.apply_ban_record(uuid, ban_record, overwrite, now);
                        let ip_ban_effect =
                            banlist.apply_ip_ban_record(ip, ip_ban_record, overwrite, now);
                        // Only submit edit if one of these had an effect.
                        ban_effect.or(ip_ban_effect).map(|_| Some(frontend_info))
                    },
                    BanOperation::Unban { info } => {
                        let action = BanAction::Unban(info);
                        let ban_record = make_record(action.clone());
                        let ban_effect = banlist.apply_ban_record(uuid, ban_record, overwrite, now);
                        // If there is a matching IP ban we should remove that as well.
                        //
                        // Validation checks that there is only one active IP ban for a particular
                        // uuid, since if we ensure IP bans also ban the uuid and regular unbans
                        // remove an existing IP ban if it exists then a user won't be able to
                        // connect from another IP while there is an active IP ban linked to that
                        // user.
                        let ip = banlist
                            .ip_bans
                            .iter()
                            .find(|(_ip, entry)| {
                                entry.current.uuid_when_performed == Some(uuid)
                                    && !entry.current.is_expired(now)
                            })
                            .map(|(ip, _)| *ip);

                        ip.and_then(|ip| {
                            let ip_ban_record = make_ip_record(action);
                            banlist.apply_ip_ban_record(ip, ip_ban_record, overwrite, now)
                        })
                        // Only submit edit if one of these had an effect.
                        .or(ban_effect).map(|_| None)
                    },
                    BanOperation::UnbanIp { info, uuid } => {
                        let ip = banlist
                            .ip_bans
                            .iter()
                            .find(|(_ip, entry)| {
                                entry.current.uuid_when_performed == Some(uuid)
                                    && !entry.current.is_expired(now)
                            })
                            .map(|(ip, _)| *ip);

                        ip.and_then(|ip| {
                            // Note: It is kind of redundant to include uuid here (since it's not
                            // going to change from the ban).
                            banlist.apply_ip_ban_record(
                                ip,
                                make_ip_record(BanAction::Unban(info)),
                                overwrite,
                                now,
                            )
                        })
                        .map(|_| None)
                    },
                }
            });

            match edit_result {
                Some((info, Ok(()))) => Ok(info),
                Some((_, Err(err))) => Err(BanOperationError::EditFailed(err)),
                None => Err(BanOperationError::NoEffect),
            }
        }

        /// Only meant to be called by `Self::ban_operation` within the `edit`
        /// closure.
        ///
        /// Returns `None` to cancel early and abandon the edit.
        #[must_use]
        fn apply_ban_record(
            &mut self,
            uuid: Uuid,
            record: BanRecord,
            overwrite: bool,
            now: DateTime<Utc>,
        ) -> Option<()> {
            match self.uuid_bans.entry(uuid) {
                hash_map::Entry::Vacant(v) => {
                    // If this is an unban, it will have no effect, so return early.
                    if matches!(record.action, BanAction::Unban(_)) {
                        return None;
                    }
                    // Otherwise, this will at least potentially have an effect
                    // (assuming it succeeds).
                    v.insert(BanEntry {
                        current: record,
                        history: Vec::new(),
                        // This is a hint anyway, but expired will also be set to true
                        // before saving by the call `edit`
                        // makes to `validate` (through `try_into`), which will set it
                        // to true in the event that
                        // the ban time was so short
                        // that it expired during the interval
                        // between creating the action and saving it.
                        //
                        // TODO: Decide if we even care enough about this case to worry
                        // about the gap. Probably not, even
                        // though it does involve time!
                        expired: false,
                    });
                },
                hash_map::Entry::Occupied(mut o) => {
                    let entry = o.get_mut();
                    // If overwrite is off, check that this entry (if successful) would
                    // actually change the ban status.
                    if !overwrite && entry.current.is_expired(now) == record.is_expired(now) {
                        return None;
                    }
                    // Push the current (most recent) entry to the back of the history
                    // list.
                    entry.history.push(mem::replace(&mut entry.current, record));
                },
            }
            Some(())
        }

        /// Only meant to be called by `Self::ban_operation` within the `edit`
        /// closure.
        ///
        /// Returns `None` to cancel early and abandon the edit.
        #[must_use]
        fn apply_ip_ban_record(
            &mut self,
            ip: NormalizedIpAddr,
            record: IpBanRecord,
            overwrite: bool,
            now: DateTime<Utc>,
        ) -> Option<()> {
            match self.ip_bans.entry(ip) {
                hash_map::Entry::Vacant(v) => {
                    // If this is an unban, it will have no effect, so return early.
                    if matches!(record.action, BanAction::Unban(_)) {
                        return None;
                    }
                    // Otherwise, this will at least potentially have an effect
                    // (assuming it succeeds).
                    v.insert(IpBanEntry {
                        current: record,
                        history: Vec::new(),
                        // This is a hint anyway, but expired will also be set to true
                        // before saving by the call `edit`
                        // makes to `validate` (through `try_into`), which will set it
                        // to true in the event that
                        // the ban time was so short
                        // that it expired during the interval
                        // between creating the action and saving it.
                        //
                        // TODO: Decide if we even care enough about this case to worry
                        // about the gap. Probably not, even
                        // though it does involve time!
                        expired: false,
                    });
                },
                hash_map::Entry::Occupied(mut o) => {
                    let entry = o.get_mut();
                    // If overwrite is off, check that this entry (if successful) would
                    // actually change the ban status.
                    if !overwrite && entry.current.is_expired(now) == record.is_expired(now) {
                        return None;
                    }
                    // Push the current (most recent) entry to the back of the history
                    // list.
                    entry.history.push(mem::replace(&mut entry.current, record));
                },
            }
            Some(())
        }

        /// One-off migration from the previous version.  This must be
        /// guaranteed to produce a valid settings file as long as it is
        /// called with a valid settings file from the previous version.
        pub(super) fn migrate(prev: prev::Banlist) -> Self {
            let prev::Banlist(uuid_map) = prev;
            // Mostly the same structure but we introduce a new type of ban in a separate
            // map.
            Banlist {
                uuid_bans: uuid_map
                    .into_iter()
                    .map(|(uuid, entry)| (uuid, BanEntry::migrate(entry)))
                    .collect(),
                // Previous version had no concept of IP bans
                ip_bans: HashMap::new(),
            }
        }

        /// Perform any needed validation on this banlist that can't be done
        /// using parsing.
        ///
        /// The returned version being "Old" indicates the loaded setting has
        /// been modified during validation (this is why validate takes
        /// `&mut self`).
        pub(super) fn validate(&mut self) -> Result<Version, <Final as EditableSetting>::Error> {
            let mut version = Version::Latest;
            let now = Utc::now();
            let Self { uuid_bans, ip_bans } = self;
            for (&uuid, value) in uuid_bans.iter_mut() {
                if matches!(value.validate(now, uuid)?, Version::Old) {
                    // Update detected.
                    version = Version::Old;
                }
            }

            let mut uuids = hashbrown::HashSet::new();
            for (&_ip_addr, value) in ip_bans.iter_mut() {
                // Validate that there are not multiple active IP bans
                // linked to the same UUID. (since if timing happens to match
                // the per entry validation won't catch this)
                //
                // collapsible_if: more clear not to have side effects in the if condition
                #[allow(clippy::collapsible_if)]
                if let Some(uuid) = value.current.uuid_when_performed
                    && !value.current.is_expired(now)
                {
                    if !uuids.insert(uuid) {
                        return Err(BanError::Ip {
                            kind: BanErrorKind::ActiveIpBansShareUuid,
                            uuid: Some(uuid),
                            username_from_uuid_entry: uuid_bans
                                .get(&uuid)
                                .map(|e| e.current.username_when_performed.clone()),
                        });
                    }
                }
                if matches!(value.validate(now, uuid_bans)?, Version::Old) {
                    // Update detected.
                    version = Version::Old;
                }
            }
            Ok(version)
        }
    }

    // NOTE: Whenever there is a version upgrade, copy this note as well as the
    // commented-out code below to the next version, then uncomment the code
    // for this version.
    /* impl TryFrom<Banlist> for Final {
        type Error = <Final as EditableSetting>::Error;

        #[allow(clippy::useless_conversion)]
        fn try_from(mut value: Banlist) -> Result<Final, Self::Error> {
            value.validate()?;
            Ok(next::Banlist::migrate(value).try_into().expect(MIGRATION_UPGRADE_GUARANTEE))
        }
    } */
}