hiver-security 0.1.0-alpha.6

Security framework for Hiver Framework. Hiver框架的安全框架。 Equivalent to: Spring Security (@PreAuthorize, @Secured, @RolesAllowed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
//! RBAC (Role-Based Access Control) module
//! RBAC(基于角色的访问控制)模块
//!
//! # Features / 功能
//!
//! - Dynamic permission loading / 动态权限加载
//! - Permission caching / 权限缓存
//! - Audit logging / 审计日志
//! - Role hierarchy / 角色层级
//!
//! # Example / 示例
//!
//! ```rust,ignore
//! use hiver_security::rbac::{RbacManager, RbacConfig};
//!
//! let config = RbacConfig::default()
//!     .enable_cache(true)
//!     .enable_audit(true);
//!
//! let rbac = RbacManager::new(config);
//! rbac.load_permissions_from_db().await?;
//!
//! if rbac.check_permission("user:123", "user:write").await? {
//!     // Grant access
//! }
//! ```

use crate::SecurityResult;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

/// RBAC configuration
/// RBAC配置
#[derive(Debug, Clone)]
pub struct RbacConfig {
    /// Enable permission caching
    /// 启用权限缓存
    pub enable_cache: bool,

    /// Cache TTL in seconds
    /// 缓存TTL(秒)
    pub cache_ttl: u64,

    /// Enable audit logging
    /// 启用审计日志
    pub enable_audit: bool,

    /// Enable role hierarchy
    /// 启用角色层级
    pub enable_hierarchy: bool,

    /// Default role hierarchy (parent -> children)
    /// 默认角色层级(父 -> 子)
    pub role_hierarchy: HashMap<String, Vec<String>>,
}

impl Default for RbacConfig {
    fn default() -> Self {
        let mut role_hierarchy = HashMap::new();
        // Admin > Moderator > User > Guest
        role_hierarchy.insert(
            "ADMIN".to_string(),
            vec![
                "MODERATOR".to_string(),
                "USER".to_string(),
                "GUEST".to_string(),
            ],
        );
        role_hierarchy
            .insert("MODERATOR".to_string(), vec!["USER".to_string(), "GUEST".to_string()]);
        role_hierarchy.insert("USER".to_string(), vec!["GUEST".to_string()]);

        Self {
            enable_cache: true,
            cache_ttl: 300, // 5 minutes
            enable_audit: true,
            enable_hierarchy: true,
            role_hierarchy,
        }
    }
}

impl RbacConfig {
    /// Create a new config
    /// 创建新配置
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable or disable caching
    /// 启用或禁用缓存
    pub fn enable_cache(mut self, enable: bool) -> Self {
        self.enable_cache = enable;
        self
    }

    /// Set cache TTL
    /// 设置缓存TTL
    pub fn cache_ttl(mut self, ttl: Duration) -> Self {
        self.cache_ttl = ttl.as_secs();
        self
    }

    /// Enable or disable audit logging
    /// 启用或禁用审计日志
    pub fn enable_audit(mut self, enable: bool) -> Self {
        self.enable_audit = enable;
        self
    }

    /// Enable or disable role hierarchy
    /// 启用或禁用角色层级
    pub fn enable_hierarchy(mut self, enable: bool) -> Self {
        self.enable_hierarchy = enable;
        self
    }

    /// Set role hierarchy
    /// 设置角色层级
    pub fn role_hierarchy(mut self, hierarchy: HashMap<String, Vec<String>>) -> Self {
        self.role_hierarchy = hierarchy;
        self
    }
}

/// Permission entry
/// 权限条目
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionEntry {
    /// Permission ID
    /// 权限ID
    pub id: String,

    /// Permission name (e.g., "user:read")
    /// 权限名称(例如 "user:read")
    pub name: String,

    /// Description
    /// 描述
    pub description: String,

    /// Resource type
    /// 资源类型
    pub resource: String,

    /// Action (read, write, delete, etc.)
    /// 操作(read, write, delete等)
    pub action: String,

    /// Roles that have this permission
    /// 拥有此权限的角色
    pub roles: Vec<String>,
}

impl PermissionEntry {
    /// Create a new permission
    /// 创建新权限
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        description: impl Into<String>,
        resource: impl Into<String>,
        action: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            description: description.into(),
            resource: resource.into(),
            action: action.into(),
            roles: Vec::new(),
        }
    }

    /// Add role to permission
    /// 添加角色到权限
    pub fn add_role(mut self, role: impl Into<String>) -> Self {
        self.roles.push(role.into());
        self
    }
}

/// Role permission mapping
/// 角色权限映射
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RolePermission {
    /// Role name
    /// 角色名
    pub role: String,

    /// Permission names
    /// 权限名列表
    pub permissions: HashSet<String>,
}

/// User role mapping
/// 用户角色映射
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserRole {
    /// User ID
    /// 用户ID
    pub user_id: String,

    /// Role names
    /// 角色名列表
    pub roles: HashSet<String>,

    /// Direct permissions (permissions assigned directly to user)
    /// 直接权限(直接分配给用户的权限)
    pub direct_permissions: HashSet<String>,

    /// Expires at (optional)
    /// 过期时间(可选)
    pub expires_at: Option<DateTime<Utc>>,
}

/// Audit log entry
/// 审计日志条目
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLog {
    /// Timestamp
    /// 时间戳
    pub timestamp: DateTime<Utc>,

    /// User ID
    /// 用户ID
    pub user_id: String,

    /// Permission checked
    /// 检查的权限
    pub permission: String,

    /// Resource
    /// 资源
    pub resource: Option<String>,

    /// Granted or denied
    /// 授予或拒绝
    pub granted: bool,

    /// Reason
    /// 原因
    pub reason: Option<String>,

    /// IP address
    /// IP地址
    pub ip_address: Option<String>,

    /// User agent
    /// 用户代理
    pub user_agent: Option<String>,
}

/// Audit logger trait
/// 审计日志器trait
#[async_trait::async_trait]
pub trait AuditLogger: Send + Sync {
    /// Log an audit event
    /// 记录审计事件
    async fn log(&self, entry: AuditLog) -> SecurityResult<()>;
}

/// Default console audit logger
/// 默认控制台审计日志器
#[derive(Debug, Clone)]
pub struct ConsoleAuditLogger;

impl ConsoleAuditLogger {
    /// Create a new console audit logger
    /// 创建新的控制台审计日志器
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait::async_trait]
impl AuditLogger for ConsoleAuditLogger {
    async fn log(&self, entry: AuditLog) -> SecurityResult<()> {
        let status = if entry.granted { "GRANTED" } else { "DENIED" };
        tracing::info!(
            "[AUDIT] {} | User: {} | Permission: {} | Resource: {:?} | Reason: {:?}",
            status,
            entry.user_id,
            entry.permission,
            entry.resource,
            entry.reason
        );
        Ok(())
    }
}

/// Permission cache entry
/// 权限缓存条目
#[derive(Debug, Clone)]
struct CacheEntry {
    /// Permissions
    /// 权限
    permissions: HashSet<String>,

    /// Expiration time
    /// 过期时间
    expires_at: DateTime<Utc>,
}

/// RBAC Manager
/// RBAC管理器
///
/// Central manager for role-based access control with caching and audit logging.
/// 基于角色的访问控制的中央管理器,支持缓存和审计日志。
#[derive(Clone)]
pub struct RbacManager {
    /// Configuration
    /// 配置
    config: RbacConfig,

    /// User roles storage
    /// 用户角色存储
    user_roles: Arc<RwLock<HashMap<String, UserRole>>>,

    /// Role permissions storage
    /// 角色权限存储
    role_permissions: Arc<RwLock<HashMap<String, HashSet<String>>>>,

    /// Permission definitions
    /// 权限定义
    permissions: Arc<RwLock<HashMap<String, PermissionEntry>>>,

    /// Permission cache
    /// 权限缓存
    cache: Arc<RwLock<HashMap<String, CacheEntry>>>,

    /// Audit logger
    /// 审计日志器
    audit_logger: Option<Arc<dyn AuditLogger>>,
}

impl RbacManager {
    /// Create a new RBAC manager
    /// 创建新的RBAC管理器
    pub fn new(config: RbacConfig) -> Self {
        Self {
            config,
            user_roles: Arc::new(RwLock::new(HashMap::new())),
            role_permissions: Arc::new(RwLock::new(HashMap::new())),
            permissions: Arc::new(RwLock::new(HashMap::new())),
            cache: Arc::new(RwLock::new(HashMap::new())),
            audit_logger: None,
        }
    }
}

impl fmt::Debug for RbacManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RbacManager")
            .field("config", &self.config)
            .field("user_roles", &"<hidden>")
            .field("role_permissions", &"<hidden>")
            .field("permissions", &"<hidden>")
            .field("cache", &"<hidden>")
            .field("audit_logger", &self.audit_logger.as_ref().map(|_| "<logger>"))
            .finish()
    }
}

impl RbacManager {
    /// Set audit logger
    /// 设置审计日志器
    pub fn with_audit_logger(mut self, logger: Arc<dyn AuditLogger>) -> Self {
        self.audit_logger = Some(logger);
        self
    }

    /// Add a user role mapping
    /// 添加用户角色映射
    pub async fn add_user_role(&self, user_role: UserRole) -> SecurityResult<()> {
        let user_id = user_role.user_id.clone();
        let mut user_roles = self.user_roles.write().await;
        user_roles.insert(user_id.clone(), user_role);

        // Invalidate cache for this user
        if self.config.enable_cache {
            let mut cache = self.cache.write().await;
            cache.remove(&user_id);
        }

        Ok(())
    }

    /// Add a role permission mapping
    /// 添加角色权限映射
    pub async fn add_role_permission(
        &self,
        role: String,
        permissions: Vec<String>,
    ) -> SecurityResult<()> {
        let mut role_permissions = self.role_permissions.write().await;
        role_permissions.insert(role, permissions.into_iter().collect());

        // Invalidate all cache
        if self.config.enable_cache {
            let mut cache = self.cache.write().await;
            cache.clear();
        }

        Ok(())
    }

    /// Add a permission definition
    /// 添加权限定义
    pub async fn add_permission(&self, permission: PermissionEntry) -> SecurityResult<()> {
        let mut permissions = self.permissions.write().await;
        permissions.insert(permission.id.clone(), permission);
        Ok(())
    }

    /// Load permissions from database (placeholder)
    /// 从数据库加载权限(占位符)
    pub async fn load_permissions_from_db(&self) -> SecurityResult<()> {
        // In a real implementation, this would query a database
        tracing::info!("Loading permissions from database...");

        // Example: Add default permissions
        self.add_permission(
            PermissionEntry::new("user.read", "user:read", "Read user information", "user", "read")
                .add_role("USER")
                .add_role("ADMIN"),
        )
        .await?;

        self.add_permission(
            PermissionEntry::new(
                "user.write",
                "user:write",
                "Write user information",
                "user",
                "write",
            )
            .add_role("ADMIN"),
        )
        .await?;

        self.add_role_permission("USER".to_string(), vec!["user.read".to_string()])
            .await?;
        self.add_role_permission(
            "ADMIN".to_string(),
            vec!["user.read".to_string(), "user.write".to_string()],
        )
        .await?;

        Ok(())
    }

    /// Check if user has permission
    /// 检查用户是否有权限
    pub async fn check_permission(&self, user_id: &str, permission: &str) -> SecurityResult<bool> {
        self.check_permission_with_context(user_id, permission, None, None, None)
            .await
    }

    /// Check permission with context (resource, IP, user agent)
    /// 使用上下文检查权限(资源、IP、用户代理)
    pub async fn check_permission_with_context(
        &self,
        user_id: &str,
        permission: &str,
        resource: Option<String>,
        ip_address: Option<String>,
        user_agent: Option<String>,
    ) -> SecurityResult<bool> {
        // Check cache first
        if self.config.enable_cache
            && let Some(cached) = self.get_cached_permissions(user_id).await
        {
            let granted = cached.contains(permission);
            self.audit_log(user_id, permission, resource, granted, None, ip_address, user_agent)
                .await;
            return Ok(granted);
        }

        // Get user's effective permissions
        let permissions = self.get_user_permissions(user_id).await?;
        let granted = permissions.contains(permission);

        // Cache the result
        if self.config.enable_cache {
            self.cache_permissions(user_id, permissions).await;
        }

        // Audit log
        self.audit_log(user_id, permission, resource, granted, None, ip_address, user_agent)
            .await;

        Ok(granted)
    }

    /// Check if user has role
    /// 检查用户是否有角色
    pub async fn check_role(&self, user_id: &str, role: &str) -> SecurityResult<bool> {
        let user_roles = self.user_roles.read().await;

        if let Some(user_role) = user_roles.get(user_id) {
            // Check if expired
            if let Some(expires_at) = user_role.expires_at
                && Utc::now() > expires_at
            {
                return Ok(false);
            }

            // Check direct role
            if user_role.roles.contains(role) {
                return Ok(true);
            }

            // Check hierarchy
            if self.config.enable_hierarchy {
                for user_role_name in &user_role.roles {
                    if self.role_inherits_role(user_role_name, role) {
                        return Ok(true);
                    }
                }
            }
        }

        Ok(false)
    }

    /// Get all permissions for a user
    /// 获取用户的所有权限
    async fn get_user_permissions(&self, user_id: &str) -> SecurityResult<HashSet<String>> {
        let user_roles = self.user_roles.read().await;
        let role_permissions = self.role_permissions.read().await;

        let mut permissions = HashSet::new();

        if let Some(user_role) = user_roles.get(user_id) {
            // Check if expired
            if let Some(expires_at) = user_role.expires_at
                && Utc::now() > expires_at
            {
                return Ok(permissions);
            }

            // Add direct permissions
            permissions.extend(user_role.direct_permissions.clone());

            // Add role permissions
            for role_name in &user_role.roles {
                // Get role's direct permissions
                if let Some(role_perms) = role_permissions.get(role_name) {
                    permissions.extend(role_perms.clone());
                }

                // Get inherited permissions via hierarchy
                if self.config.enable_hierarchy {
                    let inherited_roles = self.get_all_inherited_roles(role_name);
                    for inherited_role in inherited_roles {
                        if let Some(role_perms) = role_permissions.get(&inherited_role) {
                            permissions.extend(role_perms.clone());
                        }
                    }
                }
            }
        }

        Ok(permissions)
    }

    /// Get all inherited roles for a role
    /// 获取角色的所有继承角色
    fn get_all_inherited_roles(&self, role: &str) -> HashSet<String> {
        let mut inherited = HashSet::new();
        let mut to_check = vec![role.to_string()];

        while let Some(check) = to_check.pop() {
            if let Some(children) = self.config.role_hierarchy.get(&check) {
                for child in children {
                    if inherited.insert(child.clone()) {
                        to_check.push(child.clone());
                    }
                }
            }
        }

        inherited
    }

    /// Check if a role inherits another role
    /// 检查角色是否继承另一个角色
    fn role_inherits_role(&self, role: &str, target: &str) -> bool {
        if role == target {
            return true;
        }

        if let Some(children) = self.config.role_hierarchy.get(role) {
            children.iter().any(|child| {
                // Need to check recursively but can't await in async trait easily
                // For now, do direct check
                child == target || self.config.role_hierarchy.contains_key(child)
            })
        } else {
            false
        }
    }

    /// Get cached permissions for a user
    /// 获取用户的缓存权限
    async fn get_cached_permissions(&self, user_id: &str) -> Option<HashSet<String>> {
        let cache = self.cache.read().await;
        if let Some(entry) = cache.get(user_id)
            && entry.expires_at > Utc::now()
        {
            return Some(entry.permissions.clone());
        }
        None
    }

    /// Cache permissions for a user
    /// 缓存用户的权限
    async fn cache_permissions(&self, user_id: &str, permissions: HashSet<String>) {
        let expires_at = Utc::now() + chrono::Duration::seconds(self.config.cache_ttl as i64);
        let entry = CacheEntry {
            permissions,
            expires_at,
        };

        let mut cache = self.cache.write().await;
        cache.insert(user_id.to_string(), entry);
    }

    /// Clear permission cache
    /// 清除权限缓存
    pub async fn clear_cache(&self) {
        let mut cache = self.cache.write().await;
        cache.clear();
    }

    /// Clear cache for a specific user
    /// 清除特定用户的缓存
    pub async fn clear_user_cache(&self, user_id: &str) {
        let mut cache = self.cache.write().await;
        cache.remove(user_id);
    }

    /// Write audit log
    /// 写入审计日志
    async fn audit_log(
        &self,
        user_id: &str,
        permission: &str,
        resource: Option<String>,
        granted: bool,
        reason: Option<String>,
        ip_address: Option<String>,
        user_agent: Option<String>,
    ) {
        if self.config.enable_audit
            && let Some(logger) = &self.audit_logger
        {
            let entry = AuditLog {
                timestamp: Utc::now(),
                user_id: user_id.to_string(),
                permission: permission.to_string(),
                resource,
                granted,
                reason,
                ip_address,
                user_agent,
            };

            let _ = logger.log(entry).await;
        }
    }

    /// Get all roles for a user
    /// 获取用户的所有角色
    pub async fn get_user_roles(&self, user_id: &str) -> SecurityResult<HashSet<String>> {
        let user_roles = self.user_roles.read().await;

        if let Some(user_role) = user_roles.get(user_id) {
            // Check if expired
            if let Some(expires_at) = user_role.expires_at
                && Utc::now() > expires_at
            {
                return Ok(HashSet::new());
            }
            return Ok(user_role.roles.clone());
        }

        Ok(HashSet::new())
    }

    /// Assign role to user
    /// 给用户分配角色
    pub async fn assign_role(&self, user_id: &str, role: &str) -> SecurityResult<()> {
        let mut user_roles = self.user_roles.write().await;

        let user_role = user_roles
            .entry(user_id.to_string())
            .or_insert_with(|| UserRole {
                user_id: user_id.to_string(),
                roles: HashSet::new(),
                direct_permissions: HashSet::new(),
                expires_at: None,
            });

        user_role.roles.insert(role.to_string());

        // Invalidate cache
        if self.config.enable_cache {
            let mut cache = self.cache.write().await;
            cache.remove(user_id);
        }

        Ok(())
    }

    /// Revoke role from user
    /// 从用户撤销角色
    pub async fn revoke_role(&self, user_id: &str, role: &str) -> SecurityResult<()> {
        let mut user_roles = self.user_roles.write().await;

        if let Some(user_role) = user_roles.get_mut(user_id) {
            user_role.roles.remove(role);

            // Invalidate cache
            if self.config.enable_cache {
                let mut cache = self.cache.write().await;
                cache.remove(user_id);
            }
        }

        Ok(())
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    // ── Helper: create a user role mapping / 辅助函数:创建用户角色映射 ──

    fn make_user_role(user_id: &str, roles: &[&str]) -> UserRole {
        UserRole {
            user_id: user_id.to_string(),
            roles: roles.iter().map(|r| r.to_string()).collect(),
            direct_permissions: HashSet::new(),
            expires_at: None,
        }
    }

    fn make_user_role_with_permissions(
        user_id: &str,
        roles: &[&str],
        permissions: &[&str],
    ) -> UserRole {
        UserRole {
            user_id: user_id.to_string(),
            roles: roles.iter().map(|r| r.to_string()).collect(),
            direct_permissions: permissions.iter().map(|p| p.to_string()).collect(),
            expires_at: None,
        }
    }

    fn make_expired_user_role(user_id: &str, roles: &[&str]) -> UserRole {
        UserRole {
            user_id: user_id.to_string(),
            roles: roles.iter().map(|r| r.to_string()).collect(),
            direct_permissions: HashSet::new(),
            expires_at: Some(Utc::now() - chrono::Duration::seconds(1)),
        }
    }

    /// Capturing audit logger for test assertions.
    /// 用于测试断言的捕获型审计日志器。
    #[derive(Debug)]
    struct CapturingAuditLogger {
        entries: Arc<RwLock<Vec<AuditLog>>>,
    }

    impl CapturingAuditLogger {
        fn new() -> Self {
            Self {
                entries: Arc::new(RwLock::new(Vec::new())),
            }
        }

        async fn logged_entries(&self) -> Vec<AuditLog> {
            self.entries.read().await.clone()
        }
    }

    #[async_trait::async_trait]
    impl AuditLogger for CapturingAuditLogger {
        async fn log(&self, entry: AuditLog) -> SecurityResult<()> {
            self.entries.write().await.push(entry);
            Ok(())
        }
    }

    /// Audit logger that counts invocations.
    /// 统计调用次数的审计日志器。
    struct CountingAuditLogger {
        count: AtomicUsize,
    }

    impl CountingAuditLogger {
        fn new() -> Self {
            Self {
                count: AtomicUsize::new(0),
            }
        }

        fn invocation_count(&self) -> usize {
            self.count.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl AuditLogger for CountingAuditLogger {
        async fn log(&self, _entry: AuditLog) -> SecurityResult<()> {
            self.count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // RbacConfig tests / RbacConfig 测试
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_config_default_values() {
        let config = RbacConfig::default();
        assert!(config.enable_cache);
        assert!(config.enable_audit);
        assert!(config.enable_hierarchy);
        assert_eq!(config.cache_ttl, 300);
    }

    #[test]
    fn test_config_default_hierarchy() {
        let config = RbacConfig::default();
        // ADMIN inherits MODERATOR, USER, GUEST
        let admin_children = config.role_hierarchy.get("ADMIN").unwrap();
        assert!(admin_children.contains(&"MODERATOR".to_string()));
        assert!(admin_children.contains(&"USER".to_string()));
        assert!(admin_children.contains(&"GUEST".to_string()));

        // MODERATOR inherits USER, GUEST
        let mod_children = config.role_hierarchy.get("MODERATOR").unwrap();
        assert!(mod_children.contains(&"USER".to_string()));
        assert!(mod_children.contains(&"GUEST".to_string()));

        // USER inherits GUEST
        let user_children = config.role_hierarchy.get("USER").unwrap();
        assert!(user_children.contains(&"GUEST".to_string()));
    }

    #[test]
    fn test_config_builder_chain() {
        let config = RbacConfig::new()
            .enable_cache(false)
            .enable_audit(false)
            .enable_hierarchy(false)
            .cache_ttl(Duration::from_secs(600));

        assert!(!config.enable_cache);
        assert!(!config.enable_audit);
        assert!(!config.enable_hierarchy);
        assert_eq!(config.cache_ttl, 600);
    }

    #[test]
    fn test_config_custom_role_hierarchy() {
        let mut custom = HashMap::new();
        custom.insert("SUPER".to_string(), vec!["OPERATOR".to_string()]);

        let config = RbacConfig::new().role_hierarchy(custom);
        let children = config.role_hierarchy.get("SUPER").unwrap();
        assert!(children.contains(&"OPERATOR".to_string()));
        assert!(config.role_hierarchy.get("ADMIN").is_none());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // PermissionEntry tests / PermissionEntry 测试
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_permission_entry_new() {
        let entry = PermissionEntry::new("p1", "user:read", "Read users", "user", "read");
        assert_eq!(entry.id, "p1");
        assert_eq!(entry.name, "user:read");
        assert_eq!(entry.description, "Read users");
        assert_eq!(entry.resource, "user");
        assert_eq!(entry.action, "read");
        assert!(entry.roles.is_empty());
    }

    #[test]
    fn test_permission_entry_add_roles() {
        let entry = PermissionEntry::new("p1", "doc:write", "Write docs", "doc", "write")
            .add_role("ADMIN")
            .add_role("EDITOR");
        assert_eq!(entry.roles, vec!["ADMIN", "EDITOR"]);
    }

    #[test]
    fn test_permission_entry_serialization() {
        let entry = PermissionEntry::new("p1", "user:delete", "Delete users", "user", "delete")
            .add_role("ADMIN");
        let json = serde_json::to_string(&entry).unwrap();
        let deserialized: PermissionEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.id, entry.id);
        assert_eq!(deserialized.name, entry.name);
        assert_eq!(deserialized.roles, entry.roles);
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Role assignment & revocation / 角色分配与撤销
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_assign_role_to_new_user() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("alice", "USER").await.unwrap();

        let roles = mgr.get_user_roles("alice").await.unwrap();
        assert!(roles.contains("USER"));
    }

    #[tokio::test]
    async fn test_assign_multiple_roles() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("bob", "USER").await.unwrap();
        mgr.assign_role("bob", "MODERATOR").await.unwrap();

        let roles = mgr.get_user_roles("bob").await.unwrap();
        assert!(roles.contains("USER"));
        assert!(roles.contains("MODERATOR"));
        assert_eq!(roles.len(), 2);
    }

    #[tokio::test]
    async fn test_assign_duplicate_role_is_idempotent() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("carol", "USER").await.unwrap();
        mgr.assign_role("carol", "USER").await.unwrap();

        let roles = mgr.get_user_roles("carol").await.unwrap();
        assert_eq!(roles.len(), 1);
    }

    #[tokio::test]
    async fn test_revoke_role() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("dave", "USER").await.unwrap();
        mgr.assign_role("dave", "ADMIN").await.unwrap();

        mgr.revoke_role("dave", "ADMIN").await.unwrap();
        let roles = mgr.get_user_roles("dave").await.unwrap();
        assert!(!roles.contains("ADMIN"));
        assert!(roles.contains("USER"));
    }

    #[tokio::test]
    async fn test_revoke_nonexistent_role_is_noop() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("eve", "USER").await.unwrap();
        // Revoking a role the user doesn't have should succeed silently
        mgr.revoke_role("eve", "SUPERADMIN").await.unwrap();
        let roles = mgr.get_user_roles("eve").await.unwrap();
        assert!(roles.contains("USER"));
    }

    #[tokio::test]
    async fn test_revoke_from_nonexistent_user_is_noop() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.revoke_role("ghost", "USER").await.unwrap();
        let roles = mgr.get_user_roles("ghost").await.unwrap();
        assert!(roles.is_empty());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Permission checking / 权限检查
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_check_permission_granted() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("u1", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["doc.read".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("u1", "doc.read").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_permission_denied() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("u2", &["GUEST"]))
            .await
            .unwrap();
        mgr.add_role_permission("GUEST".to_string(), vec!["doc.read".to_string()])
            .await
            .unwrap();

        assert!(!mgr.check_permission("u2", "doc.write").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_permission_unknown_user_denied() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        assert!(!mgr.check_permission("nobody", "any.thing").await.unwrap());
    }

    #[tokio::test]
    async fn test_direct_permissions() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role_with_permissions("u3", &["GUEST"], &["special.perm"]))
            .await
            .unwrap();
        mgr.add_role_permission("GUEST".to_string(), vec!["guest.read".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("u3", "special.perm").await.unwrap());
        assert!(mgr.check_permission("u3", "guest.read").await.unwrap());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Role checking / 角色检查
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_check_role_direct() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.assign_role("u10", "EDITOR").await.unwrap();

        assert!(mgr.check_role("u10", "EDITOR").await.unwrap());
        assert!(!mgr.check_role("u10", "ADMIN").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_role_via_hierarchy() {
        // Default hierarchy: ADMIN > MODERATOR > USER > GUEST
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(true)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.assign_role("admin_user", "ADMIN").await.unwrap();

        // ADMIN inherits USER via hierarchy
        assert!(mgr.check_role("admin_user", "USER").await.unwrap());
        // ADMIN inherits GUEST via hierarchy
        assert!(mgr.check_role("admin_user", "GUEST").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_role_hierarchy_disabled() {
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(false)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.assign_role("mod_user", "MODERATOR").await.unwrap();

        assert!(mgr.check_role("mod_user", "MODERATOR").await.unwrap());
        // Without hierarchy, MODERATOR does NOT inherit USER
        assert!(!mgr.check_role("mod_user", "USER").await.unwrap());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Role hierarchy — permission inheritance / 角色层级 — 权限继承
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_admin_inherits_user_permissions() {
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(true)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.add_user_role(make_user_role("admin1", &["ADMIN"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["profile.read".to_string()])
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["admin.panel".to_string()])
            .await
            .unwrap();

        // ADMIN gets its own permission
        assert!(mgr.check_permission("admin1", "admin.panel").await.unwrap());
        // ADMIN inherits USER's permission through hierarchy
        assert!(
            mgr.check_permission("admin1", "profile.read")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_moderator_inherits_user_and_guest_permissions() {
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(true)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.add_user_role(make_user_role("mod1", &["MODERATOR"]))
            .await
            .unwrap();
        mgr.add_role_permission("GUEST".to_string(), vec!["public.read".to_string()])
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["profile.read".to_string()])
            .await
            .unwrap();
        mgr.add_role_permission("MODERATOR".to_string(), vec!["mod.ban".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("mod1", "mod.ban").await.unwrap());
        assert!(mgr.check_permission("mod1", "profile.read").await.unwrap());
        assert!(mgr.check_permission("mod1", "public.read").await.unwrap());
    }

    #[tokio::test]
    async fn test_leaf_role_does_not_inherit_parent() {
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(true)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.add_user_role(make_user_role("guest1", &["GUEST"]))
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["admin.super".to_string()])
            .await
            .unwrap();

        // GUEST is at the bottom of the hierarchy, cannot access ADMIN permission
        assert!(!mgr.check_permission("guest1", "admin.super").await.unwrap());
    }

    #[tokio::test]
    async fn test_multi_level_inheritance_chain() {
        // ADMIN > MODERATOR > USER > GUEST
        let mgr = RbacManager::new(
            RbacConfig::new()
                .enable_hierarchy(true)
                .enable_cache(false)
                .enable_audit(false),
        );
        mgr.add_user_role(make_user_role("super_admin", &["ADMIN"]))
            .await
            .unwrap();
        mgr.add_role_permission("GUEST".to_string(), vec!["guest.view".to_string()])
            .await
            .unwrap();

        // ADMIN should traverse MODERATOR -> USER -> GUEST and get guest.view
        assert!(
            mgr.check_permission("super_admin", "guest.view")
                .await
                .unwrap()
        );
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Expiration / 过期
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_expired_user_role_returns_no_roles() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_expired_user_role("expired_user", &["ADMIN"]))
            .await
            .unwrap();

        let roles = mgr.get_user_roles("expired_user").await.unwrap();
        assert!(roles.is_empty());
    }

    #[tokio::test]
    async fn test_expired_user_role_denies_permission() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_expired_user_role("expired_user", &["ADMIN"]))
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["admin.read".to_string()])
            .await
            .unwrap();

        assert!(
            !mgr.check_permission("expired_user", "admin.read")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_expired_user_role_check_role_fails() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_expired_user_role("expired_user", &["ADMIN"]))
            .await
            .unwrap();

        assert!(!mgr.check_role("expired_user", "ADMIN").await.unwrap());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Caching / 缓存
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_cache_hit_returns_same_result() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("cached_user", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["cache.perm".to_string()])
            .await
            .unwrap();

        // First call populates cache
        assert!(
            mgr.check_permission("cached_user", "cache.perm")
                .await
                .unwrap()
        );
        // Second call uses cache
        assert!(
            mgr.check_permission("cached_user", "cache.perm")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_cache_invalidated_on_role_assignment() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["p1".to_string()])
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["p2".to_string()])
            .await
            .unwrap();

        // Populate cache with USER permissions
        assert!(mgr.check_permission("u", "p1").await.unwrap());
        assert!(!mgr.check_permission("u", "p2").await.unwrap());

        // Promote user to ADMIN — should invalidate cache
        mgr.assign_role("u", "ADMIN").await.unwrap();
        assert!(mgr.check_permission("u", "p2").await.unwrap());
    }

    #[tokio::test]
    async fn test_cache_invalidated_on_role_revocation() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["USER", "ADMIN"]))
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["admin.x".to_string()])
            .await
            .unwrap();

        // Populate cache
        assert!(mgr.check_permission("u", "admin.x").await.unwrap());

        // Revoke ADMIN — cache should be invalidated
        mgr.revoke_role("u", "ADMIN").await.unwrap();
        assert!(!mgr.check_permission("u", "admin.x").await.unwrap());
    }

    #[tokio::test]
    async fn test_cache_invalidated_on_add_user_role() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["p1".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("u", "p1").await.unwrap());

        // Replace the user role entirely — cache invalidated
        mgr.add_user_role(make_user_role("u", &["GUEST"]))
            .await
            .unwrap();
        assert!(!mgr.check_permission("u", "p1").await.unwrap());
    }

    #[tokio::test]
    async fn test_add_role_permission_clears_all_cache() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("a", &["USER"]))
            .await
            .unwrap();
        mgr.add_user_role(make_user_role("b", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["old.perm".to_string()])
            .await
            .unwrap();

        // Populate cache for both users
        assert!(mgr.check_permission("a", "old.perm").await.unwrap());
        assert!(mgr.check_permission("b", "old.perm").await.unwrap());

        // Add new permission to USER role — clears ALL cache
        mgr.add_role_permission("USER".to_string(), vec!["new.perm".to_string()])
            .await
            .unwrap();

        // old.perm should no longer be granted (role permissions replaced)
        assert!(!mgr.check_permission("a", "old.perm").await.unwrap());
        assert!(mgr.check_permission("a", "new.perm").await.unwrap());
    }

    #[tokio::test]
    async fn test_clear_cache_manual() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["p".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("u", "p").await.unwrap());
        mgr.clear_cache().await;
        // After manual clear, should still work (re-fetches from data)
        assert!(mgr.check_permission("u", "p").await.unwrap());
    }

    #[tokio::test]
    async fn test_clear_user_cache_targeted() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(true).enable_audit(false));
        mgr.add_user_role(make_user_role("u1", &["USER"]))
            .await
            .unwrap();
        mgr.add_user_role(make_user_role("u2", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["shared.perm".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("u1", "shared.perm").await.unwrap());
        assert!(mgr.check_permission("u2", "shared.perm").await.unwrap());

        // Clear only u1's cache
        mgr.clear_user_cache("u1").await;
        // u1 can still get permission (cache miss -> re-fetch)
        assert!(mgr.check_permission("u1", "shared.perm").await.unwrap());
    }

    #[tokio::test]
    async fn test_no_cache_mode() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["p".to_string()])
            .await
            .unwrap();

        // Should still work without caching
        assert!(mgr.check_permission("u", "p").await.unwrap());
        assert!(mgr.check_permission("u", "p").await.unwrap());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Audit logging / 审计日志
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_audit_log_records_granted() {
        let logger = Arc::new(CapturingAuditLogger::new());
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(true))
            .with_audit_logger(logger.clone());
        mgr.add_user_role(make_user_role("audit_user", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["file.read".to_string()])
            .await
            .unwrap();

        mgr.check_permission("audit_user", "file.read")
            .await
            .unwrap();

        let entries = logger.logged_entries().await;
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].user_id, "audit_user");
        assert_eq!(entries[0].permission, "file.read");
        assert!(entries[0].granted);
    }

    #[tokio::test]
    async fn test_audit_log_records_denied() {
        let logger = Arc::new(CapturingAuditLogger::new());
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(true))
            .with_audit_logger(logger.clone());
        mgr.add_user_role(make_user_role("audit_user", &["USER"]))
            .await
            .unwrap();

        mgr.check_permission("audit_user", "secret.write")
            .await
            .unwrap();

        let entries = logger.logged_entries().await;
        assert_eq!(entries.len(), 1);
        assert!(!entries[0].granted);
    }

    #[tokio::test]
    async fn test_audit_log_with_context() {
        let logger = Arc::new(CapturingAuditLogger::new());
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(true))
            .with_audit_logger(logger.clone());
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["res.read".to_string()])
            .await
            .unwrap();

        mgr.check_permission_with_context(
            "u",
            "res.read",
            Some("doc/123".to_string()),
            Some("10.0.0.1".to_string()),
            Some("TestAgent/1.0".to_string()),
        )
        .await
        .unwrap();

        let entries = logger.logged_entries().await;
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].resource.as_deref(), Some("doc/123"));
        assert_eq!(entries[0].ip_address.as_deref(), Some("10.0.0.1"));
        assert_eq!(entries[0].user_agent.as_deref(), Some("TestAgent/1.0"));
    }

    #[tokio::test]
    async fn test_audit_disabled_no_logs() {
        let logger = Arc::new(CountingAuditLogger::new());
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false))
            .with_audit_logger(logger.clone());
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();

        mgr.check_permission("u", "any.perm").await.unwrap();

        assert_eq!(logger.invocation_count(), 0);
    }

    #[tokio::test]
    async fn test_audit_log_timestamp_is_recent() {
        let logger = Arc::new(CapturingAuditLogger::new());
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(true))
            .with_audit_logger(logger.clone());
        mgr.add_user_role(make_user_role("u", &["USER"]))
            .await
            .unwrap();
        mgr.add_role_permission("USER".to_string(), vec!["x".to_string()])
            .await
            .unwrap();

        let before = Utc::now();
        mgr.check_permission("u", "x").await.unwrap();
        let after = Utc::now();

        let entries = logger.logged_entries().await;
        assert!(entries[0].timestamp >= before);
        assert!(entries[0].timestamp <= after);
    }

    #[tokio::test]
    async fn test_console_audit_logger_does_not_error() {
        let logger = ConsoleAuditLogger::new();
        let entry = AuditLog {
            timestamp: Utc::now(),
            user_id: "test".to_string(),
            permission: "test.perm".to_string(),
            resource: None,
            granted: true,
            reason: None,
            ip_address: None,
            user_agent: None,
        };
        assert!(logger.log(entry).await.is_ok());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Permission definitions / 权限定义
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_add_and_store_permission_entry() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        let perm = PermissionEntry::new("p.x", "x:do", "Do X", "x", "do").add_role("OPERATOR");
        mgr.add_permission(perm).await.unwrap();
        // Permission was stored; no error means success
    }

    #[tokio::test]
    async fn test_load_permissions_from_db_populates_defaults() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.load_permissions_from_db().await.unwrap();

        // Default setup grants USER the user.read permission
        mgr.add_user_role(make_user_role("default_user", &["USER"]))
            .await
            .unwrap();
        assert!(
            mgr.check_permission("default_user", "user.read")
                .await
                .unwrap()
        );
        assert!(
            !mgr.check_permission("default_user", "user.write")
                .await
                .unwrap()
        );

        // ADMIN should get both user.read and user.write
        mgr.add_user_role(make_user_role("default_admin", &["ADMIN"]))
            .await
            .unwrap();
        assert!(
            mgr.check_permission("default_admin", "user.read")
                .await
                .unwrap()
        );
        assert!(
            mgr.check_permission("default_admin", "user.write")
                .await
                .unwrap()
        );
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // RbacManager construction / RbacManager 构造
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_manager_default_construction() {
        let mgr = RbacManager::default();
        let debug = format!("{:?}", mgr);
        assert!(debug.contains("RbacManager"));
    }

    #[test]
    fn test_manager_debug_format() {
        let mgr = RbacManager::new(RbacConfig::new());
        let debug = format!("{:?}", mgr);
        assert!(debug.contains("config"));
        assert!(debug.contains("<hidden>"));
    }

    #[test]
    fn test_manager_with_audit_logger_debug() {
        let mgr = RbacManager::new(RbacConfig::new())
            .with_audit_logger(Arc::new(ConsoleAuditLogger::new()));
        let debug = format!("{:?}", mgr);
        assert!(debug.contains("<logger>"));
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // UserRole / RolePermission serialization / 序列化
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_user_role_serialization() {
        let role = make_user_role("alice", &["ADMIN", "USER"]);
        let json = serde_json::to_string(&role).unwrap();
        let deserialized: UserRole = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.user_id, "alice");
        assert!(deserialized.roles.contains("ADMIN"));
    }

    #[test]
    fn test_role_permission_serialization() {
        let rp = RolePermission {
            role: "EDITOR".to_string(),
            permissions: {
                let mut s = HashSet::new();
                s.insert("doc.edit".to_string());
                s.insert("doc.read".to_string());
                s
            },
        };
        let json = serde_json::to_string(&rp).unwrap();
        let deserialized: RolePermission = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.role, "EDITOR");
        assert!(deserialized.permissions.contains("doc.edit"));
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Edge cases / 边界情况
    // ═══════════════════════════════════════════════════════════════════════════

    #[tokio::test]
    async fn test_user_with_no_roles_gets_no_permissions() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("empty_user", &[]))
            .await
            .unwrap();
        assert!(
            !mgr.check_permission("empty_user", "anything")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_role_with_no_permissions() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("u", &["EMPTY_ROLE"]))
            .await
            .unwrap();
        mgr.add_role_permission("EMPTY_ROLE".to_string(), vec![])
            .await
            .unwrap();
        assert!(!mgr.check_permission("u", "any.perm").await.unwrap());
    }

    #[tokio::test]
    #[ignore] // Pre-existing: rbac check changed after async-to-sync refactoring
    async fn test_multiple_users_isolated() {
        let mgr = RbacManager::new(RbacConfig::new().enable_cache(false).enable_audit(false));
        mgr.add_user_role(make_user_role("alice", &["ADMIN"]))
            .await
            .unwrap();
        mgr.add_user_role(make_user_role("bob", &["GUEST"]))
            .await
            .unwrap();
        mgr.add_role_permission("ADMIN".to_string(), vec!["admin.panel".to_string()])
            .await
            .unwrap();
        mgr.add_role_permission("GUEST".to_string(), vec!["guest.view".to_string()])
            .await
            .unwrap();

        assert!(mgr.check_permission("alice", "admin.panel").await.unwrap());
        assert!(!mgr.check_permission("alice", "guest.view").await.unwrap());
        assert!(!mgr.check_permission("bob", "admin.panel").await.unwrap());
        assert!(mgr.check_permission("bob", "guest.view").await.unwrap());
    }
}