mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
dav: Refactor proppatch, remove InvalidProperty trait
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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,27 +93,34 @@ 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() {
|
||||
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);
|
||||
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)]
|
||||
// - 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) => {
|
||||
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),
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user