strig 0.8.0

Extensible authorization engine: permission catalog, RBAC, ABAC and ReBAC in one decision, with governance and auditing of changes.
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
use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "audit")]
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::attr::Attributes;
#[cfg(feature = "audit")]
use crate::audit::{AuditStatus, Auditor, FailMode};
use crate::condition::Condition;
use crate::decision::Request;
use crate::engine::{Context, Engine};
use crate::explain::Explanation;
use crate::grant::{Effect, Grant, SubjectMatcher};
use crate::rebac::TupleStore;
use crate::resource::{Anchor, Resource};
use crate::subject::{RoleAssignment, Subject};

/// Proposed change to the grant set or to a subject's roles.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[allow(clippy::large_enum_variant)]
pub enum Mutation {
    Add(Grant),
    Remove {
        id: String,
    },
    /// Gives `role` to `subject`. The grant set does not change; whoever stores the subject's roles
    /// writes [`Mutation::subject_after`].
    AssignRole {
        subject: Subject,
        role: RoleAssignment,
    },
    /// Takes `role` from `subject`, comparing role and scope.
    RevokeRole {
        subject: Subject,
        role: RoleAssignment,
    },
}

impl Mutation {
    /// Subject, assignment and `true` to assign (`false` to revoke), if the change is a
    /// role change.
    pub fn role_change(&self) -> Option<(&Subject, &RoleAssignment, bool)> {
        match self {
            Mutation::AssignRole { subject, role } => Some((subject, role, true)),
            Mutation::RevokeRole { subject, role } => Some((subject, role, false)),
            Mutation::Add(_) | Mutation::Remove { .. } => None,
        }
    }

    /// Which of the four changes this is.
    pub fn kind(&self) -> MutationKind {
        match self {
            Mutation::Add(_) => MutationKind::Add,
            Mutation::Remove { .. } => MutationKind::Remove,
            Mutation::AssignRole { .. } => MutationKind::AssignRole,
            Mutation::RevokeRole { .. } => MutationKind::RevokeRole,
        }
    }

    /// The subject of the role change with the role added or removed. `None` for a grant
    /// change.
    pub fn subject_after(&self) -> Option<Subject> {
        let (subject, role, assigned) = self.role_change()?;
        Some(with_role_change(subject, role, assigned))
    }
}

/// The kind of a [`Mutation`], without its payload.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MutationKind {
    Add,
    Remove,
    AssignRole,
    RevokeRole,
}

impl MutationKind {
    pub const ALL: [MutationKind; 4] = [
        MutationKind::Add,
        MutationKind::Remove,
        MutationKind::AssignRole,
        MutationKind::RevokeRole,
    ];

    /// The name the kind has in JSON and in the audit trail.
    pub fn as_str(self) -> &'static str {
        match self {
            MutationKind::Add => "add",
            MutationKind::Remove => "remove",
            MutationKind::AssignRole => "assign_role",
            MutationKind::RevokeRole => "revoke_role",
        }
    }
}

fn with_role_change(subject: &Subject, role: &RoleAssignment, assigned: bool) -> Subject {
    let mut next = subject.clone();
    if !assigned {
        next.roles.retain(|r| r != role);
    } else if !next.roles.contains(role) {
        next.roles.push(role.clone());
    }
    next
}

/// Same subject: subjects are identified by id, ignoring the kind, and only when the id exists.
/// The same id with another kind counts as the same subject, the safe side for self-management and lockout.
pub(crate) fn same_subject(a: &Subject, b: &Subject) -> bool {
    a.id.is_some() && a.id == b.id
}

/// The role assignments of `subject` that a rule guarding a change must count at `now`: the ones that
/// have not ended and have a well-formed window. An assignment that starts later still counts, because
/// the rule protects against what the holder will have, not only what they have at this instant;
/// without an instant nothing is known to have ended. Compare with the engine, where only what is in
/// force decides.
fn standing_roles(subject: &Subject, now: Option<u64>) -> impl Iterator<Item = &RoleAssignment> {
    subject
        .roles
        .iter()
        .filter(move |r| r.check_window().is_ok() && !r.validity().has_ended(now))
}

fn role_label(role: &RoleAssignment) -> String {
    match &role.scope {
        Some(scope) => format!("{} em {scope}", role.role),
        None => role.role.clone(),
    }
}

/// What an invariant sees: who changes, what, and the set before and after. In a role
/// change, `before` and `after` are the same set; the effect shows up in
/// [`Proposal::subject_after`].
pub struct Proposal<'a> {
    pub actor: &'a Subject,
    pub mutation: &'a Mutation,
    pub before: &'a [Grant],
    pub after: &'a [Grant],
    pub tuples: Option<&'a dyn TupleStore>,
    /// The instant the change is judged at, in milliseconds since the Unix epoch; see
    /// [`Governance::at`]. Without it, grants and roles with a validity window fail closed, as in
    /// [`Engine::decide`].
    pub now: Option<u64>,
}

impl<'a> Proposal<'a> {
    /// The grant added, or the one removed. `None` for a role change.
    pub fn grant(&self) -> Option<&'a Grant> {
        match self.mutation {
            Mutation::Add(grant) => Some(grant),
            Mutation::Remove { id } => self.before.iter().find(|g| &g.id == id),
            Mutation::AssignRole { .. } | Mutation::RevokeRole { .. } => None,
        }
    }

    /// `subject` as it would be after the change: with the role applied, if it is the subject of
    /// the role change (same id); unchanged in the other cases.
    pub fn subject_after<'s>(&self, subject: &'s Subject) -> Cow<'s, Subject> {
        match self.mutation.role_change() {
            Some((target, role, assigned)) if same_subject(target, subject) => {
                Cow::Owned(with_role_change(subject, role, assigned))
            }
            _ => Cow::Borrowed(subject),
        }
    }

    pub fn before_context(&self) -> Context<'_> {
        self.context(&self.before)
    }

    pub fn after_context(&self) -> Context<'_> {
        self.context(&self.after)
    }

    fn context<'s>(&'s self, grants: &'s &'a [Grant]) -> Context<'s> {
        let ctx = Context::new(grants);
        let ctx = match self.tuples {
            Some(tuples) => ctx.with_tuples(tuples),
            None => ctx,
        };
        match self.now {
            Some(now) => ctx.at(now),
            None => ctx,
        }
    }
}

/// Compensating change proposed with a [`Violation`]. It names the subject by id and carries only
/// the role: nothing else about the subject is exposed to whoever causes the refusal or reads the
/// audit. Nothing is applied; the caller turns it into an ordinary change with
/// [`Suggestion::into_mutation`] and sends it through the governance like any other.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Suggestion {
    AssignRole { subject_id: String, role: RoleAssignment },
    RevokeRole { subject_id: String, role: RoleAssignment },
}

impl Suggestion {
    /// The suggestion for a role change; `None` for a grant change or a subject without id.
    fn of(mutation: &Mutation) -> Option<Self> {
        let (subject, role, assigned) = mutation.role_change()?;
        let subject_id = subject.id.clone()?;
        let role = role.clone();
        Some(if assigned {
            Suggestion::AssignRole { subject_id, role }
        } else {
            Suggestion::RevokeRole { subject_id, role }
        })
    }

    /// The change to send, for the stored `subject` the suggestion names (same id).
    pub fn into_mutation(self, subject: Subject) -> Mutation {
        match self {
            Suggestion::AssignRole { role, .. } => Mutation::AssignRole { subject, role },
            Suggestion::RevokeRole { role, .. } => Mutation::RevokeRole { subject, role },
        }
    }

    pub fn subject_id(&self) -> &str {
        match self {
            Suggestion::AssignRole { subject_id, .. } | Suggestion::RevokeRole { subject_id, .. } => subject_id,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Violation {
    pub invariant: String,
    pub message: String,
    /// Change that would let the refused one through, set by [`Governance`] only when it was
    /// checked: the invariant's [`Invariant::remedy`] passes every invariant, with the same actor.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub suggestion: Option<Box<Suggestion>>,
}

impl Violation {
    pub fn new(invariant: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            invariant: invariant.into(),
            message: message.into(),
            suggestion: None,
        }
    }
}

impl fmt::Display for Violation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}", self.invariant, self.message)
    }
}

/// Rule that every grant or role change must respect.
pub trait Invariant: Send + Sync {
    fn name(&self) -> &str;
    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation>;

    /// The change that would let a refused proposal through, called only after `check` refused it.
    /// [`Governance`] sends it through every invariant, with the same actor and the same grant set,
    /// and only a change they all accept becomes the [`Violation::suggestion`]. Only role changes
    /// are suggested. The default proposes nothing.
    fn remedy(&self, _engine: &Engine, _proposal: &Proposal<'_>) -> Option<Mutation> {
        None
    }
}

#[cfg(feature = "audit")]
type CallMode = Option<FailMode>;
#[cfg(not(feature = "audit"))]
type CallMode = Option<std::convert::Infallible>;

#[cfg(feature = "audit")]
type Delivery = Option<AuditStatus>;
#[cfg(not(feature = "audit"))]
type Delivery = Option<std::convert::Infallible>;

/// Applies grant and role changes only after they pass every invariant and, with the
/// `audit` feature, after recording the decision.
#[derive(Default)]
pub struct Governance {
    invariants: Vec<Box<dyn Invariant>>,
    #[cfg(feature = "audit")]
    auditor: Option<Arc<Auditor>>,
}

impl Governance {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with(mut self, invariant: impl Invariant + 'static) -> Self {
        self.invariants.push(Box::new(invariant));
        self
    }

    /// Records every change, applied or refused. If the record fails, the change is refused
    /// with the `audit` violation.
    #[cfg(feature = "audit")]
    pub fn audited(mut self, auditor: Arc<Auditor>) -> Self {
        self.auditor = Some(auditor);
        self
    }

    /// The auditor `audited` set, for the records of the approval flow (see [`crate::pending`]).
    #[cfg(feature = "audit")]
    pub(crate) fn auditor(&self) -> Option<&Arc<Auditor>> {
        self.auditor.as_ref()
    }

    pub fn len(&self) -> usize {
        self.invariants.len()
    }

    pub fn is_empty(&self) -> bool {
        self.invariants.is_empty()
    }

    /// Returns the grant set after the change, or all the violations. Persistence
    /// is up to the application, which writes only on `Ok`. For a role change the set comes back unchanged
    /// and the subject to write comes from [`Mutation::subject_after`].
    pub fn apply(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        self.apply_with(engine, actor, current, mutation, tuples, &Attributes::new())
    }

    /// The governance judging every change at `now` (milliseconds since the Unix epoch): the actor's
    /// grants and roles, and the grants and roles the change reads, count only inside their validity
    /// window, as with [`Context::at`]. The crate never reads a clock; the caller passes the instant.
    /// [`Governance::apply`] and [`Governance::apply_with`] pass none, and what has a window then fails
    /// closed.
    pub fn at(&self, now: u64) -> GovernanceAt<'_> {
        GovernanceAt {
            governance: self,
            now,
            call_mode: None,
        }
    }

    /// Like [`Governance::apply`], with request metadata (ip, request id, reason) that goes
    /// to the audit from the `detailed` level upward.
    pub fn apply_with(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
        meta: &Attributes,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        self.run(engine, actor, current, mutation, Scene { tuples, now: None }, meta)
    }

    fn run(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        scene: Scene<'_>,
        meta: &Attributes,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        self.run_reporting(engine, actor, current, mutation, scene, meta, None)
            .0
    }

    /// `run` plus where the audit record ended up (`None` with no auditor). `call` is the
    /// [`FailMode`] for this change.
    #[allow(clippy::too_many_arguments)]
    fn run_reporting(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        scene: Scene<'_>,
        meta: &Attributes,
        call: CallMode,
    ) -> (Result<Vec<Grant>, Vec<Violation>>, Delivery) {
        crate::obs::mutation(
            || {
                let result = self.evaluate_inner(engine, actor, current, mutation, scene, true);
                #[cfg(feature = "audit")]
                if let Some(auditor) = &self.auditor {
                    return match auditor.grant_mutation_with(actor, mutation, current, &result, meta, call) {
                        Ok(emitted) => (result, Some(emitted.status)),
                        Err(error) => (Err(vec![Violation::new("audit", error.to_string())]), None),
                    };
                }
                #[cfg(not(feature = "audit"))]
                let _ = (meta, call);
                (result, None)
            },
            |(result, _)| result.as_ref().err().map(Vec::as_slice),
        )
    }

    /// The mutation checked against the invariants, with no audit and no side effect. Also used by
    /// [`Materialized::apply`](crate::Materialized::apply) to check a whole batch before recording any of it.
    pub(crate) fn evaluate(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        self.evaluate_inner(engine, actor, current, mutation, Scene { tuples, now: None }, true)
    }

    /// [`Governance::evaluate`] at `now`, for the approval flow to judge a change without recording it.
    pub(crate) fn evaluate_at(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
        now: u64,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        let scene = Scene { tuples, now: Some(now) };
        self.evaluate_inner(engine, actor, current, mutation, scene, true)
    }

    fn evaluate_inner(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        scene: Scene<'_>,
        with_remedies: bool,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        let after: Cow<'_, [Grant]> = match mutation {
            Mutation::Add(grant) => {
                if current.iter().any(|g| g.id == grant.id) {
                    return Err(vec![Violation::new(
                        "mutation",
                        format!("grant {} já existe", grant.id),
                    )]);
                }
                let mut next = current.to_vec();
                next.push(grant.clone());
                Cow::Owned(next)
            }
            Mutation::Remove { id } => {
                if !current.iter().any(|g| &g.id == id) {
                    return Err(vec![Violation::new("mutation", format!("grant {id} não existe"))]);
                }
                Cow::Owned(current.iter().filter(|g| &g.id != id).cloned().collect())
            }
            Mutation::AssignRole { subject, role } => {
                check_role_mutation(subject, role, true)?;
                Cow::Borrowed(current)
            }
            Mutation::RevokeRole { subject, role } => {
                check_role_mutation(subject, role, false)?;
                Cow::Borrowed(current)
            }
        };
        let proposal = Proposal {
            actor,
            mutation,
            before: current,
            after: &after,
            tuples: scene.tuples,
            now: scene.now,
        };
        let mut violations = Vec::new();
        for invariant in &self.invariants {
            let Err(mut violation) = invariant.check(engine, &proposal) else {
                continue;
            };
            if with_remedies
                && let Some(fix) = invariant.remedy(engine, &proposal)
                && self.evaluate_inner(engine, actor, current, &fix, scene, false).is_ok()
            {
                violation.suggestion = Suggestion::of(&fix).map(Box::new);
            }
            violations.push(violation);
        }
        if violations.is_empty() {
            Ok(after.into_owned())
        } else {
            Err(violations)
        }
    }
}

/// What a change is judged against besides the grants: the tuples and the instant.
#[derive(Clone, Copy)]
struct Scene<'a> {
    tuples: Option<&'a dyn TupleStore>,
    now: Option<u64>,
}

/// [`Governance`] judging every change at one instant; see [`Governance::at`].
pub struct GovernanceAt<'g> {
    governance: &'g Governance,
    now: u64,
    call_mode: CallMode,
}

impl GovernanceAt<'_> {
    /// The [`FailMode`] of the audit of every change made through this value, over the mode of the
    /// permission and the auditor's default.
    #[cfg(feature = "audit")]
    pub fn fail_mode(mut self, mode: FailMode) -> Self {
        self.call_mode = Some(mode);
        self
    }

    /// Like [`GovernanceAt::apply_with`], and also says where the audit record ended up: `None`
    /// with no auditor or a refused record, `Some(Pending)` when the sink failed in `open` mode and
    /// the change went ahead anyway.
    #[cfg(feature = "audit")]
    pub fn apply_reporting(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
        meta: &Attributes,
    ) -> (Result<Vec<Grant>, Vec<Violation>>, Option<AuditStatus>) {
        let scene = Scene {
            tuples,
            now: Some(self.now),
        };
        self.governance
            .run_reporting(engine, actor, current, mutation, scene, meta, self.call_mode)
    }

    /// Like [`Governance::apply`], at the instant.
    pub fn apply(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        self.apply_with(engine, actor, current, mutation, tuples, &Attributes::new())
    }

    /// Like [`Governance::apply_with`], at the instant.
    pub fn apply_with(
        &self,
        engine: &Engine,
        actor: &Subject,
        current: &[Grant],
        mutation: &Mutation,
        tuples: Option<&dyn TupleStore>,
        meta: &Attributes,
    ) -> Result<Vec<Grant>, Vec<Violation>> {
        let scene = Scene {
            tuples,
            now: Some(self.now),
        };
        self.governance
            .run_reporting(engine, actor, current, mutation, scene, meta, self.call_mode)
            .0
    }
}

fn check_role_mutation(subject: &Subject, role: &RoleAssignment, assigned: bool) -> Result<(), Vec<Violation>> {
    let Some(id) = subject.id.as_deref() else {
        return Err(vec![Violation::new(
            "mutation",
            "papel só se atribui a sujeito identificado",
        )]);
    };
    if assigned && let Err(error) = role.check_window() {
        return Err(vec![Violation::new("mutation", error.to_string())]);
    }
    let has = subject.roles.contains(role);
    let message = match (assigned, has) {
        (true, true) => format!("{id} já tem o papel {}", role_label(role)),
        (false, false) => format!("{id} não tem o papel {}", role_label(role)),
        _ => return Ok(()),
    };
    Err(vec![Violation::new("mutation", message)])
}

/// The added grant exists in the catalog and targets a kind the permission accepts. The assigned
/// role is known (it is in the engine's [`RoleGraph`](crate::RoleGraph) or in some current grant) and
/// its scope, if any, uses a kind from the catalog. Revoking always passes: removing an
/// unknown role is cleanup.
pub struct CatalogConformance;

impl CatalogConformance {
    fn check_role(&self, engine: &Engine, current: &[Grant], role: &RoleAssignment) -> Result<(), Violation> {
        let known = engine.roles().contains(&role.role)
            || current
                .iter()
                .any(|g| matches!(&g.subject, SubjectMatcher::Role { id } if *id == role.role));
        if !known {
            return Err(Violation::new(
                self.name(),
                format!("papel desconhecido: {}", role.role),
            ));
        }
        match &role.scope {
            Some(scope) if !engine.catalog().is_target_kind(&scope.kind) => Err(Violation::new(
                self.name(),
                format!("escopo {scope} usa tipo fora do catálogo"),
            )),
            _ => Ok(()),
        }
    }
}

impl Invariant for CatalogConformance {
    fn name(&self) -> &str {
        "catalog_conformance"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        match proposal.mutation {
            Mutation::Add(grant) => engine
                .validate_grant(grant)
                .map_err(|e| Violation::new(self.name(), e.to_string())),
            Mutation::AssignRole { role, .. } => self.check_role(engine, proposal.before, role),
            Mutation::Remove { .. } | Mutation::RevokeRole { .. } => Ok(()),
        }
    }
}

/// Nobody changes a grant that applies to themselves: neither the user directly, nor a role they
/// hold (direct or inherited). Grants by principal and by relation are left out. Nobody assigns
/// or revokes a role of their own, either.
pub struct NoSelfManagement;

impl Invariant for NoSelfManagement {
    fn name(&self) -> &str {
        "no_self_management"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        let actor = proposal.actor;
        if let Some((subject, _, _)) = proposal.mutation.role_change() {
            return if same_subject(subject, actor) {
                Err(Violation::new(
                    self.name(),
                    "não é permitido gerenciar os próprios papéis",
                ))
            } else {
                Ok(())
            };
        }
        let Some(grant) = proposal.grant() else {
            return Ok(());
        };
        let affects_actor = match &grant.subject {
            SubjectMatcher::User { id } => actor.id.as_deref() == Some(id.as_str()),
            SubjectMatcher::Role { id } => engine
                .roles()
                .expand(standing_roles(actor, proposal.now).map(|r| r.role.as_str()))
                .contains(id),
            _ => false,
        };
        if affects_actor {
            Err(Violation::new(
                self.name(),
                "não é permitido gerenciar as próprias permissões",
            ))
        } else {
            Ok(())
        }
    }
}

/// The actor must hold `key` on `resource` before the change.
pub struct RequirePermission {
    key: String,
    resource: Resource,
}

impl RequirePermission {
    pub fn new(key: impl Into<String>, resource: Resource) -> Self {
        Self {
            key: key.into(),
            resource,
        }
    }
}

impl Invariant for RequirePermission {
    fn name(&self) -> &str {
        "require_permission"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        let request = Request::new(proposal.actor, &self.key, &self.resource);
        if engine.check(&proposal.before_context(), &request) {
            Ok(())
        } else {
            Err(Violation::new(self.name(), format!("falta a permissão {}", self.key)))
        }
    }
}

/// After the change, each critical permission still has at least one member able to
/// exercise it on `resource`. Prevents locking a scope out of its own management. Each member
/// is evaluated as they would be after the change ([`Proposal::subject_after`]), so revoking the
/// role of the last one who exercises the permission is also refused.
///
/// With [`NoLockout::with_reserve`], the refusal may carry a [`Suggestion`]: assigning the
/// administrator role to a reserve subject. It is only offered when it really fixes the lockout and
/// the governance accepts it (see [`Invariant::remedy`]); otherwise the violation has no suggestion.
pub struct NoLockout {
    critical: Vec<String>,
    members: Vec<Subject>,
    resource: Resource,
    reserve: Option<Reserve>,
}

struct Reserve {
    admin_role: RoleAssignment,
    subjects: Vec<Subject>,
}

impl NoLockout {
    pub fn new<I, S>(critical: I, members: Vec<Subject>, resource: Resource) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            critical: critical.into_iter().map(Into::into).collect(),
            members,
            resource,
            reserve: None,
        }
    }

    /// Subjects held in reserve and the administrator role that the suggestion assigns to one of
    /// them. A reserve subject is only suggested when it is one of the `members` (same id), has an
    /// id, does not have the role yet, is not the actor of the refused change, and the role makes
    /// every critical permission covered again. Only its id and the role leave the invariant.
    #[must_use]
    pub fn with_reserve<I>(mut self, admin_role: RoleAssignment, subjects: I) -> Self
    where
        I: IntoIterator<Item = Subject>,
    {
        self.reserve = Some(Reserve {
            admin_role,
            subjects: subjects.into_iter().collect(),
        });
        self
    }

    /// Critical permissions with no member able to exercise them after the change. With `promoted`,
    /// that subject stands in for the member with the same id.
    fn uncovered(&self, engine: &Engine, proposal: &Proposal<'_>, promoted: Option<&Subject>) -> Vec<&String> {
        let ctx = proposal.after_context();
        self.critical
            .iter()
            .filter(|key| {
                !self.members.iter().any(|m| {
                    let member = match promoted {
                        Some(p) if same_subject(p, m) => Cow::Borrowed(p),
                        _ => proposal.subject_after(m),
                    };
                    engine.check(&ctx, &Request::new(&member, key, &self.resource))
                })
            })
            .collect()
    }
}

impl Invariant for NoLockout {
    fn name(&self) -> &str {
        "no_lockout"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        match self.uncovered(engine, proposal, None).first() {
            Some(first) => Err(Violation::new(
                self.name(),
                format!("a mudança deixaria ninguém com a permissão {first}"),
            )),
            None => Ok(()),
        }
    }

    fn remedy(&self, engine: &Engine, proposal: &Proposal<'_>) -> Option<Mutation> {
        let reserve = self.reserve.as_ref()?;
        reserve.subjects.iter().find_map(|candidate| {
            candidate.id.as_ref()?;
            if same_subject(candidate, proposal.actor) {
                return None;
            }
            let member = self.members.iter().find(|m| same_subject(m, candidate))?;
            let base = proposal.subject_after(member);
            if member.roles.contains(&reserve.admin_role) || base.roles.contains(&reserve.admin_role) {
                return None;
            }
            let promoted = with_role_change(&base, &reserve.admin_role, true);
            self.uncovered(engine, proposal, Some(&promoted))
                .is_empty()
                .then(|| Mutation::AssignRole {
                    subject: member.clone(),
                    role: reserve.admin_role.clone(),
                })
        })
    }
}

/// `subject` never receives `allow` on `permissions` or on any permission that grants them.
///
/// On a role assignment, it only applies when `subject` is [`SubjectMatcher::User`] with the
/// subject's id: it refuses the role if any current `allow` grant for them, or for a role they inherit,
/// grants a forbidden permission, regardless of scope. The other matchers (principal, `any`,
/// role, relation) do not apply to role assignment.
pub struct Forbid {
    subject: SubjectMatcher,
    permissions: Vec<String>,
}

impl Forbid {
    pub fn new<I, S>(subject: SubjectMatcher, permissions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            subject,
            permissions: permissions.into_iter().map(Into::into).collect(),
        }
    }
}

impl Invariant for Forbid {
    fn name(&self) -> &str {
        "forbid"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        match proposal.mutation {
            Mutation::Add(grant) => self.check_grant(engine, grant),
            Mutation::AssignRole { subject, role } => self.check_role(engine, proposal, subject, role),
            Mutation::Remove { .. } | Mutation::RevokeRole { .. } => Ok(()),
        }
    }
}

impl Forbid {
    fn forbidden_by<'s>(&'s self, engine: &Engine, permission: &str) -> Option<&'s String> {
        self.permissions
            .iter()
            .find(|p| engine.catalog().implies(permission, p))
    }

    fn check_grant(&self, engine: &Engine, grant: &Grant) -> Result<(), Violation> {
        if grant.effect != Effect::Allow || grant.subject != self.subject {
            return Ok(());
        }
        match self.forbidden_by(engine, &grant.permission) {
            Some(forbidden) => Err(Violation::new(
                self.name(),
                format!(
                    "{} concederia {forbidden}, proibido para este sujeito",
                    grant.permission
                ),
            )),
            None => Ok(()),
        }
    }

    fn check_role(
        &self,
        engine: &Engine,
        proposal: &Proposal<'_>,
        subject: &Subject,
        role: &RoleAssignment,
    ) -> Result<(), Violation> {
        let SubjectMatcher::User { id } = &self.subject else {
            return Ok(());
        };
        if subject.id.as_deref() != Some(id.as_str()) {
            return Ok(());
        }
        let roles = engine.roles().expand([role.role.as_str()]);
        for grant in proposal
            .before
            .iter()
            .filter(|g| g.effect == Effect::Allow && !g.validity().has_ended(proposal.now))
        {
            let SubjectMatcher::Role { id: granted } = &grant.subject else {
                continue;
            };
            if !roles.contains(granted) {
                continue;
            }
            if let Some(forbidden) = self.forbidden_by(engine, &grant.permission) {
                return Err(Violation::new(
                    self.name(),
                    format!(
                        "o papel {} concederia {forbidden} pelo grant {} ({}), proibido para este sujeito",
                        role.role, grant.id, grant.permission
                    ),
                ));
            }
        }
        Ok(())
    }
}

/// Resource on which `grant` takes effect, as far as governance can tell without a concrete
/// resource: the target step (a kind-only target becomes `kind:*`), the grant's scope and the scope
/// of the role assignment that carries it. The implicit `global` is not repeated.
fn grant_resource(grant: &Grant, role_scope: Option<&Anchor>) -> Resource {
    let mut path: Vec<Anchor> = Vec::new();
    let target = Anchor {
        kind: grant.target.kind.clone(),
        id: grant.target.id.clone().or_else(|| Some(KIND_WILDCARD.to_string())),
    };
    for anchor in [Some(target), grant.scope.clone(), role_scope.cloned()]
        .into_iter()
        .flatten()
    {
        if !anchor.is_global() && !path.contains(&anchor) {
            path.push(anchor);
        }
    }
    Resource {
        path,
        ..Resource::default()
    }
}

/// Id of the synthetic step that stands for "any instance" of a kind-only target.
const KIND_WILDCARD: &str = "*";

/// Id of the probe subject built from a grant's matcher.
const PROBE_ID: &str = "strig:probe";

/// A subject that the grant's matcher selects. `None` for a relation, which depends on tuples.
fn matcher_subject(matcher: &SubjectMatcher) -> Option<Subject> {
    Some(match matcher {
        SubjectMatcher::User { id } => Subject::user(id.clone()),
        SubjectMatcher::Role { id } => Subject::user(PROBE_ID).with_role(id.clone()),
        SubjectMatcher::Principal { name } => Subject::user(PROBE_ID).with_principal(name.clone()),
        SubjectMatcher::Any => Subject::user(PROBE_ID),
        SubjectMatcher::Relation { .. } => return None,
    })
}

/// What a change hands out: one allow grant, on one resource, with every permission it confers.
struct Conferred<'a> {
    grant: &'a Grant,
    resource: Resource,
    /// The grant's permission and every permission it grants by implication.
    keys: Vec<String>,
}

fn conferred_keys(engine: &Engine, permission: &str) -> Vec<String> {
    let mut keys = vec![permission.to_string()];
    keys.extend(engine.catalog().descendants(permission));
    keys
}

/// The allow grants that the change gives to someone: the added grant, or, in a role assignment,
/// the allow grants of the role (and of the roles it inherits) in the scope of the assignment.
fn conferred<'a>(engine: &Engine, proposal: &Proposal<'a>) -> Vec<Conferred<'a>> {
    let ended = |grant: &Grant| grant.validity().has_ended(proposal.now);
    match proposal.mutation {
        Mutation::Add(grant) if grant.effect == Effect::Allow && !ended(grant) => vec![Conferred {
            grant,
            resource: grant_resource(grant, None),
            keys: conferred_keys(engine, &grant.permission),
        }],
        Mutation::AssignRole { role, .. } if !role.validity().has_ended(proposal.now) => {
            let roles = engine.roles().expand([role.role.as_str()]);
            proposal
                .before
                .iter()
                .filter(|g| g.effect == Effect::Allow && !ended(g))
                .filter(|g| matches!(&g.subject, SubjectMatcher::Role { id } if roles.contains(id)))
                .map(|grant| Conferred {
                    grant,
                    resource: grant_resource(grant, role.scope.as_ref()),
                    keys: conferred_keys(engine, &grant.permission),
                })
                .collect()
        }
        _ => Vec::new(),
    }
}

/// The `deny` grants that the change lifts: the removed one, or, in a role revocation, the `deny`
/// grants of the role (and of the roles it inherits) in the scope of the assignment. What each one was
/// blocking, the permission and the ones it implies, is now open to its subject.
fn unlocked<'a>(engine: &Engine, proposal: &Proposal<'a>) -> Vec<Conferred<'a>> {
    let ended = |grant: &Grant| grant.validity().has_ended(proposal.now);
    match proposal.mutation {
        Mutation::Remove { .. } => proposal
            .grant()
            .filter(|g| g.effect == Effect::Deny && !ended(g))
            .map(|grant| Conferred {
                grant,
                resource: grant_resource(grant, None),
                keys: conferred_keys(engine, &grant.permission),
            })
            .into_iter()
            .collect(),
        Mutation::RevokeRole { role, .. } if !role.validity().has_ended(proposal.now) => {
            let roles = engine.roles().expand([role.role.as_str()]);
            proposal
                .before
                .iter()
                .filter(|g| g.effect == Effect::Deny && !ended(g))
                .filter(|g| matches!(&g.subject, SubjectMatcher::Role { id } if roles.contains(id)))
                .map(|grant| Conferred {
                    grant,
                    resource: grant_resource(grant, role.scope.as_ref()),
                    keys: conferred_keys(engine, &grant.permission),
                })
                .collect()
        }
        _ => Vec::new(),
    }
}

/// Holding a permission requires holding its `depends_on` prerequisites on the same resource.
/// The change is refused when it would leave someone with a permission and without one of its
/// prerequisites: adding the permission alone, adding a `deny` on the prerequisite, removing the
/// prerequisite, or assigning or revoking a role that carries them.
///
/// The check is conservative and approximate, not a second decision engine:
///
/// - A grant is probed with a synthetic subject that only has what the grant's matcher selects (a
///   user id, a role, a principal, or nothing for `any`). A role that confers `edit` without `view` is
///   refused even if everyone who holds it also holds another role with `view`; to model that, give the
///   prerequisite to the role itself. A relation matcher depends on tuples and is not probed.
/// - A grant with a condition is probed as it evaluates for the synthetic subject. If the condition is
///   false for it, the grant is not seen. The engine still denies the permission at decision time when the
///   prerequisite is missing, so this never lets anything through.
/// - On a role change, the grants of the subject come from its user id, its roles (each one, with its
///   own scope), its principals and `any`; relation grants are not considered.
/// - The cost grows with grants × permissions they confer × decisions: removing a grant or adding a
///   `deny` walks every `allow` of the set, which is quadratic in large sets.
pub struct Prerequisites;

impl Prerequisites {
    /// The first prerequisite of `key` that `subject` lacks on `resource`, if the subject holds
    /// `key` itself.
    fn missing(
        engine: &Engine,
        ctx: &Context<'_>,
        subject: &Subject,
        key: &str,
        resource: &Resource,
    ) -> Option<String> {
        if !engine.catalog().has_prerequisites(key)
            || !engine
                .decide_ignoring_prerequisites(ctx, &Request::new(subject, key, resource))
                .is_allowed()
        {
            return None;
        }
        engine
            .catalog()
            .prerequisites(key)
            .find(|p| !engine.check(ctx, &Request::new(subject, p, resource)))
            .map(str::to_string)
    }

    fn violation(&self, key: &str, prerequisite: &str) -> Violation {
        Violation::new(self.name(), format!("{key} exige {prerequisite} no mesmo recurso"))
    }

    /// Allow grants that `subject` (after the change) picks up through its user id, each of its roles
    /// (with the scope of the assignment that carries it), its principals, or `any`.
    fn grants_of<'g>(engine: &Engine, subject: &Subject, grants: &'g [Grant]) -> Vec<(&'g Grant, Option<Anchor>)> {
        let mut found = Vec::new();
        for grant in grants.iter().filter(|g| g.effect == Effect::Allow) {
            match &grant.subject {
                SubjectMatcher::User { id } if subject.id.as_deref() == Some(id.as_str()) => {
                    found.push((grant, None));
                }
                SubjectMatcher::Role { id } => {
                    for assignment in &subject.roles {
                        if engine.roles().expand([assignment.role.as_str()]).contains(id) {
                            found.push((grant, assignment.scope.clone()));
                        }
                    }
                }
                SubjectMatcher::Principal { name } if subject.principals.contains(name) => {
                    found.push((grant, None));
                }
                SubjectMatcher::Any => found.push((grant, None)),
                _ => {}
            }
        }
        found
    }

    /// The first `allow` of `after` that stands on a prerequisite the `changed` permission covers and
    /// that is no longer fully held: the grant's holder has it without the prerequisite.
    fn broken_by(
        &self,
        engine: &Engine,
        proposal: &Proposal<'_>,
        ctx: &Context<'_>,
        changed: &str,
    ) -> Result<(), Violation> {
        for grant in proposal.after.iter().filter(|g| g.effect == Effect::Allow) {
            let Some(subject) = matcher_subject(&grant.subject) else {
                continue;
            };
            let resource = grant_resource(grant, None);
            for key in conferred_keys(engine, &grant.permission) {
                let relies = engine
                    .catalog()
                    .prerequisites(&key)
                    .any(|p| engine.catalog().implies(changed, p));
                if !relies {
                    continue;
                }
                if let Some(p) = Self::missing(engine, ctx, &subject, &key, &resource) {
                    return Err(self.violation(&key, &p));
                }
            }
        }
        Ok(())
    }
}

impl Invariant for Prerequisites {
    fn name(&self) -> &str {
        "prerequisites"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        let ctx = proposal.after_context();
        match proposal.mutation {
            Mutation::Add(grant) if grant.effect == Effect::Deny => {
                if grant.validity().has_ended(proposal.now) {
                    return Ok(());
                }
                self.broken_by(engine, proposal, &ctx, &grant.permission)
            }
            Mutation::Add(grant) => {
                let Some(subject) = matcher_subject(&grant.subject) else {
                    return Ok(());
                };
                for c in conferred(engine, proposal) {
                    for key in &c.keys {
                        if let Some(p) = Self::missing(engine, &ctx, &subject, key, &c.resource) {
                            return Err(self.violation(key, &p));
                        }
                    }
                }
                Ok(())
            }
            Mutation::Remove { .. } => match proposal.grant().filter(|g| !g.validity().has_ended(proposal.now)) {
                Some(removed) => self.broken_by(engine, proposal, &ctx, &removed.permission),
                None => Ok(()),
            },
            Mutation::AssignRole { role, .. } | Mutation::RevokeRole { role, .. }
                if role.validity().has_ended(proposal.now) =>
            {
                Ok(())
            }
            Mutation::AssignRole { subject, .. } | Mutation::RevokeRole { subject, .. } => {
                let after = proposal.subject_after(subject);
                for (grant, scope) in Self::grants_of(engine, &after, proposal.after) {
                    let resource = grant_resource(grant, scope.as_ref());
                    for key in conferred_keys(engine, &grant.permission) {
                        if let Some(p) = Self::missing(engine, &ctx, &after, &key, &resource) {
                            return Err(self.violation(&key, &p));
                        }
                    }
                }
                Ok(())
            }
        }
    }
}

/// A permission marked [`PermissionDef::non_delegable`](crate::PermissionDef::non_delegable) is only
/// granted by an actor who holds `manager` on the resource: having the permission is not enough to
/// pass it on. Applies to adding a grant and to assigning a role that carries the permission.
pub struct NonDelegable {
    manager: String,
}

impl NonDelegable {
    pub fn new(manager: impl Into<String>) -> Self {
        Self {
            manager: manager.into(),
        }
    }
}

impl Invariant for NonDelegable {
    fn name(&self) -> &str {
        "non_delegable"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        let ctx = proposal.before_context();
        for c in conferred(engine, proposal) {
            let Some(key) = c
                .keys
                .iter()
                .find(|k| engine.catalog().get(k).is_some_and(|d| d.non_delegable))
            else {
                continue;
            };
            let request = Request::new(proposal.actor, &self.manager, &c.resource);
            if !engine.check(&ctx, &request) {
                return Err(Violation::new(
                    self.name(),
                    format!("{key} não é delegável: só quem tem {} concede", self.manager),
                ));
            }
        }
        Ok(())
    }
}

/// A permission marked [`PermissionDef::management_only`](crate::PermissionDef::management_only) is
/// only granted to the management `roles`: never straight to a user, a principal or an ordinary
/// role. Assigning a role outside `roles` that carries the permission is refused as well.
pub struct ManagementOnly {
    roles: Vec<String>,
}

impl ManagementOnly {
    pub fn new<I, S>(roles: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            roles: roles.into_iter().map(Into::into).collect(),
        }
    }

    fn is_management(&self, role: &str) -> bool {
        self.roles.iter().any(|r| r == role)
    }
}

impl Invariant for ManagementOnly {
    fn name(&self) -> &str {
        "management_only"
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        for c in conferred(engine, proposal) {
            let Some(key) = c
                .keys
                .iter()
                .find(|k| engine.catalog().get(k).is_some_and(|d| d.management_only))
            else {
                continue;
            };
            let allowed = match proposal.mutation {
                Mutation::AssignRole { role, .. } => self.is_management(&role.role),
                _ => matches!(&c.grant.subject, SubjectMatcher::Role { id } if self.is_management(id)),
            };
            if !allowed {
                return Err(Violation::new(
                    self.name(),
                    format!("{key} só pode ser concedida a papéis de gestão"),
                ));
            }
        }
        Ok(())
    }
}

/// Permission that exempts its holder from [`NoEscalation`] on `resource`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Bypass {
    pub permission: String,
    pub resource: Resource,
}

impl Bypass {
    pub fn new(permission: impl Into<String>, resource: Resource) -> Self {
        Self {
            permission: permission.into(),
            resource,
        }
    }
}

/// The actor only hands out what they hold. Three things count as handing out:
///
/// - adding an `allow` grant, or assigning a role: every permission it confers, the permission itself
///   and each one it implies, must be held by the actor on the resource where it would apply (for a
///   role: in the scope of the assignment);
/// - removing a `deny` grant, or revoking a role that carries one: the permissions the `deny` was
///   blocking are unlocked for its subject, so the actor must hold each of them where they become
///   valid. It is the same rule as for an `allow`, applied to what the change lets through;
/// - a condition does not widen: when the actor holds a permission only through grants with a
///   condition, the new grant must carry that condition, the same or inside an `all`. Anything else is
///   refused (fail closed), since the check cannot compare conditions by meaning.
///
/// The actor is evaluated against a synthetic resource built from the grant (target, scope and the
/// scope of the assignment), with no attributes: a condition on resource attributes does not hold
/// there, so it also fails closed. With [`NoEscalation::bypass`], whoever holds the bypass permission
/// is exempt.
#[derive(Default)]
pub struct NoEscalation {
    bypass: Option<Bypass>,
}

impl NoEscalation {
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn bypass(mut self, bypass: Bypass) -> Self {
        self.bypass = Some(bypass);
        self
    }

    fn name_of(&self) -> &'static str {
        "no_escalation"
    }

    /// The actor holds every key of `c` on its resource. With `carried`, the grant that hands the
    /// permission out, it must also be at least as restrictive as the actor's own derivation.
    fn require_held(
        &self,
        engine: &Engine,
        ctx: &Context<'_>,
        actor: &Subject,
        c: &Conferred<'_>,
        carried: Option<&Grant>,
    ) -> Result<(), Violation> {
        for key in &c.keys {
            let explanation = engine.explain(ctx, &Request::new(actor, key, &c.resource));
            if !explanation.decision.is_allowed() {
                return Err(Violation::new(
                    self.name_of(),
                    format!("o ator não tem {key} onde a mudança passaria a valer"),
                ));
            }
            let Some(grant) = carried else { continue };
            let Some(required) = conditions_behind(&explanation) else {
                continue;
            };
            if !required.iter().any(|r| covers(grant.condition.as_ref(), r)) {
                return Err(Violation::new(
                    self.name_of(),
                    format!("o ator só tem {key} sob condição: a concessão precisa carregar a mesma condição"),
                ));
            }
        }
        Ok(())
    }
}

/// Conditions of the `allow` grants the decision stands on. `None` when some of them is
/// unconditional, or no grant decided (an override), so nothing limits the actor.
fn conditions_behind(explanation: &Explanation) -> Option<Vec<&Condition>> {
    let mut conditions = Vec::new();
    for trace in explanation.candidates().filter(|t| t.grant.effect == Effect::Allow) {
        conditions.push(trace.grant.condition.as_ref()?);
    }
    (!conditions.is_empty()).then_some(conditions)
}

/// `candidate` is `required` itself, or an `all` that has it among its terms (at any depth).
fn covers(candidate: Option<&Condition>, required: &Condition) -> bool {
    match candidate {
        None => false,
        Some(c) if c == required => true,
        Some(Condition::All { of }) => of.iter().any(|term| covers(Some(term), required)),
        Some(_) => false,
    }
}

impl Invariant for NoEscalation {
    fn name(&self) -> &str {
        self.name_of()
    }

    fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
        let ctx = proposal.before_context();
        if let Some(bypass) = &self.bypass
            && engine.check(
                &ctx,
                &Request::new(proposal.actor, &bypass.permission, &bypass.resource),
            )
        {
            return Ok(());
        }
        for c in conferred(engine, proposal) {
            self.require_held(engine, &ctx, proposal.actor, &c, Some(c.grant))?;
        }
        for c in unlocked(engine, proposal) {
            self.require_held(engine, &ctx, proposal.actor, &c, None)?;
        }
        Ok(())
    }
}