0.1.3: upload a vault to a team from the app, detach first; AGPL-3.0

- The Lune window shows Upload on a vault that isn't on a team, for the team
  owner and anyone who manages a vault there. The hub now lets those people
  create vaults too (was owner only); /api/me and the canvas say can_create.
- A vault on its own remote (GitHub, GitLab, Gitea…) must be detached first
  (`lunesync vault detach`, or the Upload sheet's Detach): it shares what's
  waiting one last time, then lives on this device only. Upload refuses
  anything else, and a check-in no longer silently moves a same-named vault
  off its own remote onto the hub.
- Upload logic moved from the CLI into core (lune_core::upload).
- Canvas: "1 person loses access".
- License: AGPL-3.0-only. README and SPEC §8 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 16:44:36 +07:00
parent 584b279496
commit 4ab9d580b8
26 changed files with 1082 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
[package]
name = "lunehub"
version.workspace = true
license.workspace = true
edition = "2024"
[dependencies]

View File

@@ -261,9 +261,10 @@ pub async fn me(State(hub): State<Shared>, headers: HeaderMap) -> ApiResult {
if nudge {
conn.execute("UPDATE devices SET nudge = 0 WHERE id = ?1", [&caller.device_id])?;
}
let can_create = db::can_create_vaults(&conn, caller.account_id, caller.owner)?;
Ok(Json(json!({
"person": caller.person, "owner": caller.owner, "device_label": caller.device_label, "vaults": vaults,
"leaving": leaving, "nudge": nudge
"leaving": leaving, "nudge": nudge, "can_create": can_create
})))
}
@@ -288,9 +289,6 @@ pub struct VaultReq {
pub async fn create_vault(State(hub): State<Shared>, headers: HeaderMap, Json(req): Json<VaultReq>) -> ApiResult {
let caller = authenticate(&hub, &headers)?;
if !caller.owner {
return Err(denied("only the team owner can create vaults"));
}
let name = req.name.trim();
let ok = !name.is_empty()
&& name.len() <= 60
@@ -300,6 +298,9 @@ pub async fn create_vault(State(hub): State<Shared>, headers: HeaderMap, Json(re
return Err(bad("vault names use letters, numbers, spaces, - _ . (up to 60)"));
}
let conn = hub.db.lock().unwrap();
if !db::can_create_vaults(&conn, caller.account_id, caller.owner)? {
return Err(denied("only the team owner and people who manage a vault can create vaults"));
}
if db::vault_id(&conn, name)?.is_some() {
return Err(ApiError(StatusCode::CONFLICT, format!("there's already a vault named {name}")));
}

View File

@@ -220,6 +220,22 @@ pub fn role(conn: &Connection, account_id: i64, vault_id: i64) -> Result<Option<
.optional()?)
}
/// Who may create vaults (and move vaults onto the hub): the team owner, and anyone with Manage on a vault.
pub fn can_create_vaults(conn: &Connection, account_id: i64, owner: bool) -> Result<bool> {
if owner {
return Ok(true);
}
Ok(conn
.query_row(
"SELECT 1 FROM memberships m JOIN vaults v ON v.id = m.vault_id
WHERE m.account_id = ?1 AND m.role = 'manage' AND v.archived IS NULL LIMIT 1",
[account_id],
|_| Ok(()),
)
.optional()?
.is_some())
}
pub fn grant(conn: &Connection, account_id: i64, vault_id: i64, role: &str) -> Result<()> {
conn.execute(
"INSERT INTO memberships (account_id, vault_id, role) VALUES (?1, ?2, ?3)

View File

@@ -222,8 +222,9 @@ pub async fn canvas(State(hub): State<Shared>, headers: HeaderMap) -> ApiResult
.collect()
};
let can_create = db::can_create_vaults(&conn, caller.account_id, caller.owner)?;
Ok(Json(json!({
"me": { "id": format!("a{}", caller.account_id), "person": caller.person, "owner": caller.owner },
"me": { "id": format!("a{}", caller.account_id), "person": caller.person, "owner": caller.owner, "can_create": can_create },
"vaults": vaults, "people": people, "members": members, "pending": pending
})))
}

View File

@@ -621,3 +621,71 @@ fn deleting_a_vault_and_leaving_a_team() {
lune_core::membership::leave_hub(&mut stale, "http://127.0.0.1:9", true).unwrap();
assert!(stale.hubs.is_empty() && stale.vaults.is_empty() && kept.exists());
}
#[test]
fn a_vault_moves_onto_the_hub_only_once_it_is_detached() {
let _turn = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
let tmp = tempfile::TempDir::new().unwrap();
let (_hub, url, owner_link) = start_hub(&tmp.path().join("data"));
let (_, sen_w) = hub::join(&owner_link, "Mac").unwrap();
let mut mac = device(&url, &sen_w, "Mac", &tmp.path().join("mac"));
mac.use_hub_credentials();
let sen = mac.hubs[0].clone();
sen.create_vault("Team").unwrap();
// Creating vaults: the owner, and people who manage a vault; not people who only edit.
let (_, ed_w) = hub::join(&sen.invite("Ed", &[("Team".into(), "edit".into())]).unwrap(), "Ed pc").unwrap();
let ed = as_device(&url, &ed_w);
assert!(!ed.me().unwrap().can_create);
assert!(ed.create_vault("Eds").is_err());
as_device(&url, &sen_w);
sen.grant("Ed", "Team", "manage").unwrap();
let ed = as_device(&url, &ed_w);
assert!(ed.me().unwrap().can_create);
assert_eq!(ed.create_vault("Eds").unwrap().role, "manage");
// A vault on its own remote (a stand-in for GitLab), with history.
let gitlab = tmp.path().join("gitlab.git");
lune_core::git2::Repository::init_bare(&gitlab).unwrap();
let remote_url = format!("file://{}", gitlab.display());
let (cfg, _, err) = vault::add("Brain", &tmp.path().join("mac/Brain"), &remote_url, "Mac").unwrap();
assert_eq!(err, None);
mac.vaults.push(cfg);
let v = open(&mac, "Brain");
write_and_save(&v, "idea.md", "first\n");
v.sync().unwrap();
write_and_save(&v, "idea.md", "second\n");
drop(v);
// Not uploaded while it still syncs elsewhere, and a check-in doesn't move it either.
let sen = as_device(&url, &sen_w);
let err = lune_core::upload::upload(&mut mac, &sen, "Brain", false).unwrap_err().to_string();
assert!(err.contains("Detach it"), "{err}");
sen.create_vault("Brain").unwrap();
check_in(&mut mac);
assert!(mac.find("Brain").unwrap().hub_vault.is_none(), "a vault on its own remote stays put");
// Detaching shares what was waiting there one last time, then it's only on this device.
lune_core::upload::detach(&mut mac, "Brain").unwrap();
let there = lune_core::git2::Repository::open_bare(&gitlab).unwrap();
let tip = there.find_branch("main", lune_core::git2::BranchType::Local).unwrap().get().peel_to_commit().unwrap();
assert_eq!(tip.tree().unwrap().get_name("idea.md").unwrap().to_object(&there).unwrap().as_blob().unwrap().content(), b"second\n");
let v = open(&mac, "Brain");
assert!(v.is_local());
write_and_save(&v, "later.md", "kept here\n");
v.sync().unwrap();
assert_eq!(lune_core::status::of(mac.find("Brain").unwrap(), "Mac").headline, "Only on this device · not shared");
drop(v);
// Uploading joins the same-named team vault, history included; another device gets every note.
let said = lune_core::upload::upload(&mut mac, &sen, "Brain", false).unwrap();
assert!(said.contains("history included"), "{said}");
assert!(mac.find("Brain").unwrap().hub_vault.is_some());
let code = sen.pair().unwrap();
let other_w = hub::login(&url, &code, "Air").unwrap();
let mut other = device(&url, &other_w, "Air", &tmp.path().join("air"));
check_in(&mut other);
let o = open(&other, "Brain");
assert_eq!(std::fs::read(o.root.join("idea.md")).unwrap(), b"second\n");
assert_eq!(std::fs::read(o.root.join("later.md")).unwrap(), b"kept here\n");
}