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//! - every part needs the right to refer to it (issue #161): without it an
15//!   unreadable part answers as a missing one, after the write check, and a
16//!   part the triplet already holds may stay;
17//! - choosing the address of a new point is a reference too;
18//! - updating needs the right to write the triplet as it is and as it will be;
19//! - deleting needs the right to write every triplet the delete takes along,
20//!   or nothing is deleted;
21//! - a point the subject created, still only its own type, is the subject's:
22//!   that is what a substitution query reserves before it sets the type.
23
24use anyhow::{bail, Result};
25
26use super::{cascade_of, DoubletRole, Triplet, TripletMarkers, TripletQuery, Triplets};
27use crate::error::LinkError;
28use crate::link::Link;
29use crate::permissions::{AccessPolicy, Forbidden, PermissionsDecorator};
30
31impl<L: Triplets, P: AccessPolicy> PermissionsDecorator<L, P> {
32    /// The outer doublet of a triplet the store below holds.
33    fn outer_of(&mut self, index: u32) -> Option<Link> {
34        self.inner_mut().get_link(index)
35    }
36
37    fn triplet_readable(&mut self, triplet: &Triplet) -> bool {
38        self.outer_of(triplet.index)
39            .is_some_and(|outer| self.can_read_triplet(&outer))
40    }
41
42    fn triplet_writable(&mut self, triplet: &Triplet) -> bool {
43        self.outer_of(triplet.index)
44            .is_some_and(|outer| self.can_write_triplet(&outer))
45    }
46
47    /// `from` or `to` must be `0`, the triplet itself, a part `before` holds
48    /// or a triplet the subject may refer to; a hidden one answers as a missing one.
49    fn check_part(&mut self, part: u32, before: Option<&Triplet>, name: &str) -> Result<()> {
50        if part == 0 || before.is_some_and(|before| holds(before, part)) {
51            return Ok(());
52        }
53        if self.reference_is_triplet(part) {
54            return Ok(());
55        }
56        bail!("The {name} part {part} is not a triplet.")
57    }
58}
59
60/// Whether `part` is `triplet` or one of its parts.
61fn holds(triplet: &Triplet, part: u32) -> bool {
62    part == triplet.index || part == triplet.type_id || part == triplet.from || part == triplet.to
63}
64
65impl<L: Triplets, P: AccessPolicy> Triplets for PermissionsDecorator<L, P> {
66    fn triplet_markers(&self) -> TripletMarkers {
67        self.inner().triplet_markers()
68    }
69
70    fn role_of(&mut self, id: u32) -> Option<DoubletRole> {
71        let link = self.outer_of(id)?;
72        if !self.can_read_triplet(&link) {
73            return None;
74        }
75        self.inner_mut().role_of(id)
76    }
77
78    fn reference_is_triplet(&mut self, index: u32) -> bool {
79        self.is_triplet(index) || (self.can_reference(index) && self.inner_mut().is_triplet(index))
80    }
81
82    fn create_triplet(&mut self, type_id: u32, from: u32, to: u32) -> Result<u32> {
83        if !self.policy_can_create()
84            || (type_id != 0 && !self.policy_can_write(&Link::new(0, type_id, 0)))
85        {
86            return Err(Forbidden::new("create").into());
87        }
88        // A subject that may write a type need not read it: the rule that says
89        // so names it. That it exists is left to the layer below.
90        if type_id != 0 && !self.can_reference(type_id) {
91            return Err(LinkError::not_found(type_id).into());
92        }
93        self.check_part(from, None, "from")?;
94        self.check_part(to, None, "to")?;
95        let index = self.inner_mut().create_triplet(type_id, from, to)?;
96        self.remember(index);
97        Ok(index)
98    }
99
100    fn create_triplet_point(&mut self) -> Result<u32> {
101        if !self.policy_can_create() {
102            return Err(Forbidden::new("create").into());
103        }
104        let index = self.inner_mut().create_triplet_point()?;
105        self.remember(index);
106        Ok(index)
107    }
108
109    fn create_triplet_point_at(&mut self, address: u32) -> Result<()> {
110        if !self.policy_can_create() {
111            return Err(Forbidden::new("create").into());
112        }
113        // Choosing an address is a reference: without the right a hidden link
114        // and a free address answer alike.
115        if !self.can_reference(address) {
116            return Err(LinkError::not_found(address).into());
117        }
118        // The subject may refer to this hidden link: the point must not land on it.
119        if self.inner_mut().exists(address) {
120            bail!("Link {address} exists");
121        }
122        self.inner_mut().create_triplet_point_at(address)?;
123        self.remember(address);
124        Ok(())
125    }
126
127    fn update_triplet(&mut self, index: u32, type_id: u32, from: u32, to: u32) -> Result<Triplet> {
128        let Some(before) = self.get_triplet(index) else {
129            bail!("Link {index} is not a triplet.");
130        };
131        // Only an empty triplet has itself as its target.
132        let target = if from == 0 && to == 0 { index } else { 0 };
133        if !self.triplet_writable(&before)
134            || !self.can_write_triplet(&Link::new(index, type_id, target))
135        {
136            return Err(Forbidden::new(format!("update {index}")).into());
137        }
138        if type_id != 0 && !holds(&before, type_id) && !self.can_reference(type_id) {
139            return Err(LinkError::not_found(type_id).into());
140        }
141        self.check_part(from, Some(&before), "from")?;
142        self.check_part(to, Some(&before), "to")?;
143        self.inner_mut().update_triplet(index, type_id, from, to)
144    }
145
146    fn delete_triplet(&mut self, index: u32) -> Result<Vec<Triplet>> {
147        let Some(first) = self.get_triplet(index) else {
148            bail!("Link {index} is not a triplet.");
149        };
150        for triplet in cascade_of(self.inner_mut(), first) {
151            if !self.triplet_writable(&triplet) {
152                return Err(Forbidden::new(format!("delete {index}")).into());
153            }
154        }
155        let deleted = self.inner_mut().delete_triplet(index)?;
156        for triplet in &deleted {
157            self.forget(triplet.index);
158        }
159        Ok(deleted)
160    }
161
162    fn each_triplet(&mut self, query: TripletQuery) -> Vec<Triplet> {
163        let mut found = self.inner_mut().each_triplet(query);
164        found.retain(|triplet| self.triplet_readable(triplet));
165        found
166    }
167}