backbone-core 3.0.2

Backbone Framework Core - Foundation for generic CRUD system
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
//! Domain policy — enforces pure business rules that govern whether CRUD
//! operations are valid for a given entity state.
//!
//! `DomainPolicy<E>` is intentionally free of authentication context.
//! It answers questions like "is this entity in a state that allows deletion?"
//! or "is this transition valid?".  Identity-based rules live in
//! `backbone-auth::resource_policy::ResourcePolicy<E>`.
//!
//! Policies are composable via `AllOfPolicy` / `AnyOfPolicy`.
//!
//! # Example
//!
//! ```rust,ignore
//! use backbone_core::policy::{DomainPolicy, PolicyDecision};
//!
//! struct OrderCancelPolicy;
//!
//! #[async_trait::async_trait]
//! impl DomainPolicy<Order> for OrderCancelPolicy {
//!     async fn can_delete(&self, entity: &Order) -> PolicyDecision {
//!         if entity.status == OrderStatus::Delivered {
//!             Err("cannot cancel a delivered order".into())
//!         } else {
//!             Ok(true)
//!         }
//!     }
//! }
//! ```

use async_trait::async_trait;
use std::marker::PhantomData;
use std::sync::Arc;

/// The result of a policy evaluation.
///
/// - `Ok(true)`  — operation is **permitted**.
/// - `Ok(false)` — operation is **denied** (soft deny, no message).
/// - `Err(msg)`  — operation is **denied** with a human-readable reason.
pub type PolicyDecision = Result<bool, String>;

// ─── PolicyContext (kept for compatibility / optional use in custom policies) ─

/// Contextual information available to custom policies that need it.
///
/// `DomainPolicy` methods do NOT take `PolicyContext` — this struct is
/// available for custom policy implementations that need richer context
/// beyond the entity state (e.g. tenant quota checks).
#[derive(Debug, Clone, Default)]
pub struct PolicyContext {
    /// The authenticated user's ID, if any.
    pub user_id: Option<String>,
    /// Roles held by the current principal.
    pub roles: Vec<String>,
    /// Permissions held by the current principal.
    pub permissions: Vec<String>,
    /// Arbitrary key-value metadata (tenant id, feature flags, etc.).
    pub metadata: std::collections::HashMap<String, String>,
}

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

    pub fn with_user(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    pub fn with_role(mut self, role: impl Into<String>) -> Self {
        self.roles.push(role.into());
        self
    }

    pub fn with_permission(mut self, perm: impl Into<String>) -> Self {
        self.permissions.push(perm.into());
        self
    }

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

    pub fn has_permission(&self, perm: &str) -> bool {
        self.permissions.iter().any(|p| p == perm)
    }
}

// ─── PolicyOutcome (kept for compatibility) ───────────────────────────────────

/// Legacy outcome type — retained for code that uses it directly.
/// Prefer `PolicyDecision` (`Result<bool, String>`) in new code.
#[derive(Debug, Clone, PartialEq)]
pub enum PolicyOutcome {
    /// The operation is allowed.
    Permit,
    /// The operation is denied.  The string is a human-readable reason.
    Deny(String),
}

impl PolicyOutcome {
    pub fn is_permit(&self) -> bool {
        matches!(self, PolicyOutcome::Permit)
    }

    pub fn is_deny(&self) -> bool {
        matches!(self, PolicyOutcome::Deny(_))
    }

    pub fn reason(&self) -> Option<&str> {
        match self {
            PolicyOutcome::Deny(reason) => Some(reason.as_str()),
            PolicyOutcome::Permit => None,
        }
    }

    pub fn into_decision(self) -> PolicyDecision {
        match self {
            PolicyOutcome::Permit => Ok(true),
            PolicyOutcome::Deny(reason) => Err(reason),
        }
    }
}

// ─── DomainPolicy ─────────────────────────────────────────────────────────────

/// Core domain policy trait — governs whether CRUD operations are valid
/// for a given entity `E` based on **entity state alone** (no auth context).
///
/// All methods default to `Ok(true)` (permit).  Override only the methods
/// relevant to the entity's domain invariants.
#[async_trait]
pub trait DomainPolicy<E: Send + Sync + 'static>: Send + Sync {
    /// Can a new entity in the given state be persisted?
    async fn can_create(&self, _entity: &E) -> PolicyDecision {
        Ok(true)
    }

    /// Can the entity transition from `current` to `updated` state?
    async fn can_update(&self, _current: &E, _updated: &E) -> PolicyDecision {
        Ok(true)
    }

    /// Can the entity be soft-deleted?
    async fn can_delete(&self, _entity: &E) -> PolicyDecision {
        Ok(true)
    }

    /// Can a soft-deleted entity be restored?
    async fn can_restore(&self, _entity: &E) -> PolicyDecision {
        Ok(true)
    }

    /// Convenience: enforce `can_create` and map to `Ok(())` or `Err(reason)`.
    async fn enforce_create(&self, entity: &E) -> Result<(), String> {
        self.can_create(entity).await?.then_some(()).ok_or_else(|| "create denied".into())
    }

    /// Convenience: enforce `can_delete` and map to `Ok(())` or `Err(reason)`.
    async fn enforce_delete(&self, entity: &E) -> Result<(), String> {
        self.can_delete(entity).await?.then_some(()).ok_or_else(|| "delete denied".into())
    }
}

// ─── Built-in implementations ───────────────────────────────────────────────

/// Permits every operation — useful as the generated default.
/// Replace with a real policy in your custom decorator.
pub struct PermitAllPolicy<E> {
    _phantom: PhantomData<E>,
}

impl<E> PermitAllPolicy<E> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<E> Default for PermitAllPolicy<E> {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> DomainPolicy<E> for PermitAllPolicy<E> {
    // All methods use the `Ok(true)` defaults — no override needed.
}

/// Denies every operation — useful for protecting deprecated endpoints.
pub struct DenyAllPolicy<E> {
    reason: String,
    _phantom: PhantomData<E>,
}

impl<E> DenyAllPolicy<E> {
    pub fn new(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> DomainPolicy<E> for DenyAllPolicy<E> {
    async fn can_create(&self, _entity: &E) -> PolicyDecision {
        Err(self.reason.clone())
    }
    async fn can_update(&self, _current: &E, _updated: &E) -> PolicyDecision {
        Err(self.reason.clone())
    }
    async fn can_delete(&self, _entity: &E) -> PolicyDecision {
        Err(self.reason.clone())
    }
    async fn can_restore(&self, _entity: &E) -> PolicyDecision {
        Err(self.reason.clone())
    }
}

/// Combines multiple policies with AND semantics:
/// all policies must permit an operation.
pub struct AllOfPolicy<E> {
    policies: Vec<Arc<dyn DomainPolicy<E>>>,
}

impl<E: Send + Sync + 'static> AllOfPolicy<E> {
    pub fn new(policies: Vec<Arc<dyn DomainPolicy<E>>>) -> Self {
        Self { policies }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> DomainPolicy<E> for AllOfPolicy<E> {
    async fn can_create(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            match p.can_create(entity).await {
                Ok(true) => {}
                other => return other,
            }
        }
        Ok(true)
    }

    async fn can_update(&self, current: &E, updated: &E) -> PolicyDecision {
        for p in &self.policies {
            match p.can_update(current, updated).await {
                Ok(true) => {}
                other => return other,
            }
        }
        Ok(true)
    }

    async fn can_delete(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            match p.can_delete(entity).await {
                Ok(true) => {}
                other => return other,
            }
        }
        Ok(true)
    }

    async fn can_restore(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            match p.can_restore(entity).await {
                Ok(true) => {}
                other => return other,
            }
        }
        Ok(true)
    }
}

/// Combines multiple policies with OR semantics:
/// any policy permitting is sufficient.
pub struct AnyOfPolicy<E> {
    policies: Vec<Arc<dyn DomainPolicy<E>>>,
    deny_reason: String,
}

impl<E: Send + Sync + 'static> AnyOfPolicy<E> {
    pub fn new(policies: Vec<Arc<dyn DomainPolicy<E>>>, deny_reason: impl Into<String>) -> Self {
        Self {
            policies,
            deny_reason: deny_reason.into(),
        }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> DomainPolicy<E> for AnyOfPolicy<E> {
    async fn can_create(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            if matches!(p.can_create(entity).await, Ok(true)) {
                return Ok(true);
            }
        }
        Err(self.deny_reason.clone())
    }

    async fn can_update(&self, current: &E, updated: &E) -> PolicyDecision {
        for p in &self.policies {
            if matches!(p.can_update(current, updated).await, Ok(true)) {
                return Ok(true);
            }
        }
        Err(self.deny_reason.clone())
    }

    async fn can_delete(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            if matches!(p.can_delete(entity).await, Ok(true)) {
                return Ok(true);
            }
        }
        Err(self.deny_reason.clone())
    }

    async fn can_restore(&self, entity: &E) -> PolicyDecision {
        for p in &self.policies {
            if matches!(p.can_restore(entity).await, Ok(true)) {
                return Ok(true);
            }
        }
        Err(self.deny_reason.clone())
    }
}

/// Inverts a policy.
pub struct NotPolicy<E> {
    inner: Arc<dyn DomainPolicy<E>>,
    deny_reason: String,
}

impl<E: Send + Sync + 'static> NotPolicy<E> {
    pub fn new(inner: Arc<dyn DomainPolicy<E>>, deny_reason: impl Into<String>) -> Self {
        Self {
            inner,
            deny_reason: deny_reason.into(),
        }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> DomainPolicy<E> for NotPolicy<E> {
    async fn can_create(&self, entity: &E) -> PolicyDecision {
        match self.inner.can_create(entity).await {
            Ok(true) => Err(self.deny_reason.clone()),
            _ => Ok(true),
        }
    }

    async fn can_delete(&self, entity: &E) -> PolicyDecision {
        match self.inner.can_delete(entity).await {
            Ok(true) => Err(self.deny_reason.clone()),
            _ => Ok(true),
        }
    }

    async fn can_update(&self, current: &E, updated: &E) -> PolicyDecision {
        match self.inner.can_update(current, updated).await {
            Ok(true) => Err(self.deny_reason.clone()),
            _ => Ok(true),
        }
    }

    async fn can_restore(&self, entity: &E) -> PolicyDecision {
        match self.inner.can_restore(entity).await {
            Ok(true) => Err(self.deny_reason.clone()),
            _ => Ok(true),
        }
    }
}

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

    #[derive(Debug)]
    struct FakeEntity {
        owner_id: String,
        is_locked: bool,
    }

    struct LockedPolicy;

    #[async_trait]
    impl DomainPolicy<FakeEntity> for LockedPolicy {
        async fn can_delete(&self, entity: &FakeEntity) -> PolicyDecision {
            if entity.is_locked {
                Err("entity is locked".into())
            } else {
                Ok(true)
            }
        }
    }

    #[tokio::test]
    async fn permit_all_always_permits() {
        let policy = PermitAllPolicy::<FakeEntity>::new();
        let entity = FakeEntity {
            owner_id: "u1".into(),
            is_locked: false,
        };
        assert!(matches!(policy.can_create(&entity).await, Ok(true)));
        assert!(matches!(policy.can_delete(&entity).await, Ok(true)));
    }

    #[tokio::test]
    async fn deny_all_always_denies_with_reason() {
        let policy = DenyAllPolicy::<FakeEntity>::new("deprecated");
        let entity = FakeEntity {
            owner_id: "u1".into(),
            is_locked: false,
        };
        assert!(policy.can_create(&entity).await.is_err());
        assert!(policy.can_delete(&entity).await.is_err());
    }

    #[tokio::test]
    async fn locked_policy_denies_delete_for_locked_entity() {
        let policy = LockedPolicy;
        let locked = FakeEntity {
            owner_id: "u1".into(),
            is_locked: true,
        };
        let unlocked = FakeEntity {
            owner_id: "u1".into(),
            is_locked: false,
        };
        assert!(policy.can_delete(&locked).await.is_err());
        assert!(matches!(policy.can_delete(&unlocked).await, Ok(true)));
    }

    #[tokio::test]
    async fn all_of_denies_when_any_denies() {
        let entity = FakeEntity {
            owner_id: "u1".into(),
            is_locked: true,
        };
        let policy: AllOfPolicy<FakeEntity> = AllOfPolicy::new(vec![
            Arc::new(PermitAllPolicy::new()),
            Arc::new(LockedPolicy),
        ]);
        assert!(policy.can_delete(&entity).await.is_err());
    }

    #[tokio::test]
    async fn any_of_permits_when_one_permits() {
        let entity = FakeEntity {
            owner_id: "u1".into(),
            is_locked: false,
        };
        let policy: AnyOfPolicy<FakeEntity> = AnyOfPolicy::new(
            vec![Arc::new(LockedPolicy)],
            "all policies denied",
        );
        assert!(matches!(policy.can_delete(&entity).await, Ok(true)));
    }
}