aboutsummaryrefslogtreecommitdiffstats
path: root/src/access/registry.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/access/registry.rs')
-rw-r--r--src/access/registry.rs39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/access/registry.rs b/src/access/registry.rs
index e806d0c..5a58e1f 100644
--- a/src/access/registry.rs
+++ b/src/access/registry.rs
@@ -18,7 +18,8 @@ use std::collections::BTreeMap;
use serde_json::{Value as JsonValue, json};
use super::{Capability, Caps, Right};
-use crate::document::{ReadDoc, meta, store};
+use crate::document::meta;
+use crate::document::view::{self, Read};
/// `_continuum/users`.
pub(crate) fn users_dir() -> Vec<String> {
@@ -34,13 +35,13 @@ pub(crate) fn user_path(name: &str) -> Vec<String> {
/// Whether the swarm has a registry: until it has, every user a node lets in may do
/// everything but write protocol data (`docs/access.md`, A swarm without a registry).
-pub(crate) fn exists(doc: &impl ReadDoc) -> bool {
- matches!(store::keys_at(doc, &users_dir()), Ok(Some(_)))
+pub(crate) fn exists(doc: &impl Read) -> bool {
+ matches!(view::keys_at(doc, &users_dir()), Ok(Some(_)))
}
/// The registered users' names, sorted.
-pub(crate) fn names(doc: &impl ReadDoc) -> Vec<String> {
- let mut names = store::keys_at(doc, &users_dir())
+pub(crate) fn names(doc: &impl Read) -> Vec<String> {
+ let mut names = view::keys_at(doc, &users_dir())
.ok()
.flatten()
.unwrap_or_default();
@@ -127,11 +128,11 @@ impl User {
}
/// The entry of user `name`, if it has one.
-pub(crate) fn user(doc: &impl ReadDoc, name: &str) -> Option<User> {
- let value = store::read_plain(doc, &user_path(name)).ok()??;
+pub(crate) fn user(doc: &impl Read, name: &str) -> Option<User> {
+ let value = view::read_plain(doc, &user_path(name)).ok()??;
let mut user = value.is_object().then(|| User::from_json(&value))?;
// A damaged one revokes every token.
- user.tokens_not_before = match store::read_plain(doc, &logouts_path(name)) {
+ user.tokens_not_before = match view::read_plain(doc, &logouts_path(name)) {
Ok(None) => 0,
Ok(Some(at)) => at.as_u64().unwrap_or(u64::MAX),
Err(_) => u64::MAX,
@@ -155,15 +156,15 @@ pub(crate) fn session_path(name: &str, session: &str) -> Vec<String> {
}
/// Whether user `name`'s session `session` was ended (`session_path`).
-pub(crate) fn session_ended(doc: &impl ReadDoc, name: &str, session: &str) -> bool {
- store::read_path(doc, &session_path(name, session))
+pub(crate) fn session_ended(doc: &impl Read, name: &str, session: &str) -> bool {
+ view::read_path(doc, &session_path(name, session))
.ok()
.flatten()
.is_some()
}
/// The users who may manage users: enabled, with `write` on `_continuum/users`.
-pub(crate) fn managers(doc: &impl ReadDoc) -> Vec<String> {
+pub(crate) fn managers(doc: &impl Read) -> Vec<String> {
names(doc)
.into_iter()
.filter(|name| caps_of(doc, name).allows(Right::Write, &users_dir()))
@@ -171,7 +172,7 @@ pub(crate) fn managers(doc: &impl ReadDoc) -> Vec<String> {
}
/// What user `name` may do: none if it isn't registered (or is disabled).
-pub(crate) fn caps_of(doc: &impl ReadDoc, name: &str) -> Caps {
+pub(crate) fn caps_of(doc: &impl Read, name: &str) -> Caps {
user(doc, name).map(|user| user.caps()).unwrap_or_default()
}
@@ -289,7 +290,7 @@ mod tests {
],
);
change(&mut doc, |tx| {
- store::write_path(tx, &user_path("alice"), &entry).unwrap()
+ view::write_path(tx, &user_path("alice"), &entry).unwrap()
});
assert!(exists(&doc));
assert_eq!(super::names(&doc), vec!["alice"]);
@@ -308,7 +309,7 @@ mod tests {
let mut entry = entry("x", 0, &[(vec![], &[Right::Read])]);
entry["disabled"] = json!(true);
change(&mut doc, |tx| {
- store::write_path(tx, &user_path("carol"), &entry).unwrap()
+ view::write_path(tx, &user_path("carol"), &entry).unwrap()
});
assert!(user(&doc, "carol").is_some());
assert!(caps_of(&doc, "carol").is_empty());
@@ -319,7 +320,7 @@ mod tests {
"disabled": "no"
});
change(&mut doc, |tx| {
- store::write_path(tx, &user_path("dave"), &damaged).unwrap()
+ view::write_path(tx, &user_path("dave"), &damaged).unwrap()
});
let dave = user(&doc, "dave").unwrap();
assert_eq!(dave.caps.len(), 1);
@@ -374,14 +375,14 @@ mod tests {
for (name, caps) in [("alice", all_caps()), ("bob", vec![(Vec::new(), DATA)])] {
let entry = entry("x", 0, &caps);
change(&mut doc, |tx| {
- store::write_path(tx, &user_path(name), &entry).unwrap()
+ view::write_path(tx, &user_path(name), &entry).unwrap()
});
}
assert_eq!(managers(&doc), vec!["alice"]);
let mut disabled = user_path("alice");
disabled.push("disabled".to_owned());
change(&mut doc, |tx| {
- store::write_path(tx, &disabled, &json!(true)).unwrap()
+ view::write_path(tx, &disabled, &json!(true)).unwrap()
});
assert!(managers(&doc).is_empty());
}
@@ -391,14 +392,14 @@ mod tests {
let mut ours = Document::new();
let entry = entry("x", 0, &[]);
change(&mut ours, |tx| {
- store::write_path(tx, &user_path("erin"), &entry).unwrap()
+ view::write_path(tx, &user_path("erin"), &entry).unwrap()
});
let mut theirs = ours.fork();
for (doc, at) in [(&mut ours, "notes"), (&mut theirs, "logs")] {
let mut cap_path = user_path("erin");
cap_path.extend(["caps".to_owned(), new_id()]);
let cap = capability("x", 0, &path(at), &[Right::Read]);
- change(doc, |tx| store::write_path(tx, &cap_path, &cap).unwrap());
+ change(doc, |tx| view::write_path(tx, &cap_path, &cap).unwrap());
}
ours.merge(&mut theirs).unwrap();
let caps = caps_of(&ours, "erin");