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
//! Dynamic permission management module.
//! 动态权限管理模块。
//!
//! Provides a `PermissionRegistry` for registering permission definitions,
//! a `PermissionEvaluator` for checking permissions with caching, and
//! a `PermissionAuditLogger` for audit logging.
//!
//! 提供用于注册权限定义的 `PermissionRegistry`,用于带缓存的权限检查的
//! `PermissionEvaluator`,以及用于审计日志的 `PermissionAuditLogger`。
//!
//! # Example / 示例
//!
//! ```rust,ignore
//! use hiver_security::permission::{PermissionRegistry, PermissionEvaluator, PermissionAuditLogger};
//!
//! let registry = PermissionRegistry::new();
//! registry.register("user:read", "user", "read", "Read user data");
//! registry.grant_role_permission("USER", "user:read");
//!
//! let evaluator = PermissionEvaluator::new(registry);
//! assert!(evaluator.has_permission(&["USER".to_string()], "user:read"));
//! ```

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

/// Permission definition.
/// 权限定义。
///
/// Describes a single permission with its resource and action.
/// 描述具有资源和操作的单个权限。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionDef {
    /// Permission name (e.g., "user:read").
    /// 权限名称(例如 "user:read")。
    pub name: String,

    /// Resource type this permission applies to (e.g., "user").
    /// 此权限适用的资源类型(例如 "user")。
    pub resource: String,

    /// Action on the resource (e.g., "read", "write").
    /// 对资源的操作(例如 "read", "write")。
    pub action: String,

    /// Human-readable description.
    /// 人类可读的描述。
    pub description: String,
}

impl PermissionDef {
    /// Create a new permission definition.
    /// 创建新的权限定义。
    pub fn new(
        name: impl Into<String>,
        resource: impl Into<String>,
        action: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            resource: resource.into(),
            action: action.into(),
            description: description.into(),
        }
    }
}

/// Permission registry for dynamic permission management.
/// 动态权限管理的权限注册表。
///
/// Stores permission definitions and role-to-permission mappings.
/// 存储权限定义和角色到权限的映射。
#[derive(Debug, Clone)]
pub struct PermissionRegistry {
    permissions: HashMap<String, PermissionDef>,
    role_permissions: HashMap<String, HashSet<String>>,
}

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

impl PermissionRegistry {
    /// Create a new empty permission registry.
    /// 创建新的空权限注册表。
    pub fn new() -> Self {
        Self {
            permissions: HashMap::new(),
            role_permissions: HashMap::new(),
        }
    }

    /// Register a permission definition.
    /// 注册一个权限定义。
    pub fn register(
        &mut self,
        name: impl Into<String>,
        resource: impl Into<String>,
        action: impl Into<String>,
        description: impl Into<String>,
    ) {
        let name = name.into();
        let def = PermissionDef::new(name.clone(), resource, action, description);
        self.permissions.insert(name, def);
    }

    /// Register a `PermissionDef` directly.
    /// 直接注册 `PermissionDef`。
    pub fn register_def(&mut self, def: PermissionDef) {
        self.permissions.insert(def.name.clone(), def);
    }

    /// Grant a permission to a role.
    /// 将权限授予角色。
    pub fn grant_role_permission(
        &mut self,
        role: impl Into<String>,
        permission: impl Into<String>,
    ) {
        self.role_permissions
            .entry(role.into())
            .or_default()
            .insert(permission.into());
    }

    /// Grant multiple permissions to a role.
    /// 将多个权限授予角色。
    pub fn grant_role_permissions(
        &mut self,
        role: impl Into<String>,
        permissions: impl IntoIterator<Item = impl Into<String>>,
    ) {
        let set = self.role_permissions.entry(role.into()).or_default();
        for p in permissions {
            set.insert(p.into());
        }
    }

    /// Revoke a permission from a role.
    /// 从角色撤销权限。
    pub fn revoke_role_permission(&mut self, role: &str, permission: &str) -> bool {
        if let Some(perms) = self.role_permissions.get_mut(role) {
            perms.remove(permission)
        } else {
            false
        }
    }

    /// Remove a role's entire permission set.
    /// 移除角色的整个权限集。
    pub fn remove_role(&mut self, role: &str) -> bool {
        self.role_permissions.remove(role).is_some()
    }

    /// Check if a permission is registered.
    /// 检查权限是否已注册。
    pub fn has_permission(&self, permission: &str) -> bool {
        self.permissions.contains_key(permission)
    }

    /// Get a permission definition by name.
    /// 按名称获取权限定义。
    pub fn get_permission(&self, name: &str) -> Option<&PermissionDef> {
        self.permissions.get(name)
    }

    /// Get all permissions for a role.
    /// 获取角色的所有权限。
    pub fn get_role_permissions(&self, role: &str) -> HashSet<&str> {
        self.role_permissions
            .get(role)
            .map(|s| s.iter().map(String::as_str).collect())
            .unwrap_or_default()
    }

    /// Get all registered permission names.
    /// 获取所有已注册的权限名称。
    pub fn all_permission_names(&self) -> Vec<&str> {
        self.permissions.keys().map(String::as_str).collect()
    }

    /// Get all registered role names.
    /// 获取所有已注册的角色名称。
    pub fn all_roles(&self) -> Vec<&str> {
        self.role_permissions.keys().map(String::as_str).collect()
    }

    /// Collect the effective permissions for a set of roles.
    /// 收集一组角色的有效权限。
    pub fn effective_permissions(&self, roles: &[String]) -> HashSet<String> {
        let mut perms = HashSet::new();
        for role in roles {
            if let Some(role_perms) = self.role_permissions.get(role) {
                perms.extend(role_perms.iter().cloned());
            }
        }
        perms
    }
}

/// Permission evaluator with caching.
/// 带缓存的权限评估器。
///
/// Evaluates whether a user (identified by roles) has specific permissions.
/// Cache keys are computed from the sorted set of roles to enable reuse.
/// 评估用户(通过角色标识)是否具有特定权限。
/// 缓存键从排序后的角色集合计算,以便复用。
#[derive(Clone)]
pub struct PermissionEvaluator {
    registry: Arc<RwLock<PermissionRegistry>>,
    cache: Arc<RwLock<HashMap<String, bool>>>,
}

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

impl fmt::Debug for PermissionEvaluator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PermissionEvaluator")
            .field("registry", &"<PermissionRegistry>")
            .field("cache", &"<cache>")
            .finish()
    }
}

impl PermissionEvaluator {
    /// Create a new evaluator with the given registry.
    /// 使用给定的注册表创建新的评估器。
    pub fn new(registry: PermissionRegistry) -> Self {
        Self {
            registry: Arc::new(RwLock::new(registry)),
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create from a shared `Arc<RwLock<PermissionRegistry>>`.
    /// 从共享的 `Arc<RwLock<PermissionRegistry>>` 创建。
    pub fn from_shared(registry: Arc<RwLock<PermissionRegistry>>) -> Self {
        Self {
            registry,
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Build a deterministic cache key from the sorted role list and permission.
    /// 从排序后的角色列表和权限构建确定性的缓存键。
    fn cache_key(roles: &[String], permission: &str) -> String {
        let mut sorted = roles.to_vec();
        sorted.sort();
        format!("{}|{}", sorted.join(","), permission)
    }

    /// Check if the given roles include a specific permission.
    /// 检查给定角色是否包含特定权限。
    pub async fn has_permission(&self, user_roles: &[String], permission: &str) -> bool {
        let key = Self::cache_key(user_roles, permission);

        // Check cache first / 先检查缓存
        {
            let cache = self.cache.read().await;
            if let Some(&result) = cache.get(&key) {
                return result;
            }
        }

        // Evaluate / 评估
        let registry = self.registry.read().await;
        let result = registry
            .effective_permissions(user_roles)
            .contains(permission);

        // Store in cache / 存入缓存
        {
            let mut cache = self.cache.write().await;
            cache.insert(key, result);
        }

        result
    }

    /// Check if the given roles include any of the specified permissions.
    /// 检查给定角色是否包含任一指定权限。
    pub async fn has_any_permission(&self, user_roles: &[String], permissions: &[&str]) -> bool {
        for perm in permissions {
            if self.has_permission(user_roles, perm).await {
                return true;
            }
        }
        false
    }

    /// Check if the given roles include all of the specified permissions.
    /// 检查给定角色是否包含所有指定权限。
    pub async fn has_all_permissions(&self, user_roles: &[String], permissions: &[&str]) -> bool {
        for perm in permissions {
            if !self.has_permission(user_roles, perm).await {
                return false;
            }
        }
        true
    }

    /// Invalidate the entire cache.
    /// 使整个缓存失效。
    pub async fn invalidate_cache(&self) {
        self.cache.write().await.clear();
    }

    /// Get a shared reference to the underlying registry.
    /// 获取底层注册表的共享引用。
    pub async fn registry(&self) -> Arc<RwLock<PermissionRegistry>> {
        self.registry.clone()
    }
}

/// A single permission audit entry.
/// 单个权限审计条目。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionAuditEntry {
    /// When the access check occurred.
    /// 访问检查发生的时间。
    pub timestamp: DateTime<Utc>,

    /// The user (or principal) being checked.
    /// 被检查的用户(或主体)。
    pub user: String,

    /// The permission that was evaluated.
    /// 被评估的权限。
    pub permission: String,

    /// Whether the permission was granted.
    /// 权限是否被授予。
    pub granted: bool,

    /// The resource involved (if applicable).
    /// 涉及的资源(如适用)。
    pub resource: Option<String>,
}

impl PermissionAuditEntry {
    /// Create a new audit entry.
    /// 创建新的审计条目。
    pub fn new(
        user: impl Into<String>,
        permission: impl Into<String>,
        granted: bool,
        resource: Option<String>,
    ) -> Self {
        Self {
            timestamp: Utc::now(),
            user: user.into(),
            permission: permission.into(),
            granted,
            resource,
        }
    }
}

/// Trait for permission audit loggers.
/// 权限审计日志器的 trait。
#[async_trait::async_trait]
pub trait PermissionAuditLog: Send + Sync {
    /// Log a permission access check.
    /// 记录权限访问检查。
    async fn log_access(&self, entry: PermissionAuditEntry) -> SecurityResult<()>;
}

/// Permission audit logger backed by tracing.
/// 基于 tracing 的权限审计日志器。
///
/// Outputs audit events via `tracing::info!`.
/// 通过 `tracing::info!` 输出审计事件。
#[derive(Debug, Clone, Default)]
pub struct PermissionAuditLogger;

impl PermissionAuditLogger {
    /// Create a new logger.
    /// 创建新的日志器。
    pub fn new() -> Self {
        Self
    }
}

#[async_trait::async_trait]
impl PermissionAuditLog for PermissionAuditLogger {
    async fn log_access(&self, entry: PermissionAuditEntry) -> SecurityResult<()> {
        let status = if entry.granted { "GRANTED" } else { "DENIED" };
        tracing::info!(
            "[PERM-AUDIT] {} | User: {} | Permission: {} | Resource: {:?}",
            status,
            entry.user,
            entry.permission,
            entry.resource,
        );
        Ok(())
    }
}

/// In-memory permission audit logger that captures entries for inspection.
/// 内存中的权限审计日志器,捕获条目以供检查。
///
/// Useful for testing and debugging.
/// 适用于测试和调试。
#[derive(Debug, Clone)]
pub struct InMemoryPermissionAuditLogger {
    entries: Arc<RwLock<Vec<PermissionAuditEntry>>>,
}

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

impl InMemoryPermissionAuditLogger {
    /// Create a new in-memory audit logger.
    /// 创建新的内存审计日志器。
    pub fn new() -> Self {
        Self {
            entries: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Retrieve all logged entries.
    /// 检索所有记录的条目。
    pub async fn entries(&self) -> Vec<PermissionAuditEntry> {
        self.entries.read().await.clone()
    }

    /// Clear all logged entries.
    /// 清除所有记录的条目。
    pub async fn clear(&self) {
        self.entries.write().await.clear();
    }

    /// Number of entries currently stored.
    /// 当前存储的条目数。
    pub async fn len(&self) -> usize {
        self.entries.read().await.len()
    }

    /// Check if there are no entries.
    /// 检查是否没有条目。
    pub async fn is_empty(&self) -> bool {
        self.entries.read().await.is_empty()
    }
}

#[async_trait::async_trait]
impl PermissionAuditLog for InMemoryPermissionAuditLogger {
    async fn log_access(&self, entry: PermissionAuditEntry) -> SecurityResult<()> {
        self.entries.write().await.push(entry);
        Ok(())
    }
}

// ============================================================================
// Tests / 测试
// ============================================================================

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

    // ── PermissionRegistry / 权限注册表 ──

    #[test]
    fn test_register_permission() {
        let mut reg = PermissionRegistry::new();
        reg.register("user:read", "user", "read", "Read user data");
        assert!(reg.has_permission("user:read"));
        assert!(!reg.has_permission("user:write"));
    }

    #[test]
    fn test_register_permission_def() {
        let mut reg = PermissionRegistry::new();
        let def = PermissionDef::new("doc:write", "doc", "write", "Write document");
        reg.register_def(def);
        assert!(reg.has_permission("doc:write"));
        let fetched = reg.get_permission("doc:write").unwrap();
        assert_eq!(fetched.resource, "doc");
    }

    #[test]
    fn test_grant_and_revoke_role_permission() {
        let mut reg = PermissionRegistry::new();
        reg.register("user:read", "user", "read", "Read");
        reg.register("user:write", "user", "write", "Write");
        reg.grant_role_permission("USER", "user:read");
        reg.grant_role_permission("USER", "user:write");

        let perms = reg.get_role_permissions("USER");
        assert_eq!(perms.len(), 2);
        assert!(reg.revoke_role_permission("USER", "user:write"));
        let perms = reg.get_role_permissions("USER");
        assert_eq!(perms.len(), 1);
        assert!(!reg.revoke_role_permission("USER", "nonexistent"));
    }

    #[test]
    fn test_grant_role_permissions_batch() {
        let mut reg = PermissionRegistry::new();
        reg.register("a:1", "a", "1", "");
        reg.register("a:2", "a", "2", "");
        reg.grant_role_permissions("ADMIN", vec!["a:1", "a:2"]);

        let perms = reg.get_role_permissions("ADMIN");
        assert_eq!(perms.len(), 2);
    }

    #[test]
    fn test_remove_role() {
        let mut reg = PermissionRegistry::new();
        reg.grant_role_permission("TEMP", "x:y");
        assert!(reg.remove_role("TEMP"));
        assert!(!reg.remove_role("TEMP"));
    }

    #[test]
    fn test_effective_permissions() {
        let mut reg = PermissionRegistry::new();
        reg.register("r1", "res", "read", "");
        reg.register("r2", "res", "write", "");
        reg.grant_role_permission("ADMIN", "r1");
        reg.grant_role_permission("USER", "r2");

        let roles = vec!["ADMIN".to_string(), "USER".to_string()];
        let effective = reg.effective_permissions(&roles);
        assert_eq!(effective.len(), 2);
    }

    #[test]
    fn test_effective_permissions_empty_roles() {
        let reg = PermissionRegistry::new();
        let effective = reg.effective_permissions(&[]);
        assert!(effective.is_empty());
    }

    #[test]
    fn test_all_permission_names() {
        let mut reg = PermissionRegistry::new();
        reg.register("a", "x", "y", "");
        reg.register("b", "x", "z", "");
        let names = reg.all_permission_names();
        assert_eq!(names.len(), 2);
    }

    #[test]
    fn test_all_roles() {
        let mut reg = PermissionRegistry::new();
        reg.grant_role_permission("A", "x");
        reg.grant_role_permission("B", "y");
        let roles = reg.all_roles();
        assert_eq!(roles.len(), 2);
    }

    // ── PermissionEvaluator / 权限评估器 ──

    fn make_evaluator() -> PermissionEvaluator {
        let mut reg = PermissionRegistry::new();
        reg.register("user:read", "user", "read", "Read users");
        reg.register("user:write", "user", "write", "Write users");
        reg.register("admin:panel", "admin", "panel", "Admin panel");
        reg.grant_role_permission("USER", "user:read");
        reg.grant_role_permission("ADMIN", "user:read");
        reg.grant_role_permission("ADMIN", "user:write");
        reg.grant_role_permission("ADMIN", "admin:panel");
        PermissionEvaluator::new(reg)
    }

    #[tokio::test]
    async fn test_has_permission_granted() {
        let ev = make_evaluator();
        assert!(ev.has_permission(&["USER".to_string()], "user:read").await);
    }

    #[tokio::test]
    async fn test_has_permission_denied() {
        let ev = make_evaluator();
        assert!(!ev.has_permission(&["USER".to_string()], "user:write").await);
    }

    #[tokio::test]
    async fn test_has_permission_unknown_role() {
        let ev = make_evaluator();
        assert!(!ev.has_permission(&["GUEST".to_string()], "user:read").await);
    }

    #[tokio::test]
    async fn test_has_permission_empty_roles() {
        let ev = make_evaluator();
        assert!(!ev.has_permission(&[], "user:read").await);
    }

    #[tokio::test]
    async fn test_has_any_permission() {
        let ev = make_evaluator();
        assert!(
            ev.has_any_permission(&["USER".to_string()], &["user:write", "user:read"],)
                .await
        );
        assert!(
            !ev.has_any_permission(&["GUEST".to_string()], &["admin:panel", "user:write"],)
                .await
        );
    }

    #[tokio::test]
    async fn test_has_all_permissions() {
        let ev = make_evaluator();
        assert!(
            ev.has_all_permissions(
                &["ADMIN".to_string()],
                &["user:read", "user:write", "admin:panel"],
            )
            .await
        );
        assert!(
            !ev.has_all_permissions(&["USER".to_string()], &["user:read", "user:write"],)
                .await
        );
    }

    #[tokio::test]
    async fn test_has_all_permissions_empty_list() {
        let ev = make_evaluator();
        // Empty permission list: all of nothing is trivially true
        assert!(ev.has_all_permissions(&[], &[]).await);
    }

    #[tokio::test]
    async fn test_cache_is_used() {
        let ev = make_evaluator();
        // First call populates cache
        assert!(ev.has_permission(&["USER".to_string()], "user:read").await);
        // Modify registry — cached result should still be returned
        {
            let reg_arc = ev.registry().await;
            let mut reg = reg_arc.write().await;
            reg.revoke_role_permission("USER", "user:read");
        }
        // Still true from cache
        assert!(ev.has_permission(&["USER".to_string()], "user:read").await);
        // Invalidate cache — now should reflect the change
        ev.invalidate_cache().await;
        assert!(!ev.has_permission(&["USER".to_string()], "user:read").await);
    }

    #[tokio::test]
    async fn test_invalidate_cache() {
        let ev = make_evaluator();
        ev.has_permission(&["USER".to_string()], "user:read").await;
        ev.invalidate_cache().await;
        // Should still work after invalidation (re-evaluates from registry)
        assert!(ev.has_permission(&["USER".to_string()], "user:read").await);
    }

    #[tokio::test]
    async fn test_cache_key_order_independent() {
        let ev = make_evaluator();
        let a = ev.has_permission(&["USER".to_string()], "user:read").await;
        let b = ev
            .has_permission(&["ADMIN".to_string(), "USER".to_string()], "user:read")
            .await;
        assert!(a);
        assert!(b);
    }

    #[test]
    fn test_evaluator_debug() {
        let ev = make_evaluator();
        let debug = format!("{:?}", ev);
        assert!(debug.contains("PermissionEvaluator"));
    }

    // ── PermissionAuditEntry / 权限审计条目 ──

    #[test]
    fn test_audit_entry_new() {
        let entry =
            PermissionAuditEntry::new("alice", "user:read", true, Some("doc/1".to_string()));
        assert_eq!(entry.user, "alice");
        assert!(entry.granted);
        assert_eq!(entry.resource.as_deref(), Some("doc/1"));
    }

    #[test]
    fn test_audit_entry_serialization() {
        let entry = PermissionAuditEntry::new("bob", "user:write", false, None);
        let json = serde_json::to_string(&entry).unwrap();
        let deserialized: PermissionAuditEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.user, "bob");
        assert!(!deserialized.granted);
    }

    // ── PermissionAuditLogger / 权限审计日志器 ──

    #[tokio::test]
    async fn test_tracing_audit_logger_does_not_error() {
        let logger = PermissionAuditLogger::new();
        let entry = PermissionAuditEntry::new("test", "p", true, None);
        assert!(logger.log_access(entry).await.is_ok());
    }

    #[tokio::test]
    async fn test_in_memory_audit_logger() {
        let logger = InMemoryPermissionAuditLogger::new();
        assert!(logger.is_empty().await);

        logger
            .log_access(PermissionAuditEntry::new("alice", "user:read", true, None))
            .await
            .unwrap();
        logger
            .log_access(PermissionAuditEntry::new(
                "bob",
                "user:write",
                false,
                Some("doc".to_string()),
            ))
            .await
            .unwrap();

        assert_eq!(logger.len().await, 2);
        let entries = logger.entries().await;
        assert_eq!(entries[0].user, "alice");
        assert!(entries[0].granted);
        assert_eq!(entries[1].user, "bob");
        assert!(!entries[1].granted);
    }

    #[tokio::test]
    async fn test_in_memory_audit_logger_clear() {
        let logger = InMemoryPermissionAuditLogger::new();
        logger
            .log_access(PermissionAuditEntry::new("u", "p", true, None))
            .await
            .unwrap();
        assert_eq!(logger.len().await, 1);
        logger.clear().await;
        assert!(logger.is_empty().await);
    }
}