mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
Refactoring
This commit is contained in:
35
crates/dav/src/resource/methods/delete.rs
Normal file
35
crates/dav/src/resource/methods/delete.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::privileges::UserPrivilege;
|
||||
use crate::resource::Resource;
|
||||
use crate::resource::ResourceService;
|
||||
use crate::Error;
|
||||
use actix_web::web::Path;
|
||||
use actix_web::HttpRequest;
|
||||
use actix_web::HttpResponse;
|
||||
use actix_web::Responder;
|
||||
use rustical_store::auth::User;
|
||||
|
||||
pub async fn route_delete<R: ResourceService>(
|
||||
path_components: Path<R::PathComponents>,
|
||||
req: HttpRequest,
|
||||
user: User,
|
||||
) -> Result<impl Responder, R::Error> {
|
||||
let path_components = path_components.into_inner();
|
||||
|
||||
let no_trash = req
|
||||
.headers()
|
||||
.get("X-No-Trashbin")
|
||||
.map(|val| matches!(val.to_str(), Ok("1")))
|
||||
.unwrap_or(false);
|
||||
|
||||
let resource_service = R::new(&req, path_components.clone()).await?;
|
||||
let resource = resource_service.get_resource().await?;
|
||||
let privileges = resource.get_user_privileges(&user)?;
|
||||
if !privileges.has(&UserPrivilege::Write) {
|
||||
// TODO: Actually the spec wants us to look whether we have unbind access in the parent
|
||||
// collection
|
||||
return Err(Error::Unauthorized.into());
|
||||
}
|
||||
resource_service.delete_resource(!no_trash).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().body(""))
|
||||
}
|
||||
7
crates/dav/src/resource/methods/mod.rs
Normal file
7
crates/dav/src/resource/methods/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod delete;
|
||||
mod propfind;
|
||||
mod proppatch;
|
||||
|
||||
pub(crate) use delete::route_delete;
|
||||
pub(crate) use propfind::route_propfind;
|
||||
pub(crate) use proppatch::route_proppatch;
|
||||
85
crates/dav/src/resource/methods/propfind.rs
Normal file
85
crates/dav/src/resource/methods/propfind.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::depth_header::Depth;
|
||||
use crate::privileges::UserPrivilege;
|
||||
use crate::resource::CommonPropertiesProp;
|
||||
use crate::resource::EitherProp;
|
||||
use crate::resource::Resource;
|
||||
use crate::resource::ResourceService;
|
||||
use crate::xml::MultistatusElement;
|
||||
use crate::xml::PropElement;
|
||||
use crate::xml::PropfindType;
|
||||
use crate::Error;
|
||||
use actix_web::web::Path;
|
||||
use actix_web::HttpRequest;
|
||||
use rustical_store::auth::User;
|
||||
use serde::Deserialize;
|
||||
use tracing::instrument;
|
||||
use tracing_actix_web::RootSpan;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct PropfindElement {
|
||||
#[serde(rename = "$value")]
|
||||
prop: PropfindType,
|
||||
}
|
||||
|
||||
#[instrument(parent = root_span.id(), skip(path_components, req, root_span))]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
path_components: Path<R::PathComponents>,
|
||||
body: String,
|
||||
req: HttpRequest,
|
||||
user: User,
|
||||
depth: Depth,
|
||||
root_span: RootSpan,
|
||||
) -> Result<
|
||||
MultistatusElement<
|
||||
EitherProp<<R::Resource as Resource>::Prop, CommonPropertiesProp>,
|
||||
EitherProp<<R::MemberType as Resource>::Prop, CommonPropertiesProp>,
|
||||
>,
|
||||
R::Error,
|
||||
> {
|
||||
let resource_service = R::new(&req, path_components.into_inner()).await?;
|
||||
|
||||
let resource = resource_service.get_resource().await?;
|
||||
let privileges = resource.get_user_privileges(&user)?;
|
||||
if !privileges.has(&UserPrivilege::Read) {
|
||||
return Err(Error::Unauthorized.into());
|
||||
}
|
||||
|
||||
// A request body is optional. If empty we MUST return all props
|
||||
let propfind: PropfindElement = if !body.is_empty() {
|
||||
quick_xml::de::from_str(&body).map_err(Error::XmlDeserializationError)?
|
||||
} else {
|
||||
PropfindElement {
|
||||
prop: PropfindType::Allprop,
|
||||
}
|
||||
};
|
||||
|
||||
let props = match propfind.prop {
|
||||
PropfindType::Allprop => vec!["allprop".to_owned()],
|
||||
PropfindType::Propname => vec!["propname".to_owned()],
|
||||
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into_inner(),
|
||||
};
|
||||
let props: Vec<&str> = props.iter().map(String::as_str).collect();
|
||||
|
||||
let mut member_responses = Vec::new();
|
||||
if depth != Depth::Zero {
|
||||
for (path, member) in resource_service.get_members(req.resource_map()).await? {
|
||||
member_responses.push(member.propfind(
|
||||
&path,
|
||||
props.clone(),
|
||||
&user,
|
||||
req.resource_map(),
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
let response = resource.propfind(req.path(), props, &user, req.resource_map())?;
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![response],
|
||||
member_responses,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
153
crates/dav/src/resource/methods/proppatch.rs
Normal file
153
crates/dav/src/resource/methods/proppatch.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
use crate::privileges::UserPrivilege;
|
||||
use crate::resource::InvalidProperty;
|
||||
use crate::resource::Resource;
|
||||
use crate::resource::ResourceService;
|
||||
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
|
||||
use crate::xml::MultistatusElement;
|
||||
use crate::xml::TagList;
|
||||
use crate::xml::TagName;
|
||||
use crate::Error;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{web::Path, HttpRequest};
|
||||
use rustical_store::auth::User;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
use tracing::instrument;
|
||||
use tracing_actix_web::RootSpan;
|
||||
|
||||
// https://docs.rs/quick-xml/latest/quick_xml/de/index.html#normal-enum-variant
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct PropertyElement<T> {
|
||||
#[serde(rename = "$value")]
|
||||
prop: T,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct SetPropertyElement<T> {
|
||||
prop: PropertyElement<T>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct RemovePropertyElement {
|
||||
prop: PropertyElement<TagName>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
enum Operation<PropType> {
|
||||
Set(SetPropertyElement<PropType>),
|
||||
Remove(RemovePropertyElement),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct PropertyupdateElement<T> {
|
||||
#[serde(rename = "$value", default = "Vec::new")]
|
||||
operations: Vec<Operation<T>>,
|
||||
}
|
||||
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span))]
|
||||
pub(crate) async fn route_proppatch<R: ResourceService>(
|
||||
path: Path<R::PathComponents>,
|
||||
body: String,
|
||||
req: HttpRequest,
|
||||
user: User,
|
||||
root_span: RootSpan,
|
||||
) -> Result<MultistatusElement<String, String>, R::Error> {
|
||||
let path_components = path.into_inner();
|
||||
let href = req.path().to_owned();
|
||||
let resource_service = R::new(&req, path_components.clone()).await?;
|
||||
|
||||
// Extract operations
|
||||
let PropertyupdateElement::<<R::Resource as Resource>::Prop> { operations } =
|
||||
quick_xml::de::from_str(&body).map_err(Error::XmlDeserializationError)?;
|
||||
|
||||
// Extract all set property names without verification
|
||||
// Weird workaround because quick_xml doesn't allow untagged enums
|
||||
let propnames: Vec<String> = quick_xml::de::from_str::<PropertyupdateElement<TagName>>(&body)
|
||||
.map_err(Error::XmlDeserializationError)?
|
||||
.operations
|
||||
.into_iter()
|
||||
.map(|op_el| match op_el {
|
||||
Operation::Set(set_el) => set_el.prop.prop.into(),
|
||||
// If we can't remove a nonexisting property then that's no big deal
|
||||
Operation::Remove(remove_el) => remove_el.prop.prop.into(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut resource = resource_service.get_resource().await?;
|
||||
let privileges = resource.get_user_privileges(&user)?;
|
||||
if !privileges.has(&UserPrivilege::Write) {
|
||||
return Err(Error::Unauthorized.into());
|
||||
}
|
||||
|
||||
let mut props_ok = Vec::new();
|
||||
let mut props_conflict = Vec::new();
|
||||
let mut props_not_found = Vec::new();
|
||||
|
||||
for (operation, propname) in operations.into_iter().zip(propnames) {
|
||||
match operation {
|
||||
Operation::Set(SetPropertyElement {
|
||||
prop: PropertyElement { prop },
|
||||
}) => {
|
||||
if prop.invalid_property() {
|
||||
if <R::Resource as Resource>::list_props().contains(&propname.as_str()) {
|
||||
// This happens in following cases:
|
||||
// - read-only properties with #[serde(skip_deserializing)]
|
||||
// - internal properties
|
||||
props_conflict.push(propname)
|
||||
} else {
|
||||
props_not_found.push(propname);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match resource.set_prop(prop) {
|
||||
Ok(()) => props_ok.push(propname),
|
||||
Err(Error::PropReadOnly) => props_conflict.push(propname),
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
}
|
||||
Operation::Remove(_remove_el) => {
|
||||
match <<R::Resource as Resource>::PropName as FromStr>::from_str(&propname) {
|
||||
Ok(prop) => match resource.remove_prop(&prop) {
|
||||
Ok(()) => props_ok.push(propname),
|
||||
Err(Error::PropReadOnly) => props_conflict.push(propname),
|
||||
Err(err) => return Err(err.into()),
|
||||
},
|
||||
// I guess removing a nonexisting property should be successful :)
|
||||
Err(_) => props_ok.push(propname),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if props_not_found.is_empty() && props_conflict.is_empty() {
|
||||
// Only save if no errors occured
|
||||
resource_service.save_resource(resource).await?;
|
||||
}
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![ResponseElement {
|
||||
href,
|
||||
propstat: vec![
|
||||
PropstatWrapper::TagList(PropstatElement {
|
||||
prop: TagList::from(props_ok),
|
||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||
}),
|
||||
PropstatWrapper::TagList(PropstatElement {
|
||||
prop: TagList::from(props_not_found),
|
||||
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
|
||||
}),
|
||||
PropstatWrapper::TagList(PropstatElement {
|
||||
prop: TagList::from(props_conflict),
|
||||
status: format!("HTTP/1.1 {}", StatusCode::CONFLICT),
|
||||
}),
|
||||
],
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user