oxirs-fuseki 0.2.2

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Graph-Level Access Control Lists (ACL)
//!
//! This module implements per-named-graph ACL entries that sit **on top of** the
//! existing dataset-level RBAC model provided by [`crate::auth::rbac`].
//!
//! # Permission model
//!
//! Authorization is a strict AND of two layers:
//!
//! ```text
//! ALLOW  iff  dataset_rbac_allows(principal, action)
//!             AND  graph_acl_allows(graph_iri, principal, graph_permission)
//! ```
//!
//! The graph ACL layer is additive: if **no** ACL entry exists for a graph the
//! default decision is `Deny`, preserving the principle of least-privilege.
//! A principal whose dataset-level role includes `GlobalAdmin` or who holds
//! the graph-level `Admin` permission bypasses all graph-specific checks.
//!
//! # Example
//!
//! ```rust,ignore
//! use oxirs_fuseki::auth::graph_acl::{GraphAclStore, GraphPermission};
//!
//! let store = GraphAclStore::new();
//! store.grant("http://example.org/g1", "alice", GraphPermission::Read).await?;
//!
//! let can_read = store
//!     .check("http://example.org/g1", "alice", GraphPermission::Read)
//!     .await;
//! assert!(can_read);
//! ```

use crate::auth::types::{Permission, User};
use crate::error::{FusekiError, FusekiResult};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info};

// ──────────────────────────────────────────────────────────────────────────────
// Core permission enum
// ──────────────────────────────────────────────────────────────────────────────

/// Fine-grained permission that can be attached to a named-graph ACL entry.
///
/// Permissions are **additive**: `Admin` implies both `Write` and `Read`;
/// `Write` implies `Read`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum GraphPermission {
    /// Allows SPARQL SELECT / CONSTRUCT / DESCRIBE / ASK against the graph.
    Read,
    /// Allows INSERT DATA / DELETE DATA / LOAD / CLEAR for the graph.
    /// Implies `Read`.
    Write,
    /// Full control: can grant/revoke ACL entries and perform destructive ops.
    /// Implies `Write` and `Read`.
    Admin,
}

impl GraphPermission {
    /// Returns `true` when `self` logically implies `other`.
    ///
    /// ```text
    /// Admin  → Write → Read
    /// ```
    pub fn implies(self, other: GraphPermission) -> bool {
        match self {
            GraphPermission::Admin => true,
            GraphPermission::Write => {
                matches!(other, GraphPermission::Read | GraphPermission::Write)
            }
            GraphPermission::Read => matches!(other, GraphPermission::Read),
        }
    }

    /// Returns a human-readable label.
    pub fn as_str(self) -> &'static str {
        match self {
            GraphPermission::Read => "read",
            GraphPermission::Write => "write",
            GraphPermission::Admin => "admin",
        }
    }
}

impl std::fmt::Display for GraphPermission {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Principal
// ──────────────────────────────────────────────────────────────────────────────

/// A security principal that can appear in a graph ACL entry.
///
/// Two principal kinds are supported:
/// - `User(username)` — a specific authenticated user
/// - `Role(role_name)` — any user who holds the named RBAC role
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Principal {
    User { name: String },
    Role { name: String },
}

impl Principal {
    /// Construct a user principal.
    pub fn user(name: impl Into<String>) -> Self {
        Principal::User { name: name.into() }
    }

    /// Construct a role principal.
    pub fn role(name: impl Into<String>) -> Self {
        Principal::Role { name: name.into() }
    }

    /// Returns `true` when this principal applies to `user`.
    pub fn matches_user(&self, user: &User) -> bool {
        match self {
            Principal::User { name } => &user.username == name,
            Principal::Role { name } => user.roles.contains(name),
        }
    }
}

impl std::fmt::Display for Principal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Principal::User { name } => write!(f, "user:{name}"),
            Principal::Role { name } => write!(f, "role:{name}"),
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// ACL entry (the persistent record)
// ──────────────────────────────────────────────────────────────────────────────

/// A single ACL entry that binds a principal to a permission on a named graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAclEntry {
    /// The named graph IRI (e.g. `"http://example.org/graph/private"`).
    pub graph_iri: String,
    /// Who holds this permission.
    pub principal: Principal,
    /// The permission that is granted.
    pub permission: GraphPermission,
    /// When this entry was created.
    pub granted_at: DateTime<Utc>,
    /// Optional: who created this entry (for audit trails).
    pub granted_by: Option<String>,
}

impl GraphAclEntry {
    fn new(
        graph_iri: impl Into<String>,
        principal: Principal,
        permission: GraphPermission,
        granted_by: Option<String>,
    ) -> Self {
        Self {
            graph_iri: graph_iri.into(),
            principal,
            permission,
            granted_at: Utc::now(),
            granted_by,
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Management request / response types
// ──────────────────────────────────────────────────────────────────────────────

/// Request to grant a principal access to a named graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrantGraphAccess {
    pub graph_iri: String,
    pub principal: Principal,
    pub permission: GraphPermission,
    /// Username of the person performing the grant (for auditing).
    pub granted_by: Option<String>,
}

/// Request to revoke a principal's access to a named graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevokeGraphAccess {
    pub graph_iri: String,
    pub principal: Principal,
    /// If `None`, **all** permissions for this principal on the graph are revoked.
    pub permission: Option<GraphPermission>,
}

/// Summary of the access-check result returned by [`GraphAclPolicy`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AclDecision {
    /// Access is allowed. Contains the reason for diagnostic purposes.
    Allow { reason: AllowReason },
    /// Access is denied.
    Deny { reason: DenyReason },
}

impl AclDecision {
    /// Returns `true` iff this decision is `Allow`.
    pub fn is_allow(&self) -> bool {
        matches!(self, AclDecision::Allow { .. })
    }
}

/// Why access was allowed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AllowReason {
    /// Principal holds a dataset-level admin permission (bypasses graph ACL).
    DatasetAdmin,
    /// Principal has an explicit graph-level ACL entry (or implied by higher entry).
    ExplicitAcl,
}

/// Why access was denied.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DenyReason {
    /// Dataset-level RBAC already denied the request — graph ACL was not consulted.
    DatasetRbacDenied,
    /// Dataset-level RBAC passed but there is no matching graph ACL entry.
    NoAclEntry,
}

// ──────────────────────────────────────────────────────────────────────────────
// GraphAcl — the per-graph permission set
// ──────────────────────────────────────────────────────────────────────────────

/// In-memory representation of all ACL entries for a single named graph.
///
/// Stored as `HashMap<Principal, HashSet<GraphPermission>>` for O(1) lookups.
#[derive(Debug, Default, Clone)]
struct GraphAcl {
    entries: HashMap<Principal, HashSet<GraphPermission>>,
}

impl GraphAcl {
    fn grant(&mut self, principal: Principal, permission: GraphPermission) {
        self.entries
            .entry(principal)
            .or_insert_with(HashSet::new)
            .insert(permission);
    }

    /// Revoke a specific permission (or all permissions if `permission` is `None`).
    fn revoke(&mut self, principal: &Principal, permission: Option<GraphPermission>) -> bool {
        match self.entries.get_mut(principal) {
            None => false,
            Some(perms) => {
                if let Some(p) = permission {
                    let removed = perms.remove(&p);
                    if perms.is_empty() {
                        self.entries.remove(principal);
                    }
                    removed
                } else {
                    self.entries.remove(principal).is_some()
                }
            }
        }
    }

    /// Check whether `principal` has (or is implied to have) `permission`.
    ///
    /// A principal satisfies the check when they hold **any** permission that
    /// `implies` the requested one.
    fn check(&self, principal: &Principal, permission: GraphPermission) -> bool {
        match self.entries.get(principal) {
            None => false,
            Some(perms) => perms.iter().any(|p| p.implies(permission)),
        }
    }

    /// Check whether `user` (matched against user and role principals) has `permission`.
    fn check_user(&self, user: &User, permission: GraphPermission) -> bool {
        self.entries.iter().any(|(principal, perms)| {
            principal.matches_user(user) && perms.iter().any(|p| p.implies(permission))
        })
    }

    /// Enumerate all `(Principal, GraphPermission)` pairs in this ACL.
    fn list(&self) -> Vec<(Principal, GraphPermission)> {
        let mut out = Vec::new();
        for (principal, perms) in &self.entries {
            for perm in perms {
                out.push((principal.clone(), *perm));
            }
        }
        out.sort_by(|a, b| a.1.cmp(&b.1).then(a.0.to_string().cmp(&b.0.to_string())));
        out
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// GraphAclStore — the top-level CRUD service
// ──────────────────────────────────────────────────────────────────────────────

/// Thread-safe in-memory store for graph-level ACL entries.
///
/// This is the primary API surface used by the server to manage graph ACLs.
/// All mutating operations are async-safe behind `tokio::sync::RwLock`.
///
/// # Clonable handle
///
/// `GraphAclStore` is `Clone` and wraps its state in an `Arc`, so callers can
/// cheaply share it across Axum handler closures.
#[derive(Debug, Clone)]
pub struct GraphAclStore {
    inner: Arc<RwLock<HashMap<String, GraphAcl>>>,
}

impl GraphAclStore {
    /// Create an empty store.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    // ── Mutations ─────────────────────────────────────────────────────────────

    /// Grant `principal` the given `permission` on `graph_iri`.
    ///
    /// Idempotent: granting an already-held permission is a no-op.
    pub async fn grant(&self, req: &GrantGraphAccess) -> FusekiResult<GraphAclEntry> {
        let mut store = self.inner.write().await;
        let acl = store
            .entry(req.graph_iri.clone())
            .or_insert_with(GraphAcl::default);

        acl.grant(req.principal.clone(), req.permission);

        let entry = GraphAclEntry::new(
            &req.graph_iri,
            req.principal.clone(),
            req.permission,
            req.granted_by.clone(),
        );

        info!(
            "GraphACL grant: {} {} on <{}>",
            req.principal, req.permission, req.graph_iri
        );

        Ok(entry)
    }

    /// Revoke access from `principal` on `graph_iri`.
    ///
    /// Returns `Ok(true)` if an entry was removed, `Ok(false)` if nothing
    /// matched (idempotent — not an error).
    pub async fn revoke(&self, req: &RevokeGraphAccess) -> FusekiResult<bool> {
        let mut store = self.inner.write().await;

        match store.get_mut(&req.graph_iri) {
            None => {
                debug!("GraphACL revoke: no ACL for <{}> (noop)", req.graph_iri);
                Ok(false)
            }
            Some(acl) => {
                let removed = acl.revoke(&req.principal, req.permission);
                if removed {
                    info!(
                        "GraphACL revoke: {} {:?} on <{}>",
                        req.principal, req.permission, req.graph_iri
                    );
                    // Clean up empty ACL maps
                    if acl.entries.is_empty() {
                        store.remove(&req.graph_iri);
                    }
                }
                Ok(removed)
            }
        }
    }

    // ── Queries ───────────────────────────────────────────────────────────────

    /// Check whether a `Principal` directly holds (or is implied to hold)
    /// `permission` on `graph_iri`.
    ///
    /// This is the low-level check that does **not** consider dataset-level RBAC.
    /// Use [`GraphAclPolicy::check`] for the combined check.
    pub async fn check(
        &self,
        graph_iri: &str,
        principal: &Principal,
        permission: GraphPermission,
    ) -> bool {
        let store = self.inner.read().await;
        match store.get(graph_iri) {
            None => false,
            Some(acl) => acl.check(principal, permission),
        }
    }

    /// Check whether `user` (matching on username and roles) has `permission`
    /// on `graph_iri`.
    pub async fn check_user(
        &self,
        graph_iri: &str,
        user: &User,
        permission: GraphPermission,
    ) -> bool {
        let store = self.inner.read().await;
        match store.get(graph_iri) {
            None => false,
            Some(acl) => acl.check_user(user, permission),
        }
    }

    /// List all ACL entries for a named graph.
    pub async fn list_graph(&self, graph_iri: &str) -> Vec<GraphAclEntry> {
        let store = self.inner.read().await;
        match store.get(graph_iri) {
            None => vec![],
            Some(acl) => acl
                .list()
                .into_iter()
                .map(|(principal, permission)| {
                    GraphAclEntry::new(graph_iri, principal, permission, None)
                })
                .collect(),
        }
    }

    /// List all ACL entries across all graphs.
    pub async fn list_all(&self) -> Vec<GraphAclEntry> {
        let store = self.inner.read().await;
        let mut out = Vec::new();
        for (graph_iri, acl) in store.iter() {
            for (principal, permission) in acl.list() {
                out.push(GraphAclEntry::new(graph_iri, principal, permission, None));
            }
        }
        out.sort_by(|a, b| {
            a.graph_iri
                .cmp(&b.graph_iri)
                .then(a.permission.cmp(&b.permission))
        });
        out
    }

    /// List all graphs for which `principal` has any explicit ACL entry.
    pub async fn graphs_for_principal(
        &self,
        principal: &Principal,
    ) -> Vec<(String, GraphPermission)> {
        let store = self.inner.read().await;
        let mut out = Vec::new();
        for (graph_iri, acl) in store.iter() {
            if let Some(perms) = acl.entries.get(principal) {
                for perm in perms {
                    out.push((graph_iri.clone(), *perm));
                }
            }
        }
        out.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)));
        out
    }

    /// Remove **all** ACL entries for a named graph (e.g., when the graph is
    /// deleted from the dataset).
    pub async fn purge_graph(&self, graph_iri: &str) -> FusekiResult<usize> {
        let mut store = self.inner.write().await;
        match store.remove(graph_iri) {
            None => Ok(0),
            Some(acl) => {
                let count = acl.entries.values().map(|s| s.len()).sum();
                info!(
                    "GraphACL purge: removed {} entries for <{}>",
                    count, graph_iri
                );
                Ok(count)
            }
        }
    }

    /// Return the total number of ACL entries currently stored.
    pub async fn entry_count(&self) -> usize {
        let store = self.inner.read().await;
        store
            .values()
            .map(|acl| acl.entries.values().map(|s| s.len()).sum::<usize>())
            .sum()
    }
}

impl Default for GraphAclStore {
    fn default() -> Self {
        Self::new()
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// GraphAclPolicy — AND-combines dataset RBAC + graph ACL
// ──────────────────────────────────────────────────────────────────────────────

/// Policy evaluator that AND-combines the dataset-level RBAC decision with the
/// graph-level ACL decision.
///
/// The evaluation algorithm:
///
/// 1. If the user holds `Permission::GlobalAdmin` or `Permission::DatasetAdmin(dataset_id)`,
///    return `Allow { reason: DatasetAdmin }` immediately — admins bypass graph ACLs.
/// 2. Check dataset-level RBAC via the supplied `dataset_rbac_check` callback.
///    If denied → `Deny { reason: DatasetRbacDenied }`.
/// 3. Look up the graph ACL in the store.
///    If no matching entry exists → `Deny { reason: NoAclEntry }`.
/// 4. Otherwise → `Allow { reason: ExplicitAcl }`.
#[derive(Clone)]
pub struct GraphAclPolicy {
    store: GraphAclStore,
}

impl GraphAclPolicy {
    /// Create a policy backed by the given ACL store.
    pub fn new(store: GraphAclStore) -> Self {
        Self { store }
    }

    /// Perform the combined access-control check.
    ///
    /// # Parameters
    /// - `user` — The authenticated user.
    /// - `dataset_id` — Identifier of the dataset that contains the graph
    ///   (used for dataset-admin bypass check).
    /// - `graph_iri` — The named graph being accessed.
    /// - `permission` — The graph-level permission being requested.
    pub async fn check(
        &self,
        user: &User,
        dataset_id: &str,
        graph_iri: &str,
        permission: GraphPermission,
    ) -> AclDecision {
        // ── Step 1: Admin bypass ─────────────────────────────────────────────
        let is_global_admin = user.permissions.contains(&Permission::GlobalAdmin);
        let is_dataset_admin = user
            .permissions
            .contains(&Permission::DatasetAdmin(dataset_id.to_string()));

        if is_global_admin || is_dataset_admin {
            debug!(
                "GraphAclPolicy: admin bypass for {} on <{}>",
                user.username, graph_iri
            );
            return AclDecision::Allow {
                reason: AllowReason::DatasetAdmin,
            };
        }

        // ── Step 2: Dataset-level RBAC gate ──────────────────────────────────
        // At dataset level we require at least the read permission for any
        // graph-level check; write/admin require the corresponding dataset perm.
        let dataset_ok = self.dataset_rbac_allows(user, dataset_id, permission);
        if !dataset_ok {
            debug!(
                "GraphAclPolicy: dataset RBAC denied {} {:?} in dataset '{}'",
                user.username, permission, dataset_id
            );
            return AclDecision::Deny {
                reason: DenyReason::DatasetRbacDenied,
            };
        }

        // ── Step 3: Graph ACL check ───────────────────────────────────────────
        let acl_allows = self.store.check_user(graph_iri, user, permission).await;
        if acl_allows {
            debug!(
                "GraphAclPolicy: ACL allowed {} {:?} on <{}>",
                user.username, permission, graph_iri
            );
            AclDecision::Allow {
                reason: AllowReason::ExplicitAcl,
            }
        } else {
            debug!(
                "GraphAclPolicy: no ACL entry for {} {:?} on <{}>",
                user.username, permission, graph_iri
            );
            AclDecision::Deny {
                reason: DenyReason::NoAclEntry,
            }
        }
    }

    /// Minimal dataset-level RBAC check embedded directly in the policy.
    ///
    /// This uses the `Permission` enum already on the `User` struct so it works
    /// without an async call to the full `RbacManager`.  The rules are:
    ///
    /// | Permission requested | Dataset-level requirements                              |
    /// |----------------------|--------------------------------------------------------|
    /// | `Read`               | `GlobalRead`, `DatasetRead(id)`, `GlobalWrite`, `GlobalAdmin` |
    /// | `Write`              | `GlobalWrite`, `DatasetWrite(id)`, `GlobalAdmin`       |
    /// | `Admin`              | `GlobalAdmin`, `DatasetAdmin(id)`                      |
    fn dataset_rbac_allows(
        &self,
        user: &User,
        dataset_id: &str,
        permission: GraphPermission,
    ) -> bool {
        user.permissions.iter().any(|p| match permission {
            GraphPermission::Read => {
                matches!(
                    p,
                    Permission::GlobalRead
                        | Permission::GlobalWrite
                        | Permission::GlobalAdmin
                        | Permission::SparqlQuery
                ) || matches!(p, Permission::DatasetRead(id) if id == dataset_id)
                    || matches!(p, Permission::DatasetWrite(id) if id == dataset_id)
                    || matches!(p, Permission::DatasetAdmin(id) if id == dataset_id)
            }

            GraphPermission::Write => {
                matches!(
                    p,
                    Permission::GlobalWrite | Permission::GlobalAdmin | Permission::SparqlUpdate
                ) || matches!(p, Permission::DatasetWrite(id) if id == dataset_id)
                    || matches!(p, Permission::DatasetAdmin(id) if id == dataset_id)
            }

            GraphPermission::Admin => {
                matches!(p, Permission::GlobalAdmin)
                    || matches!(p, Permission::DatasetAdmin(id) if id == dataset_id)
            }
        })
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;

    // ── Helpers ───────────────────────────────────────────────────────────────

    fn make_user(username: &str, roles: &[&str], permissions: &[Permission]) -> User {
        User {
            username: username.to_string(),
            roles: roles.iter().map(|s| s.to_string()).collect(),
            email: None,
            full_name: None,
            last_login: Some(Utc::now()),
            permissions: permissions.to_vec(),
        }
    }

    fn grant_req(
        graph_iri: &str,
        principal: Principal,
        permission: GraphPermission,
    ) -> GrantGraphAccess {
        GrantGraphAccess {
            graph_iri: graph_iri.to_string(),
            principal,
            permission,
            granted_by: Some("admin".to_string()),
        }
    }

    fn revoke_req(
        graph_iri: &str,
        principal: Principal,
        permission: Option<GraphPermission>,
    ) -> RevokeGraphAccess {
        RevokeGraphAccess {
            graph_iri: graph_iri.to_string(),
            principal,
            permission,
        }
    }

    const G1: &str = "http://example.org/graph/g1";
    const G2: &str = "http://example.org/graph/g2";
    const G3: &str = "http://example.org/graph/g3";

    // ── 1. Grant read and verify ───────────────────────────────────────────────

    #[tokio::test]
    async fn test_grant_read_allows_read() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();

        assert!(store.check(G1, &alice, GraphPermission::Read).await);
    }

    // ── 2. Grant read does not allow write ────────────────────────────────────

    #[tokio::test]
    async fn test_grant_read_denies_write() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();

        assert!(!store.check(G1, &alice, GraphPermission::Write).await);
    }

    // ── 3. Grant write implies read ───────────────────────────────────────────

    #[tokio::test]
    async fn test_grant_write_implies_read() {
        let store = GraphAclStore::new();
        let bob = Principal::user("bob");
        store
            .grant(&grant_req(G1, bob.clone(), GraphPermission::Write))
            .await
            .unwrap();

        assert!(store.check(G1, &bob, GraphPermission::Read).await);
        assert!(store.check(G1, &bob, GraphPermission::Write).await);
        assert!(!store.check(G1, &bob, GraphPermission::Admin).await);
    }

    // ── 4. Grant admin implies write and read ─────────────────────────────────

    #[tokio::test]
    async fn test_grant_admin_implies_all() {
        let store = GraphAclStore::new();
        let carol = Principal::user("carol");
        store
            .grant(&grant_req(G1, carol.clone(), GraphPermission::Admin))
            .await
            .unwrap();

        assert!(store.check(G1, &carol, GraphPermission::Read).await);
        assert!(store.check(G1, &carol, GraphPermission::Write).await);
        assert!(store.check(G1, &carol, GraphPermission::Admin).await);
    }

    // ── 5. Revoke specific permission ─────────────────────────────────────────

    #[tokio::test]
    async fn test_revoke_permission() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Write))
            .await
            .unwrap();
        let removed = store
            .revoke(&revoke_req(G1, alice.clone(), Some(GraphPermission::Write)))
            .await
            .unwrap();
        assert!(removed);
        assert!(!store.check(G1, &alice, GraphPermission::Write).await);
    }

    // ── 6. Revoke all permissions for a principal ─────────────────────────────

    #[tokio::test]
    async fn test_revoke_all_permissions() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Write))
            .await
            .unwrap();

        let removed = store
            .revoke(&revoke_req(G1, alice.clone(), None))
            .await
            .unwrap();
        assert!(removed);
        assert!(!store.check(G1, &alice, GraphPermission::Read).await);
        assert!(!store.check(G1, &alice, GraphPermission::Write).await);
        assert_eq!(store.entry_count().await, 0);
    }

    // ── 7. Revoke is idempotent on missing entry ──────────────────────────────

    #[tokio::test]
    async fn test_revoke_missing_entry_is_ok() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        let result = store
            .revoke(&revoke_req(G1, alice.clone(), Some(GraphPermission::Read)))
            .await;
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    // ── 8. No ACL entry denies access ─────────────────────────────────────────

    #[tokio::test]
    async fn test_no_entry_denies_access() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        assert!(!store.check(G1, &alice, GraphPermission::Read).await);
    }

    // ── 9. Multiple principals, multiple graphs ───────────────────────────────

    #[tokio::test]
    async fn test_multiple_principals_multiple_graphs() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        let bob = Principal::user("bob");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();
        store
            .grant(&grant_req(G2, bob.clone(), GraphPermission::Write))
            .await
            .unwrap();

        assert!(store.check(G1, &alice, GraphPermission::Read).await);
        assert!(!store.check(G1, &bob, GraphPermission::Read).await);
        assert!(store.check(G2, &bob, GraphPermission::Read).await);
        assert!(!store.check(G2, &alice, GraphPermission::Write).await);
    }

    // ── 10. Role-based principal matching ─────────────────────────────────────

    #[tokio::test]
    async fn test_role_principal_matches_user_with_role() {
        let store = GraphAclStore::new();
        let editor_role = Principal::role("editor");
        store
            .grant(&grant_req(G1, editor_role, GraphPermission::Write))
            .await
            .unwrap();

        let alice = make_user("alice", &["editor"], &[Permission::GlobalRead]);
        assert!(store.check_user(G1, &alice, GraphPermission::Write).await);
    }

    // ── 11. Role principal does not match user without role ───────────────────

    #[tokio::test]
    async fn test_role_principal_no_match_without_role() {
        let store = GraphAclStore::new();
        let editor_role = Principal::role("editor");
        store
            .grant(&grant_req(G1, editor_role, GraphPermission::Write))
            .await
            .unwrap();

        let bob = make_user("bob", &["reader"], &[Permission::GlobalRead]);
        assert!(!store.check_user(G1, &bob, GraphPermission::Write).await);
    }

    // ── 12. Grant returns correct entry ──────────────────────────────────────

    #[tokio::test]
    async fn test_grant_returns_entry() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        let entry = store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();

        assert_eq!(entry.graph_iri, G1);
        assert_eq!(entry.principal, alice);
        assert_eq!(entry.permission, GraphPermission::Read);
        assert_eq!(entry.granted_by.as_deref(), Some("admin"));
    }

    // ── 13. List graph entries ────────────────────────────────────────────────

    #[tokio::test]
    async fn test_list_graph_entries() {
        let store = GraphAclStore::new();
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G1,
                Principal::user("bob"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G2,
                Principal::user("carol"),
                GraphPermission::Admin,
            ))
            .await
            .unwrap();

        let g1_entries = store.list_graph(G1).await;
        assert_eq!(g1_entries.len(), 2);
        assert!(g1_entries.iter().all(|e| e.graph_iri == G1));

        let g2_entries = store.list_graph(G2).await;
        assert_eq!(g2_entries.len(), 1);
    }

    // ── 14. List all entries ──────────────────────────────────────────────────

    #[tokio::test]
    async fn test_list_all_entries() {
        let store = GraphAclStore::new();
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G2,
                Principal::user("bob"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G3,
                Principal::role("admin_role"),
                GraphPermission::Admin,
            ))
            .await
            .unwrap();

        let all = store.list_all().await;
        assert_eq!(all.len(), 3);
    }

    // ── 15. Purge graph ───────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_purge_graph() {
        let store = GraphAclStore::new();
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G1,
                Principal::user("bob"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G2,
                Principal::user("carol"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();

        let removed = store.purge_graph(G1).await.unwrap();
        assert_eq!(removed, 2);
        assert_eq!(store.entry_count().await, 1); // G2 entry remains
        assert!(
            !store
                .check(G1, &Principal::user("alice"), GraphPermission::Read)
                .await
        );
    }

    // ── 16. Graphs for principal ──────────────────────────────────────────────

    #[tokio::test]
    async fn test_graphs_for_principal() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();
        store
            .grant(&grant_req(G2, alice.clone(), GraphPermission::Write))
            .await
            .unwrap();
        store
            .grant(&grant_req(
                G3,
                Principal::user("bob"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();

        let alice_graphs = store.graphs_for_principal(&alice).await;
        assert_eq!(alice_graphs.len(), 2);
        assert!(alice_graphs.iter().any(|(g, _)| g == G1));
        assert!(alice_graphs.iter().any(|(g, _)| g == G2));
    }

    // ── 17. Policy: dataset admin bypass ──────────────────────────────────────

    #[tokio::test]
    async fn test_policy_dataset_admin_bypasses_acl() {
        let store = GraphAclStore::new();
        // No ACL entry for G1 at all — admin should still get through
        let policy = GraphAclPolicy::new(store);

        let admin = make_user("admin", &["admin"], &[Permission::GlobalAdmin]);
        let decision = policy
            .check(&admin, "ds1", G1, GraphPermission::Write)
            .await;
        assert!(decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Allow {
                reason: AllowReason::DatasetAdmin
            }
        );
    }

    // ── 18. Policy: dataset-scoped admin bypass ───────────────────────────────

    #[tokio::test]
    async fn test_policy_dataset_scoped_admin_bypass() {
        let store = GraphAclStore::new();
        let policy = GraphAclPolicy::new(store);

        let ds_admin = make_user(
            "ds_admin",
            &["dataset_admin"],
            &[Permission::DatasetAdmin("ds1".to_string())],
        );
        let decision = policy
            .check(&ds_admin, "ds1", G1, GraphPermission::Admin)
            .await;
        assert!(decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Allow {
                reason: AllowReason::DatasetAdmin
            }
        );
    }

    // ── 19. Policy: dataset RBAC denied ───────────────────────────────────────

    #[tokio::test]
    async fn test_policy_dataset_rbac_denied() {
        let store = GraphAclStore::new();
        // Grant graph-level ACL but user has NO dataset-level permission
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        let policy = GraphAclPolicy::new(store);

        let alice = make_user("alice", &[], &[]); // no permissions at all
        let decision = policy
            .check(&alice, "ds1", G1, GraphPermission::Write)
            .await;
        assert!(!decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Deny {
                reason: DenyReason::DatasetRbacDenied
            }
        );
    }

    // ── 20. Policy: no graph ACL entry ────────────────────────────────────────

    #[tokio::test]
    async fn test_policy_no_acl_entry_denied() {
        let store = GraphAclStore::new();
        // No graph ACL entry, but user has dataset read permission
        let policy = GraphAclPolicy::new(store);

        let alice = make_user("alice", &["reader"], &[Permission::GlobalRead]);
        let decision = policy.check(&alice, "ds1", G1, GraphPermission::Read).await;
        assert!(!decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Deny {
                reason: DenyReason::NoAclEntry
            }
        );
    }

    // ── 21. Policy: dataset read + graph ACL allows ───────────────────────────

    #[tokio::test]
    async fn test_policy_combined_allows() {
        let store = GraphAclStore::new();
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();
        let policy = GraphAclPolicy::new(store);

        let alice = make_user("alice", &["reader"], &[Permission::GlobalRead]);
        let decision = policy.check(&alice, "ds1", G1, GraphPermission::Read).await;
        assert!(decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Allow {
                reason: AllowReason::ExplicitAcl
            }
        );
    }

    // ── 22. Policy: write blocked despite graph ACL when dataset denies write ──

    #[tokio::test]
    async fn test_policy_dataset_denies_write_despite_graph_acl() {
        let store = GraphAclStore::new();
        // Graph ACL grants write, but user only has GlobalRead (no write)
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        let policy = GraphAclPolicy::new(store);

        let alice = make_user("alice", &["reader"], &[Permission::GlobalRead]);
        let decision = policy
            .check(&alice, "ds1", G1, GraphPermission::Write)
            .await;
        // Dataset RBAC doesn't permit write → deny
        assert!(!decision.is_allow());
        assert_eq!(
            decision,
            AclDecision::Deny {
                reason: DenyReason::DatasetRbacDenied
            }
        );
    }

    // ── 23. Policy: role-based principal in combined check ────────────────────

    #[tokio::test]
    async fn test_policy_role_based_principal_combined() {
        let store = GraphAclStore::new();
        store
            .grant(&grant_req(
                G1,
                Principal::role("writer"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        let policy = GraphAclPolicy::new(store);

        let alice = make_user("alice", &["writer"], &[Permission::GlobalWrite]);
        let decision = policy
            .check(&alice, "ds1", G1, GraphPermission::Write)
            .await;
        assert!(decision.is_allow());
    }

    // ── 24. GraphPermission ordering and display ──────────────────────────────

    #[test]
    fn test_graph_permission_display() {
        assert_eq!(GraphPermission::Read.as_str(), "read");
        assert_eq!(GraphPermission::Write.as_str(), "write");
        assert_eq!(GraphPermission::Admin.as_str(), "admin");
        assert_eq!(format!("{}", GraphPermission::Read), "read");
    }

    // ── 25. Entry count stays accurate across grant/revoke ────────────────────

    #[tokio::test]
    async fn test_entry_count_accurate() {
        let store = GraphAclStore::new();
        assert_eq!(store.entry_count().await, 0);
        store
            .grant(&grant_req(
                G1,
                Principal::user("alice"),
                GraphPermission::Read,
            ))
            .await
            .unwrap();
        assert_eq!(store.entry_count().await, 1);
        store
            .grant(&grant_req(
                G1,
                Principal::user("bob"),
                GraphPermission::Write,
            ))
            .await
            .unwrap();
        assert_eq!(store.entry_count().await, 2);
        store
            .revoke(&revoke_req(
                G1,
                Principal::user("alice"),
                Some(GraphPermission::Read),
            ))
            .await
            .unwrap();
        assert_eq!(store.entry_count().await, 1);
    }

    // ── 26. Idempotent grant ──────────────────────────────────────────────────

    #[tokio::test]
    async fn test_idempotent_grant() {
        let store = GraphAclStore::new();
        let alice = Principal::user("alice");
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();
        store
            .grant(&grant_req(G1, alice.clone(), GraphPermission::Read))
            .await
            .unwrap();
        assert_eq!(store.entry_count().await, 1); // still just one entry
    }
}