use crate::{Answer, Grant, Grantee, KindSet, Party, TreePath};
#[must_use]
pub fn code_grants(workspace: &str, author: &Grantee, trusted: bool) -> Vec<Grant> {
let grant = |suffix: &str, kinds: KindSet, answer: Answer| {
Grant::new(
format!("code-author:{}:{suffix}", grantee_key(author)),
author.clone(),
TreePath::workspace(workspace),
kinds,
answer,
author.clone(),
)
};
if trusted {
vec![
grant("content", KindSet::READ_UNDOABLE, Answer::Allow),
grant("outside", KindSet::OUTSIDE, Answer::Ask),
]
} else {
vec![grant("untrusted", KindSet::ALL, Answer::Ask)]
}
}
#[must_use]
pub fn code_party<'g>(author: &Grantee, own: &'g [Grant], workspace: &'g [Grant]) -> Party<'g> {
let named: Vec<&'g Grant> = workspace
.iter()
.filter(|grant| &grant.grantee == author)
.collect();
Party::new(vec![author.clone()], own)
.with_grants(named.iter().copied())
.held_off_by(named)
}
fn grantee_key(grantee: &Grantee) -> String {
match grantee {
Grantee::Member(crate::MemberId::Person(id)) => id.as_str().to_owned(),
Grantee::Member(crate::MemberId::Agent { id, person }) => {
format!(
"agent:{}",
crate::agent_answers::agent_key(id, person.as_str())
)
}
other => other.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ActionKind, Authority, Context};
const WORKSPACE: &str = "workspace";
fn node(app: &str) -> TreePath {
TreePath::workspace(WORKSPACE)
.app(app)
.kind("k")
.action("a")
}
fn answer(party: &Party<'_>, at: &TreePath, kind: ActionKind) -> Answer {
Authority::default()
.bounded_by(party.clone())
.decide(at, kind, &Context::at(0))
.parts[1]
.answer
}
tilo_test_env::scenario! {
fn authored_code_asks_by_how_far_it_is_trusted;
phase trusted_code_asks_only_before_reaching_outside;
{
let author = Grantee::agent("helper", "owner");
let own = code_grants(WORKSPACE, &author, true);
let party = code_party(&author, &own, &[]);
assert_eq!(
answer(&party, &node("x"), ActionKind::Undoable),
Answer::Allow
);
assert_eq!(answer(&party, &node("x"), ActionKind::Outside), Answer::Ask);
}
phase untrusted_code_asks_for_everything;
{
let author = Grantee::agent("helper", "owner");
let own = code_grants(WORKSPACE, &author, false);
let party = code_party(&author, &own, &[]);
for kind in [ActionKind::Undoable, ActionKind::Outside] {
assert_eq!(answer(&party, &node("so.tilo.notes"), kind), Answer::Ask);
}
}
phase the_workspaces_denial_of_an_author_holds;
{
let author = Grantee::agent("helper", "owner");
let own = code_grants(WORKSPACE, &author, true);
let workspace = [Grant::new(
"deny-notes",
author.clone(),
TreePath::workspace(WORKSPACE),
KindSet::ALL,
Answer::Deny,
Grantee::person("owner"),
)];
let party = code_party(&author, &own, &workspace);
assert_eq!(
answer(&party, &node("so.tilo.notes"), ActionKind::Read),
Answer::Deny
);
}
}
}