Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src-tauri/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,22 +595,28 @@ async fn edit_profile(Extension(t): Extension<Ticket>, Path(id): Path<String>, J
apply_refresh_rate(&mut stored.config, hz)
.map_err(|e| err(StatusCode::BAD_REQUEST, e))?;
}
// Checked before anything is saved: an unknown tag changes nothing.
// An unknown tag refuses the edit before the profile is saved.
let tags = match body.tags.as_deref() {
Some(wanted) => Some(resolve_tags(wanted)?),
None => None,
};
// A team's profile carries its tags in the same write as its other settings.
let (team_tags, personal_tags) = if t.team().is_some() { (tags, None) } else { (None, tags) };
let wrote_team_tags = team_tags.is_some();

// Without a rev, the one just read: a change made in between is not undone.
let opts = space::profile::SaveOpts {
sent_rev: Some(body.rev.unwrap_or(stored.meta.rev)),
move_to: body.folder.clone(),
tag_ids: team_tags,
inline_proxy,
password: body.password.clone(),
};
space::profile::save(door, &t, stored, opts).await.map_err(fail(StatusCode::INTERNAL_SERVER_ERROR))?;
if let Some(ids) = tags {
if let Some(ids) = personal_tags {
assign_tags(&t, &id, ids).await?;
} else if wrote_team_tags {
crate::notify_store_changed("tags");
}

let updated = space::profile::load_raw(&id).map_err(fail(StatusCode::INTERNAL_SERVER_ERROR))?;
Expand Down Expand Up @@ -2329,6 +2335,55 @@ mod tests {
drop(t.srv);
}

#[tokio::test]
async fn a_team_patch_moves_and_tags_a_profile_with_one_revision() {
let mut grants = vec![
allow(Scope::Team, Action::See),
allow(folder("Ads"), Action::Start),
allow(folder("Shops"), Action::Create),
];
let t = Team::new(&grants).await;
for f in ["Ads", "Shops"] {
t.folder(f).await;
}
for (id, name) in [("tag-secret-2", "Before"), ("review", "Needs review")] {
let body = json!({ "name": name, "data": b64(json!({ "color": "orange" })) });
t.admin_call(Method::PUT, &format!("/items/tag/{id}"), Some(body)).await;
}
t.profile("p-1", "Ads", "First", json!({})).await;
t.enter().await;

let path = "/profiles/p-1";
let before = t.on_server("p-1").await;
assert_eq!(before["card"]["tags"], json!(["tag-secret-2"]));
let rev = before["revision"].as_u64().unwrap();
let patch = json!({
"rev": rev, "notes": "Ready for review", "folder": "Shops",
"tags": ["needs review", "review"]
});
let (status, got) = t.call("PATCH", path, Some(patch.clone())).await;
assert_eq!((status, got["need"].as_str()), (StatusCode::FORBIDDEN, Some("edit")), "{got}");
assert_eq!(t.on_server("p-1").await, before);

// Edit where it is now, and Create where it goes. No Edit there.
grants.push(allow(folder("Ads"), Action::Edit));
t.set_grants(&grants).await;
let mut bad_tag = patch.clone();
bad_tag["tags"] = json!(["review", "unknown-tag"]);
let (status, got) = t.call("PATCH", path, Some(bad_tag)).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{got}");
assert_eq!(t.on_server("p-1").await, before);

let (status, got) = t.call("PATCH", path, Some(patch)).await;
assert_eq!(status, StatusCode::OK, "{got}");
assert_eq!(got["tags"], json!(["review"]));
let after = t.on_server("p-1").await;
assert_eq!(after["folder"], "Shops");
assert_eq!(after["card"]["notes"], "Ready for review");
assert_eq!(after["card"]["tags"], json!(["review"]));
assert_eq!(after["revision"].as_u64(), Some(rev + 1));
}

#[tokio::test]
async fn a_member_with_the_rights_writes_each_kind_of_change_to_the_team() {
let all = [Action::See, Action::Start, Action::Edit, Action::Create, Action::Delete];
Expand Down
5 changes: 4 additions & 1 deletion src-tauri/src/space/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct SaveOpts {
pub sent_rev: Option<u64>,
/// Where it goes; "" is no folder. None keeps it where it is; a new one takes the settings' own.
pub move_to: Option<String>,
/// Exact tag ids for an existing team profile. None keeps the current tags.
pub tag_ids: Option<Vec<String>>,
/// A proxy line came with it and was stored for it.
pub inline_proxy: bool,
/// A protected profile's password (#127): an edit of its sealed part needs it.
Expand Down Expand Up @@ -134,7 +136,8 @@ pub async fn save_profile(door: Door, t: &Ticket, team: &str, stored: StoredProf
doors::clear(door, team, &ask)?;
let edit = perm::team_write::<Edit>(team, On::Profile { id: id.clone(), folder: now.row.folder.clone() })?;
let to = if moves { Some(perm::team_write::<Create>(team, On::Folder(dest.clone()))?) } else { None };
let card = remote::card(&stored, now.row.card.tags.clone());
let tags = opts.tag_ids.unwrap_or_else(|| now.row.card.tags.clone());
let card = remote::card(&stored, tags);
let data = remote::profile_data(&stored);
let name = remote::name_of(&stored);
let base = opts.sent_rev.unwrap_or(now.row.revision);
Expand Down