acls-rs 0.2.1

Algebraically-correct permissions system with RBAC, ABAC, and temporal support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Composite permissions: sets of atomic permissions with algebraic structure.

use super::atomic::AtomicPermission;
use crate::algebra::{JoinSemilattice, MeetSemilattice, Monoid, Semigroup};
use std::collections::BTreeSet;
use std::fmt;
use std::iter::FromIterator;
use std::ops::{BitAnd, BitOr, Sub};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A set of atomic permissions forming a lattice structure.
///
/// `PermissionSet` is the primary building block for permission systems. It implements:
/// - [`Semigroup`] via union
/// - [`Monoid`] with empty set as identity
/// - [`MeetSemilattice`] with intersection as meet
/// - [`JoinSemilattice`] with union as join
/// - `Lattice` (via blanket impl)
///
/// The partial order is subset inclusion: `a ≤ b` iff `a ⊆ b` iff `a` is more restrictive than `b`.
///
/// # Examples
///
/// ```
/// use acls_rs::permission::{AtomicPermission, PermissionSet};
/// use acls_rs::algebra::{Monoid, Semigroup, MeetSemilattice, JoinSemilattice};
///
/// let perms1 = PermissionSet::from([
///     AtomicPermission::new("file", "read"),
/// ]);
/// let perms2 = PermissionSet::from([
///     AtomicPermission::new("file", "write"),
/// ]);
///
/// // Semigroup: union
/// let combined = perms1.clone().combine(perms2.clone());
/// assert_eq!(combined.len(), 2);
///
/// // Monoid: identity is empty
/// let empty = PermissionSet::identity();
/// assert!(empty.is_empty());
///
/// // Meet: intersection (most restrictive)
/// let intersection = perms1.clone().meet(perms2.clone());
/// assert!(intersection.is_empty());
///
/// // Join: union (least restrictive)
/// let union = perms1.join(perms2);
/// assert_eq!(union.len(), 2);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PermissionSet(pub(crate) BTreeSet<AtomicPermission>);

impl PermissionSet {
    /// Create a new empty permission set.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::PermissionSet;
    ///
    /// let perms = PermissionSet::new();
    /// assert!(perms.is_empty());
    /// ```
    pub fn new() -> Self {
        Self(BTreeSet::new())
    }

    /// Check if the permission set is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::PermissionSet;
    ///
    /// let perms = PermissionSet::new();
    /// assert!(perms.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get the number of permissions in the set.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let perms = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    ///     AtomicPermission::new("file", "write"),
    /// ]);
    /// assert_eq!(perms.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the set contains a specific permission.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let perms = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    /// ]);
    ///
    /// assert!(perms.contains(&AtomicPermission::new("file", "read")));
    /// assert!(!perms.contains(&AtomicPermission::new("file", "write")));
    /// ```
    pub fn contains(&self, perm: &AtomicPermission) -> bool {
        self.0.contains(perm)
    }

    /// Check if this set is a superset of another.
    ///
    /// In permission terms: this set grants at least all permissions in `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let all = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    ///     AtomicPermission::new("file", "write"),
    /// ]);
    /// let some = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    /// ]);
    ///
    /// assert!(all.is_superset_of(&some));
    /// assert!(!some.is_superset_of(&all));
    /// ```
    pub fn is_superset_of(&self, other: &Self) -> bool {
        self.0.is_superset(&other.0)
    }

    /// Check if this set is a subset of another.
    ///
    /// In permission terms: `other` grants at least all permissions in this set.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let some = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    /// ]);
    /// let all = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    ///     AtomicPermission::new("file", "write"),
    /// ]);
    ///
    /// assert!(some.is_subset_of(&all));
    /// assert!(!all.is_subset_of(&some));
    /// ```
    pub fn is_subset_of(&self, other: &Self) -> bool {
        self.0.is_subset(&other.0)
    }

    /// Check if this set has no permissions in common with another.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let read = PermissionSet::from([AtomicPermission::new("file", "read")]);
    /// let write = PermissionSet::from([AtomicPermission::new("file", "write")]);
    ///
    /// assert!(read.is_disjoint(&write));
    /// ```
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.0.is_disjoint(&other.0)
    }

    /// Insert a permission into the set. Returns `true` if the permission was not already present.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let mut perms = PermissionSet::new();
    /// assert!(perms.insert(AtomicPermission::new("file", "read")));
    /// assert!(!perms.insert(AtomicPermission::new("file", "read"))); // Already present
    /// assert_eq!(perms.len(), 1);
    /// ```
    pub fn insert(&mut self, perm: AtomicPermission) -> bool {
        self.0.insert(perm)
    }

    /// Remove a permission from the set. Returns `true` if the permission was present.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let mut perms = PermissionSet::from([AtomicPermission::new("file", "read")]);
    /// assert!(perms.remove(&AtomicPermission::new("file", "read")));
    /// assert!(!perms.remove(&AtomicPermission::new("file", "write"))); // Not present
    /// assert!(perms.is_empty());
    /// ```
    pub fn remove(&mut self, perm: &AtomicPermission) -> bool {
        self.0.remove(perm)
    }

    /// Iterate over the permissions in the set.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let perms = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    /// ]);
    ///
    /// for perm in perms.iter() {
    ///     println!("{}", perm);
    /// }
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &AtomicPermission> {
        self.0.iter()
    }

    /// Compute the set difference (permissions in self but not in other).
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let all = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    ///     AtomicPermission::new("file", "write"),
    /// ]);
    /// let read_only = PermissionSet::from([
    ///     AtomicPermission::new("file", "read"),
    /// ]);
    ///
    /// let diff = all.difference(&read_only);
    /// assert_eq!(diff.len(), 1);
    /// assert!(diff.contains(&AtomicPermission::new("file", "write")));
    /// ```
    pub fn difference(&self, other: &Self) -> Self {
        Self(self.0.difference(&other.0).cloned().collect())
    }

    /// Compute the symmetric difference (permissions in either set but not both).
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let perms1 = PermissionSet::from([AtomicPermission::new("file", "read")]);
    /// let perms2 = PermissionSet::from([AtomicPermission::new("file", "write")]);
    ///
    /// let sym_diff = perms1.symmetric_difference(&perms2);
    /// assert_eq!(sym_diff.len(), 2);
    /// ```
    pub fn symmetric_difference(&self, other: &Self) -> Self {
        Self(self.0.symmetric_difference(&other.0).cloned().collect())
    }

    /// Create a builder for constructing permission sets.
    ///
    /// # Examples
    ///
    /// ```
    /// use acls_rs::permission::{AtomicPermission, PermissionSet};
    ///
    /// let perms = PermissionSet::builder()
    ///     .insert(AtomicPermission::new("file", "read"))
    ///     .insert(AtomicPermission::new("file", "write"))
    ///     .build();
    ///
    /// assert_eq!(perms.len(), 2);
    /// ```
    pub fn builder() -> PermissionSetBuilder {
        PermissionSetBuilder::new()
    }
}

/// Builder for constructing permission sets.
#[derive(Default)]
pub struct PermissionSetBuilder {
    permissions: BTreeSet<AtomicPermission>,
}

impl PermissionSetBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a permission into the set.
    pub fn insert(mut self, perm: AtomicPermission) -> Self {
        self.permissions.insert(perm);
        self
    }

    /// Build the permission set.
    pub fn build(self) -> PermissionSet {
        PermissionSet(self.permissions)
    }
}

// Implement Default
impl Default for PermissionSet {
    fn default() -> Self {
        Self::new()
    }
}

// Implement Semigroup via union
impl Semigroup for PermissionSet {
    #[inline]
    fn combine(self, other: Self) -> Self {
        Self(self.0.union(&other.0).cloned().collect())
    }
}

// Implement Monoid with empty set as identity
impl Monoid for PermissionSet {
    #[inline]
    fn identity() -> Self {
        Self::new()
    }
}

// Implement PartialOrd via subset relation
impl PartialOrd for PermissionSet {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        use std::cmp::Ordering;

        if self == other {
            Some(Ordering::Equal)
        } else if self.is_subset_of(other) {
            Some(Ordering::Less)
        } else if self.is_superset_of(other) {
            Some(Ordering::Greater)
        } else {
            None // Incomparable (neither subset nor superset)
        }
    }
}

// Implement MeetSemilattice with intersection as meet
impl MeetSemilattice for PermissionSet {
    #[inline]
    fn meet(self, other: Self) -> Self {
        Self(self.0.intersection(&other.0).cloned().collect())
    }
}

// Implement JoinSemilattice with union as join
impl JoinSemilattice for PermissionSet {
    #[inline]
    fn join(self, other: Self) -> Self {
        self.combine(other) // Reuse semigroup combine
    }
}

// Operator overloading: & for meet
impl BitAnd for PermissionSet {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.meet(rhs)
    }
}

// Operator overloading: | for join
impl BitOr for PermissionSet {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        self.join(rhs)
    }
}

// Operator overloading: - for difference
impl Sub for PermissionSet {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        self.difference(&rhs)
    }
}

// Implement From conversions
impl From<BTreeSet<AtomicPermission>> for PermissionSet {
    fn from(set: BTreeSet<AtomicPermission>) -> Self {
        Self(set)
    }
}

impl From<Vec<AtomicPermission>> for PermissionSet {
    fn from(vec: Vec<AtomicPermission>) -> Self {
        Self(vec.into_iter().collect())
    }
}

impl<const N: usize> From<[AtomicPermission; N]> for PermissionSet {
    fn from(arr: [AtomicPermission; N]) -> Self {
        Self(arr.into_iter().collect())
    }
}

// Implement FromIterator
impl FromIterator<AtomicPermission> for PermissionSet {
    fn from_iter<T: IntoIterator<Item = AtomicPermission>>(iter: T) -> Self {
        Self(iter.into_iter().collect())
    }
}

// Implement IntoIterator
impl IntoIterator for PermissionSet {
    type Item = AtomicPermission;
    type IntoIter = std::collections::btree_set::IntoIter<AtomicPermission>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

// Implement Extend
impl Extend<AtomicPermission> for PermissionSet {
    fn extend<T: IntoIterator<Item = AtomicPermission>>(&mut self, iter: T) {
        self.0.extend(iter);
    }
}

// Implement Display
impl fmt::Display for PermissionSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        for (i, perm) in self.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", perm)?;
        }
        write!(f, "}}")
    }
}

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

    fn perm(ns: &str, action: &str) -> AtomicPermission {
        AtomicPermission::new(ns, action)
    }

    #[test]
    fn test_new() {
        let perms = PermissionSet::new();
        assert!(perms.is_empty());
        assert_eq!(perms.len(), 0);
    }

    #[test]
    fn test_from_array() {
        let perms = PermissionSet::from([perm("file", "read"), perm("file", "write")]);

        assert_eq!(perms.len(), 2);
        assert!(perms.contains(&perm("file", "read")));
    }

    #[test]
    fn test_semigroup_combine() {
        let p1 = PermissionSet::from([perm("file", "read")]);
        let p2 = PermissionSet::from([perm("file", "write")]);

        let combined = p1.combine(p2);
        assert_eq!(combined.len(), 2);
    }

    #[test]
    fn test_monoid_identity() {
        let p = PermissionSet::from([perm("file", "read")]);
        let e = PermissionSet::identity();

        assert_eq!(p.clone().combine(e.clone()), p);
        assert_eq!(e.combine(p.clone()), p);
    }

    #[test]
    fn test_meet() {
        let p1 = PermissionSet::from([perm("file", "read"), perm("file", "write")]);
        let p2 = PermissionSet::from([perm("file", "read")]);

        let meet = p1.meet(p2);
        assert_eq!(meet.len(), 1);
        assert!(meet.contains(&perm("file", "read")));
    }

    #[test]
    fn test_join() {
        let p1 = PermissionSet::from([perm("file", "read")]);
        let p2 = PermissionSet::from([perm("file", "write")]);

        let join = p1.join(p2);
        assert_eq!(join.len(), 2);
    }

    #[test]
    fn test_partial_ord() {
        let subset = PermissionSet::from([perm("file", "read")]);
        let superset = PermissionSet::from([perm("file", "read"), perm("file", "write")]);

        assert!(subset < superset);
        assert!(superset > subset);
    }

    #[test]
    fn test_operators() {
        let p1 = PermissionSet::from([perm("file", "read"), perm("file", "write")]);
        let p2 = PermissionSet::from([perm("file", "read")]);

        // Meet (intersection)
        let meet = p1.clone() & p2.clone();
        assert_eq!(meet.len(), 1);

        // Join (union)
        let join = p1.clone() | p2.clone();
        assert_eq!(join.len(), 2);

        // Difference
        let diff = p1 - p2;
        assert_eq!(diff.len(), 1);
        assert!(diff.contains(&perm("file", "write")));
    }

    #[test]
    fn test_display() {
        let perms = PermissionSet::from([perm("file", "read")]);
        let s = perms.to_string();
        assert!(s.contains("file:read"));
    }
}