Skip to main content

link_cli/triplets/
permissions.rs

1//! Permissions on triplets (issues #149 and #152): the same rules as on
2//! doublets, judged on the triplet as a whole.
3//!
4//! The doublets of a triplet are not the subject's business. A rule on the type
5//! `Note` says nothing about the wrappers and pairs that happen to store the
6//! parts of a note, so a [`PermissionsDecorator`] under
7//! [`TripletsOverDoublets`](super::TripletsOverDoublets) would hide them and
8//! refuse to write them. It goes above instead, over the [`Triplets`] store,
9//! and asks the policy only about the outer doublet `(index: type …)`:
10//!
11//! - a triplet the subject may not read does not exist for the subject, and
12//!   neither does anything that is not a triplet;
13//! - creating needs the right to create and to write the type;
14//! - updating needs the right to write the triplet as it is and as it will be;
15//! - deleting needs the right to write every triplet the delete takes along,
16//!   or nothing is deleted;
17//! - a point the subject created, still only its own type, is the subject's:
18//!   that is what a substitution query reserves before it sets the type.
19
20use anyhow::{bail, Result};
21
22use super::{
23    cascade_of, ensure_part, DoubletRole, Triplet, TripletMarkers, TripletQuery, Triplets,
24};
25use crate::error::LinkError;
26use crate::link::Link;
27use crate::permissions::{AccessPolicy, Forbidden, PermissionsDecorator};
28
29impl<L: Triplets, P: AccessPolicy> PermissionsDecorator<L, P> {
30    /// The outer doublet of a triplet the store below holds.
31    fn outer_of(&mut self, index: u32) -> Option<Link> {
32        self.inner_mut().get_link(index)
33    }
34
35    fn triplet_readable(&mut self, triplet: &Triplet) -> bool {
36        self.outer_of(triplet.index)
37            .is_some_and(|outer| self.can_read_triplet(&outer))
38    }
39
40    fn triplet_writable(&mut self, triplet: &Triplet) -> bool {
41        self.outer_of(triplet.index)
42            .is_some_and(|outer| self.can_write_triplet(&outer))
43    }
44}
45
46impl<L: Triplets, P: AccessPolicy> Triplets for PermissionsDecorator<L, P> {
47    fn triplet_markers(&self) -> TripletMarkers {
48        self.inner().triplet_markers()
49    }
50
51    fn role_of(&mut self, id: u32) -> Option<DoubletRole> {
52        let link = self.outer_of(id)?;
53        if !self.can_read_triplet(&link) {
54            return None;
55        }
56        self.inner_mut().role_of(id)
57    }
58
59    fn create_triplet(&mut self, type_id: u32, from: u32, to: u32) -> Result<u32> {
60        if !self.policy_can_create()
61            || (type_id != 0 && !self.policy_can_write(&Link::new(0, type_id, 0)))
62        {
63            return Err(Forbidden::new("create").into());
64        }
65        // A subject that may write a type need not read it, which leaves the
66        // layer below to judge that the type exists.
67        ensure_part(self, from, None, "from")?;
68        ensure_part(self, to, None, "to")?;
69        let index = self.inner_mut().create_triplet(type_id, from, to)?;
70        self.remember(index);
71        Ok(index)
72    }
73
74    fn create_triplet_point(&mut self) -> Result<u32> {
75        if !self.policy_can_create() {
76            return Err(Forbidden::new("create").into());
77        }
78        let index = self.inner_mut().create_triplet_point()?;
79        self.remember(index);
80        Ok(index)
81    }
82
83    fn create_triplet_point_at(&mut self, address: u32) -> Result<()> {
84        if !self.policy_can_create() {
85            return Err(Forbidden::new("create").into());
86        }
87        // A hidden link is not found, yet its address is taken: the point must
88        // not land on it.
89        if self.inner_mut().exists(address) {
90            return Err(LinkError::not_found(address).into());
91        }
92        self.inner_mut().create_triplet_point_at(address)?;
93        self.remember(address);
94        Ok(())
95    }
96
97    fn update_triplet(&mut self, index: u32, type_id: u32, from: u32, to: u32) -> Result<Triplet> {
98        let Some(before) = self.get_triplet(index) else {
99            bail!("Link {index} is not a triplet.");
100        };
101        // Only an empty triplet has itself as its target.
102        let target = if from == 0 && to == 0 { index } else { 0 };
103        if !self.triplet_writable(&before)
104            || !self.can_write_triplet(&Link::new(index, type_id, target))
105        {
106            return Err(Forbidden::new(format!("update {index}")).into());
107        }
108        ensure_part(self, from, Some(index), "from")?;
109        ensure_part(self, to, Some(index), "to")?;
110        self.inner_mut().update_triplet(index, type_id, from, to)
111    }
112
113    fn delete_triplet(&mut self, index: u32) -> Result<Vec<Triplet>> {
114        let Some(first) = self.get_triplet(index) else {
115            bail!("Link {index} is not a triplet.");
116        };
117        for triplet in cascade_of(self.inner_mut(), first) {
118            if !self.triplet_writable(&triplet) {
119                return Err(Forbidden::new(format!("delete {index}")).into());
120            }
121        }
122        let deleted = self.inner_mut().delete_triplet(index)?;
123        for triplet in &deleted {
124            self.forget(triplet.index);
125        }
126        Ok(deleted)
127    }
128
129    fn each_triplet(&mut self, query: TripletQuery) -> Vec<Triplet> {
130        let mut found = self.inner_mut().each_triplet(query);
131        found.retain(|triplet| self.triplet_readable(triplet));
132        found
133    }
134}