dav: Refactor proppatch, remove InvalidProperty trait

This commit is contained in:
Lennart
2024-12-25 17:34:38 +01:00
parent d74e0fa702
commit 8fdaba2b57
13 changed files with 90 additions and 143 deletions

View File

@@ -24,7 +24,7 @@ use serde::Serialize;
use sha2::{Digest, Sha256};
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
@@ -32,10 +32,10 @@ pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
calendar_id: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(CalendarPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
@@ -90,11 +90,6 @@ pub enum CalendarProp {
Getctag(String),
#[serde(rename = "CS:source", alias = "source")]
Source(Option<HrefElement>),
#[serde(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
#[derive(Clone, Debug, From, Into)]
@@ -168,9 +163,6 @@ impl Resource for CalendarResource {
CalendarPropName::Source => {
CalendarProp::Source(self.0.subscription_url.to_owned().map(HrefElement::from))
}
CalendarPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}
@@ -209,7 +201,6 @@ impl Resource for CalendarResource {
CalendarProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
// Converting between a calendar subscription calendar and a normal one would be weird
CalendarProp::Source(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}
@@ -248,7 +239,6 @@ impl Resource for CalendarResource {
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
// Converting a calendar subscription calendar into a normal one would be weird
CalendarPropName::Source => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}

View File

@@ -11,7 +11,7 @@ use rustical_store::{auth::User, CalendarObject, CalendarStore};
use rustical_xml::XmlDeserialize;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
@@ -20,10 +20,10 @@ pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
object_id: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(CalendarObjectPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
@@ -35,12 +35,6 @@ pub enum CalendarObjectProp {
// CalDAV (RFC 4791)
#[serde(rename = "C:calendar-data")]
CalendarData(String),
#[serde(other)]
#[xml(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
#[derive(Clone, From, Into)]
@@ -73,14 +67,12 @@ impl Resource for CalendarObjectResource {
CalendarObjectPropName::Getcontenttype => {
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
}
CalendarObjectPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}
#[inline]
fn resource_name() -> &'static str {
let a: CalendarObjectPropName = CalendarObjectProp::Getetag("".to_owned()).into();
"caldav_calendar_object"
}

View File

@@ -12,7 +12,7 @@ use rustical_store::CalendarStore;
use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
pub struct PrincipalResourceService<C: CalendarStore + ?Sized> {
principal: String,
@@ -24,10 +24,10 @@ pub struct PrincipalResource {
principal: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(PrincipalPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
@@ -42,12 +42,6 @@ pub enum PrincipalProp {
CalendarHomeSet(HrefElement),
#[serde(rename = "C:calendar-user-address-set")]
CalendarUserAddressSet(HrefElement),
#[serde(other)]
#[xml(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
impl PrincipalResource {
@@ -80,9 +74,6 @@ impl Resource for PrincipalResource {
PrincipalPropName::CalendarUserAddressSet => {
PrincipalProp::CalendarUserAddressSet(principal_href)
}
PrincipalPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

View File

@@ -10,7 +10,7 @@ use rustical_store::{auth::User, AddressObject, AddressbookStore};
use rustical_xml::XmlDeserialize;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
use super::methods::{get_object, put_object};
@@ -21,10 +21,10 @@ pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
object_id: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(AddressObjectPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
@@ -36,12 +36,6 @@ pub enum AddressObjectProp {
// CalDAV (RFC 4791)
#[serde(rename = "CARD:address-data")]
AddressData(String),
#[serde(other)]
#[xml(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
#[derive(Clone, From, Into)]
@@ -74,9 +68,6 @@ impl Resource for AddressObjectResource {
AddressObjectPropName::Getcontenttype => {
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
}
AddressObjectPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

View File

@@ -18,7 +18,7 @@ use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
addr_store: Arc<AS>,
@@ -26,11 +26,11 @@ pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
addressbook_id: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[serde(rename_all = "kebab-case")]
#[strum_discriminants(
name(AddressbookPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
pub enum AddressbookProp {
@@ -61,12 +61,6 @@ pub enum AddressbookProp {
// Didn't find the spec
Getctag(String),
#[serde(other)]
#[xml(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
#[derive(Clone, Debug, From, Into)]
@@ -107,9 +101,6 @@ impl Resource for AddressbookResource {
}
AddressbookPropName::SyncToken => AddressbookProp::SyncToken(self.0.format_synctoken()),
AddressbookPropName::Getctag => AddressbookProp::Getctag(self.0.format_synctoken()),
AddressbookPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}
@@ -129,7 +120,6 @@ impl Resource for AddressbookResource {
AddressbookProp::SupportedAddressData(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::SyncToken(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}
@@ -149,7 +139,6 @@ impl Resource for AddressbookResource {
AddressbookPropName::SupportedAddressData => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}

View File

@@ -12,7 +12,7 @@ use rustical_store::AddressbookStore;
use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
principal: String,
@@ -24,10 +24,10 @@ pub struct PrincipalResource {
principal: String,
}
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(PrincipalPropName),
derive(EnumString, VariantNames),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
@@ -42,12 +42,6 @@ pub enum PrincipalProp {
AddressbookHomeSet(HrefElement),
#[serde(rename = "CARD:principal-address")]
PrincipalAddress(Option<HrefElement>),
#[serde(other)]
#[xml(other)]
#[strum_discriminants(strum(disabled))]
#[default]
Invalid,
}
impl PrincipalResource {
@@ -80,9 +74,6 @@ impl Resource for PrincipalResource {
PrincipalProp::AddressbookHomeSet(principal_href)
}
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
PrincipalPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

View File

@@ -1,9 +0,0 @@
pub trait InvalidProperty {
fn invalid_property(&self) -> bool;
}
impl<T: Default + PartialEq> InvalidProperty for T {
fn invalid_property(&self) -> bool {
self == &T::default()
}
}

View File

@@ -1,5 +1,4 @@
use crate::privileges::UserPrivilege;
use crate::resource::InvalidProperty;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
@@ -9,6 +8,7 @@ use crate::Error;
use actix_web::http::StatusCode;
use actix_web::{web::Path, HttpRequest};
use rustical_store::auth::User;
use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlDocument;
use rustical_xml::XmlRootTag;
@@ -16,6 +16,21 @@ use std::str::FromStr;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[derive(XmlDeserialize, Clone, Debug)]
#[xml(untagged)]
enum SetPropertyPropWrapper<T: XmlDeserialize> {
Valid(T),
Invalid(Unparsed),
}
// We are <prop>
#[derive(XmlDeserialize, Clone, Debug)]
struct SetPropertyPropWrapperWrapper<T: XmlDeserialize> {
#[xml(ty = "untagged")]
property: SetPropertyPropWrapper<T>,
}
// We are <set>
#[derive(XmlDeserialize, Clone, Debug)]
struct SetPropertyElement<T: XmlDeserialize> {
prop: T,
@@ -47,10 +62,6 @@ enum Operation<T: XmlDeserialize> {
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
#[xml(root = b"propertyupdate")]
struct PropertyupdateElement<T: XmlDeserialize> {
// #[xml(flatten)]
// set: Vec<T>,
// #[xml(flatten)]
// remove: Vec<TagName>,
#[xml(ty = "untagged", flatten)]
operations: Vec<Operation<T>>,
}
@@ -68,21 +79,9 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
let resource_service = R::new(&req, path_components.clone()).await?;
// Extract operations
let PropertyupdateElement::<<R::Resource as Resource>::Prop> { operations } =
XmlDocument::parse_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> = PropertyupdateElement::<TagName>::parse_str(&body)
.map_err(Error::XmlDeserializationError)?
.operations
.into_iter()
.map(|op_el| match op_el {
Operation::Set(set_el) => set_el.prop.name,
// If we can't remove a nonexisting property then that's no big deal
Operation::Remove(remove_el) => remove_el.prop.property.name,
})
.collect();
let PropertyupdateElement::<SetPropertyPropWrapperWrapper<<R::Resource as Resource>::Prop>> {
operations,
} = XmlDocument::parse_str(&body).map_err(Error::XmlDeserializationError)?;
let mut resource = resource_service.get_resource().await?;
let privileges = resource.get_user_privileges(&user)?;
@@ -94,10 +93,21 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
let mut props_conflict = Vec::new();
let mut props_not_found = Vec::new();
for (operation, propname) in operations.into_iter().zip(propnames) {
for operation in operations.into_iter() {
match operation {
Operation::Set(SetPropertyElement { prop }) => {
if prop.invalid_property() {
match prop.property {
SetPropertyPropWrapper::Valid(prop) => {
let propname: <R::Resource as Resource>::PropName = prop.clone().into();
let propname: &str = propname.into();
match resource.set_prop(prop) {
Ok(()) => props_ok.push(propname.to_owned()),
Err(Error::PropReadOnly) => props_conflict.push(propname.to_owned()),
Err(err) => return Err(err.into()),
};
}
SetPropertyPropWrapper::Invalid(invalid) => {
let propname = invalid.tag_name();
if <R::Resource as Resource>::list_props().contains(&propname.as_str()) {
// This happens in following cases:
// - read-only properties with #[serde(skip_deserializing)]
@@ -106,15 +116,11 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
} 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) => {
}
Operation::Remove(remove_el) => {
let propname = remove_el.prop.property.name;
match <<R::Resource as Resource>::PropName as FromStr>::from_str(&propname) {
Ok(prop) => match resource.remove_prop(&prop) {
Ok(()) => props_ok.push(propname),

View File

@@ -7,7 +7,6 @@ use actix_web::dev::ResourceMap;
use actix_web::error::UrlGenerationError;
use actix_web::test::TestRequest;
use actix_web::{http::StatusCode, ResponseError};
pub use invalid_property::InvalidProperty;
use itertools::Itertools;
pub use resource_service::ResourceService;
use rustical_store::auth::User;
@@ -16,12 +15,11 @@ use serde::Serialize;
use std::str::FromStr;
use strum::{EnumString, VariantNames};
mod invalid_property;
mod methods;
mod resource_service;
pub trait ResourceProp: InvalidProperty + Serialize + XmlDeserialize {}
impl<T: InvalidProperty + Serialize + XmlDeserialize> ResourceProp for T {}
pub trait ResourceProp: Serialize + XmlDeserialize {}
impl<T: Serialize + XmlDeserialize> ResourceProp for T {}
pub trait ResourcePropName: FromStr + VariantNames {}
impl<T: FromStr + VariantNames> ResourcePropName for T {}
@@ -29,7 +27,7 @@ impl<T: FromStr + VariantNames> ResourcePropName for T {}
pub trait ResourceType: Serialize + XmlDeserialize {}
impl<T: Serialize + XmlDeserialize> ResourceType for T {}
#[derive(XmlDeserialize, Serialize, PartialEq, Default)]
#[derive(XmlDeserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum CommonPropertiesProp {
// WebDAV (RFC 2518)
@@ -43,10 +41,6 @@ pub enum CommonPropertiesProp {
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
Owner(HrefElement),
#[serde(other)]
#[default]
Invalid,
}
#[derive(Serialize)]
@@ -67,8 +61,8 @@ pub enum CommonPropertiesPropName {
}
pub trait Resource: Clone + 'static {
type PropName: ResourcePropName;
type Prop: ResourceProp + PartialEq;
type PropName: ResourcePropName + From<Self::Prop> + Into<&'static str>;
type Prop: ResourceProp + PartialEq + Clone;
type Error: ResponseError + From<crate::Error>;
type PrincipalResource: Resource;

View File

@@ -8,7 +8,7 @@ use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::any::type_name;
use std::marker::PhantomData;
use strum::{EnumString, VariantNames};
use strum::{EnumString, IntoStaticStr, VariantNames};
#[derive(Clone)]
pub struct RootResource<PR: Resource>(PhantomData<PR>);
@@ -19,15 +19,17 @@ impl<PR: Resource> Default for RootResource<PR> {
}
}
#[derive(EnumString, VariantNames, Clone)]
#[derive(EnumString, VariantNames, Clone, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum RootResourcePropName {}
#[derive(XmlDeserialize, Serialize, Default, Clone, PartialEq)]
pub enum RootResourceProp {
#[serde(other)]
#[default]
Invalid,
#[derive(XmlDeserialize, Serialize, Clone, PartialEq)]
pub enum RootResourceProp {}
impl From<RootResourceProp> for RootResourcePropName {
fn from(_value: RootResourceProp) -> Self {
unreachable!()
}
}
impl<PR: Resource> Resource for RootResource<PR> {

View File

@@ -5,11 +5,13 @@ use darling::FromDeriveInput;
use quote::quote;
use syn::{DataStruct, DeriveInput};
fn invalid_field_branch(allow: bool) -> proc_macro2::TokenStream {
fn invalid_field_branch(ident: &syn::Ident, allow: bool) -> proc_macro2::TokenStream {
let ident = ident.to_string();
if allow {
quote! {}
} else {
quote! { return Err(XmlDeError::InvalidFieldName(format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
quote! {
return Err(XmlDeError::InvalidFieldName(#ident, format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
}
}
@@ -85,7 +87,8 @@ impl NamedStruct {
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
let invalid_field_branch = invalid_field_branch(self.attrs.allow_invalid.is_present());
let invalid_field_branch =
invalid_field_branch(ident, self.attrs.allow_invalid.is_present());
quote! {
impl #impl_generics ::rustical_xml::XmlDeserialize for #ident #type_generics #where_clause {

View File

@@ -28,8 +28,8 @@ pub enum XmlDeError {
Other(String),
#[error("Invalid variant: {0}")]
InvalidVariant(String),
#[error("Invalid field name: {0}")]
InvalidFieldName(String),
#[error("Invalid field name in {0}: {1}")]
InvalidFieldName(&'static str, String),
#[error(transparent)]
InvalidValue(#[from] crate::value::ParseValueError),
}

View File

@@ -36,6 +36,13 @@ impl XmlDeserialize for () {
#[derive(Debug, Clone, PartialEq)]
pub struct Unparsed(BytesStart<'static>);
impl Unparsed {
pub fn tag_name(&self) -> String {
// TODO: respect namespace?
String::from_utf8_lossy(self.0.local_name().as_ref()).to_string()
}
}
impl XmlDeserialize for Unparsed {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,