| 1 | use crate::*; |
| 2 | |
| 3 | fn operation(method: &Method, parts: &[&str], mut body: Value) -> Result<(&'static str, Value)> { |
| 4 | let action = match parts { |
| 5 | [] if method == Method::GET => return Ok(("state", json!({}))), |
| 6 | ["folders"] if method == Method::GET => return Ok(("folders.list", body)), |
| 7 | ["accounts"] if method == Method::POST => "account.create", |
| 8 | ["accounts", _] if method == Method::PATCH => "account.update", |
| 9 | ["accounts", _] if method == Method::DELETE => "account.delete", |
| 10 | ["accounts", _, "rotate"] if method == Method::POST => "account.rotate", |
| 11 | ["grants"] if method == Method::POST => "grant.save", |
| 12 | ["grants", _] if method == Method::PATCH => "grant.save", |
| 13 | ["grants", _] if method == Method::DELETE => "grant.delete", |
| 14 | ["links"] if method == Method::POST => "link.create", |
| 15 | ["links", _] if method == Method::PATCH => "link.update", |
| 16 | ["links", _] if method == Method::DELETE => "link.delete", |
| 17 | _ => return Err(Error::new(404, "No share action here.")), |
| 18 | }; |
| 19 | if body.is_null() { |
| 20 | body = json!({}); |
| 21 | } |
| 22 | let fields = body |
| 23 | .as_object_mut() |
| 24 | .ok_or_else(|| Error::new(400, "Reload shares and try again."))?; |
| 25 | if let Some(id) = parts.get(1) { |
| 26 | let key = if parts[0] == "links" { "key" } else { "id" }; |
| 27 | if fields.contains_key(key) { |
| 28 | return Err(Error::new(400, "Reload shares before saving this change.")); |
| 29 | } |
| 30 | fields.insert(key.into(), json!(id)); |
| 31 | } else if parts == ["grants"] { |
| 32 | fields.insert("id".into(), Value::Null); |
| 33 | } |
| 34 | Ok((action, body)) |
| 35 | } |
| 36 | |
| 37 | pub async fn route( |
| 38 | app: Arc<App>, |
| 39 | method: &Method, |
| 40 | parts: &[&str], |
| 41 | query: &HashMap<String, String>, |
| 42 | me: &Value, |
| 43 | body: Value, |
| 44 | ) -> Result<Response> { |
| 45 | need(me, "admin")?; |
| 46 | let body = if parts == ["folders"] && method == Method::GET { |
| 47 | json!({"path":query.get("path").map(String::as_str).unwrap_or_default()}) |
| 48 | } else { |
| 49 | body |
| 50 | }; |
| 51 | let (action, payload) = operation(method, parts, body)?; |
| 52 | let users = { |
| 53 | let db = app.auth.db.lock().unwrap(); |
| 54 | let mut statement = db.prepare("SELECT id FROM users ORDER BY username")?; |
| 55 | statement |
| 56 | .query_map([], |row| row.get::<_, String>(0))? |
| 57 | .map(|id| { |
| 58 | let id = id?; |
| 59 | let profile = auth::user(&db, &id)?; |
| 60 | let groups = array(&profile["groups"]).iter().map(|group| group["name"].clone()).collect::<Vec<_>>(); |
| 61 | Ok(json!({"id":id,"username":profile["username"],"enabled":profile["enabled"],"kind":profile["kind"],"requiredActions":profile["requiredActions"],"groups":groups})) |
| 62 | }) |
| 63 | .collect::<Result<Vec<_>>>()? |
| 64 | }; |
| 65 | let mut value = host::call(json!({ |
| 66 | "operation":"copyparty.manage", "action":action, "payload":payload, "users":users, |
| 67 | })) |
| 68 | .await?; |
| 69 | if value.is_object() { |
| 70 | value["filesUrl"] = json!(env( |
| 71 | "SNOWGLOBE_FILES_URL", |
| 72 | &format!("https://file.{}", env("SNOWGLOBE_DOMAIN", "snowglobe.test")), |
| 73 | )); |
| 74 | } |
| 75 | Ok(Document::new(value).response()) |
| 76 | } |
| 77 | |
| 78 | #[cfg(test)] |
| 79 | mod tests { |
| 80 | use super::*; |
| 81 | |
| 82 | #[test] |
| 83 | fn route_identity_cannot_be_overridden_by_the_body() { |
| 84 | assert!(operation(&Method::PATCH, &["links", "kept"], json!({"key":"other"})).is_err()); |
| 85 | assert!(operation(&Method::PATCH, &["accounts", "kept"], json!({"id":"other"})).is_err()); |
| 86 | let (action, payload) = |
| 87 | operation(&Method::DELETE, &["links", "kept"], Value::Null).unwrap(); |
| 88 | assert_eq!(action, "link.delete"); |
| 89 | assert_eq!(payload, json!({"key":"kept"})); |
| 90 | assert!(operation(&Method::GET, &["accounts", "kept", "rotate"], Value::Null).is_err()); |
| 91 | } |
| 92 | } |