mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
Replace this internal_props stuff with CommonPropertiesExtension
This commit is contained in:
@@ -14,7 +14,6 @@ futures-util = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
rustical_store = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
strum = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
70
crates/dav/src/extensions/common.rs
Normal file
70
crates/dav/src/extensions/common.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use crate::{
|
||||
privileges::UserPrivilegeSet,
|
||||
resource::{NamedRoute, Resource},
|
||||
xml::{HrefElement, Resourcetype},
|
||||
};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use rustical_store::auth::User;
|
||||
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
|
||||
#[xml(unit_variants_ident = "CommonPropertiesPropName")]
|
||||
pub enum CommonPropertiesProp {
|
||||
// WebDAV (RFC 2518)
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
Resourcetype(Resourcetype),
|
||||
|
||||
// WebDAV Current Principal Extension (RFC 5397)
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
CurrentUserPrincipal(HrefElement),
|
||||
|
||||
// WebDAV Access Control Protocol (RFC 3477)
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
CurrentUserPrivilegeSet(UserPrivilegeSet),
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
Owner(Option<HrefElement>),
|
||||
}
|
||||
|
||||
pub trait CommonPropertiesExtension: Resource {
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
user: &User,
|
||||
prop: &CommonPropertiesPropName,
|
||||
) -> Result<CommonPropertiesProp, <Self as Resource>::Error> {
|
||||
Ok(match prop {
|
||||
CommonPropertiesPropName::Resourcetype => {
|
||||
CommonPropertiesProp::Resourcetype(self.get_resourcetype())
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrincipal => {
|
||||
CommonPropertiesProp::CurrentUserPrincipal(
|
||||
Self::PrincipalResource::get_url(rmap, [&user.id])
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
|
||||
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
|
||||
}
|
||||
CommonPropertiesPropName::Owner => {
|
||||
CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
|
||||
Self::PrincipalResource::get_url(rmap, [owner])
|
||||
.unwrap()
|
||||
.into()
|
||||
}))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn set_prop(&self, _prop: CommonPropertiesProp) -> Result<(), crate::Error> {
|
||||
Err(crate::Error::PropReadOnly)
|
||||
}
|
||||
|
||||
fn remove_prop(&self, _prop: &CommonPropertiesPropName) -> Result<(), crate::Error> {
|
||||
Err(crate::Error::PropReadOnly)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Resource> CommonPropertiesExtension for T {}
|
||||
@@ -1,5 +1,7 @@
|
||||
mod common;
|
||||
mod davpush;
|
||||
mod synctoken;
|
||||
|
||||
pub use common::*;
|
||||
pub use davpush::*;
|
||||
pub use synctoken::*;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
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;
|
||||
@@ -28,10 +26,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
root_span: RootSpan,
|
||||
resource_service: Data<R>,
|
||||
) -> Result<
|
||||
MultistatusElement<
|
||||
EitherProp<<R::Resource as Resource>::Prop, CommonPropertiesProp>,
|
||||
EitherProp<<R::MemberType as Resource>::Prop, CommonPropertiesProp>,
|
||||
>,
|
||||
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
|
||||
R::Error,
|
||||
> {
|
||||
let resource = resource_service.get_resource(&path).await?;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::privileges::UserPrivilegeSet;
|
||||
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
||||
use crate::xml::Resourcetype;
|
||||
use crate::xml::{multistatus::ResponseElement, TagList};
|
||||
use crate::xml::{HrefElement, Resourcetype};
|
||||
use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::{http::StatusCode, ResponseError};
|
||||
@@ -11,7 +11,6 @@ pub use resource_service::ResourceService;
|
||||
use rustical_store::auth::User;
|
||||
use rustical_xml::{EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
use std::str::FromStr;
|
||||
use strum::EnumString;
|
||||
|
||||
mod methods;
|
||||
mod resource_service;
|
||||
@@ -24,41 +23,6 @@ impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
|
||||
pub trait ResourcePropName: FromStr {}
|
||||
impl<T: FromStr> ResourcePropName for T {}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumVariants)]
|
||||
pub enum CommonPropertiesProp {
|
||||
// WebDAV (RFC 2518)
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
Resourcetype(Resourcetype),
|
||||
|
||||
// WebDAV Current Principal Extension (RFC 5397)
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
CurrentUserPrincipal(HrefElement),
|
||||
|
||||
// WebDAV Access Control Protocol (RFC 3477)
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
CurrentUserPrivilegeSet(UserPrivilegeSet),
|
||||
#[xml(ns = "crate::namespace::NS_DAV")]
|
||||
Owner(Option<HrefElement>),
|
||||
}
|
||||
|
||||
#[derive(XmlSerialize)]
|
||||
#[xml(untagged)]
|
||||
pub enum EitherProp<Left: ResourceProp, Right: ResourceProp> {
|
||||
Left(Left),
|
||||
Right(Right),
|
||||
}
|
||||
|
||||
#[derive(EnumString, Clone)]
|
||||
#[strum(serialize_all = "kebab-case")]
|
||||
pub enum CommonPropertiesPropName {
|
||||
Resourcetype,
|
||||
CurrentUserPrincipal,
|
||||
CurrentUserPrivilegeSet,
|
||||
Owner,
|
||||
}
|
||||
|
||||
pub trait Resource: Clone + 'static {
|
||||
type PropName: ResourcePropName
|
||||
+ From<Self::Prop>
|
||||
@@ -70,41 +34,7 @@ pub trait Resource: Clone + 'static {
|
||||
fn get_resourcetype(&self) -> Resourcetype;
|
||||
|
||||
fn list_props() -> Vec<(Option<Namespace<'static>>, &'static str)> {
|
||||
[
|
||||
Self::Prop::variant_names(),
|
||||
CommonPropertiesProp::variant_names(),
|
||||
]
|
||||
.concat()
|
||||
}
|
||||
|
||||
fn get_internal_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
user: &User,
|
||||
prop: &CommonPropertiesPropName,
|
||||
) -> Result<CommonPropertiesProp, Self::Error> {
|
||||
Ok(match prop {
|
||||
CommonPropertiesPropName::Resourcetype => {
|
||||
CommonPropertiesProp::Resourcetype(self.get_resourcetype())
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrincipal => {
|
||||
CommonPropertiesProp::CurrentUserPrincipal(
|
||||
Self::PrincipalResource::get_url(rmap, [&user.id])
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
|
||||
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
|
||||
}
|
||||
CommonPropertiesPropName::Owner => {
|
||||
CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
|
||||
Self::PrincipalResource::get_url(rmap, [owner])
|
||||
.unwrap()
|
||||
.into()
|
||||
}))
|
||||
}
|
||||
})
|
||||
Self::Prop::variant_names()
|
||||
}
|
||||
|
||||
fn get_prop(
|
||||
@@ -134,7 +64,7 @@ pub trait Resource: Clone + 'static {
|
||||
props: &[&str],
|
||||
user: &User,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<ResponseElement<EitherProp<Self::Prop, CommonPropertiesProp>>, Self::Error> {
|
||||
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
|
||||
let mut props = props.to_vec();
|
||||
|
||||
if props.contains(&"propname") {
|
||||
@@ -174,30 +104,19 @@ pub trait Resource: Clone + 'static {
|
||||
}
|
||||
|
||||
let mut valid_props = vec![];
|
||||
let mut internal_props = vec![];
|
||||
let mut invalid_props = vec![];
|
||||
for prop in props {
|
||||
if let Ok(valid_prop) = Self::PropName::from_str(prop) {
|
||||
valid_props.push(valid_prop);
|
||||
} else if let Ok(internal_prop) = CommonPropertiesPropName::from_str(prop) {
|
||||
internal_props.push(internal_prop);
|
||||
} else {
|
||||
invalid_props.push(prop.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
let internal_prop_responses: Vec<_> = internal_props
|
||||
.into_iter()
|
||||
.map(|prop| self.get_internal_prop(rmap, user, &prop))
|
||||
.map_ok(EitherProp::Right)
|
||||
.collect::<Result<_, Self::Error>>()?;
|
||||
|
||||
let mut prop_responses = valid_props
|
||||
let prop_responses = valid_props
|
||||
.into_iter()
|
||||
.map(|prop| self.get_prop(rmap, user, &prop))
|
||||
.map_ok(EitherProp::Left)
|
||||
.collect::<Result<Vec<_>, Self::Error>>()?;
|
||||
prop_responses.extend(internal_prop_responses);
|
||||
|
||||
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
|
||||
status: StatusCode::OK,
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
use crate::extensions::{
|
||||
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
|
||||
};
|
||||
use crate::privileges::UserPrivilegeSet;
|
||||
use crate::resource::{NamedRoute, Resource, ResourceService};
|
||||
use crate::xml::{Resourcetype, ResourcetypeInner};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use async_trait::async_trait;
|
||||
use quick_xml::name::Namespace;
|
||||
use rustical_store::auth::User;
|
||||
use rustical_xml::{EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
use serde::Serialize;
|
||||
use std::marker::PhantomData;
|
||||
use strum::{EnumString, IntoStaticStr};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RootResource<PR: Resource>(PhantomData<PR>);
|
||||
@@ -19,28 +18,9 @@ impl<PR: Resource> Default for RootResource<PR> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(EnumString, Clone, IntoStaticStr)]
|
||||
#[strum(serialize_all = "kebab-case")]
|
||||
pub enum RootResourcePropName {}
|
||||
|
||||
impl From<RootResourcePropName> for (Option<Namespace<'static>>, &'static str) {
|
||||
fn from(_value: RootResourcePropName) -> Self {
|
||||
(None, "unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, Serialize, Clone, PartialEq, EnumVariants)]
|
||||
pub enum RootResourceProp {}
|
||||
|
||||
impl From<RootResourceProp> for RootResourcePropName {
|
||||
fn from(_value: RootResourceProp) -> Self {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
|
||||
type PropName = RootResourcePropName;
|
||||
type Prop = RootResourceProp;
|
||||
type PropName = CommonPropertiesPropName;
|
||||
type Prop = CommonPropertiesProp;
|
||||
type Error = PR::Error;
|
||||
type PrincipalResource = PR;
|
||||
|
||||
@@ -53,11 +33,11 @@ impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
_rmap: &ResourceMap,
|
||||
_user: &User,
|
||||
_prop: &Self::PropName,
|
||||
rmap: &ResourceMap,
|
||||
user: &User,
|
||||
prop: &Self::PropName,
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
unreachable!("we shouldn't end up here")
|
||||
CommonPropertiesExtension::get_prop(self, rmap, user, prop)
|
||||
}
|
||||
|
||||
fn get_user_privileges(&self, _user: &User) -> Result<UserPrivilegeSet, Self::Error> {
|
||||
|
||||
Reference in New Issue
Block a user