dav: Make the get_members function more ergonomic

This commit is contained in:
Lennart
2025-06-09 20:35:25 +02:00
parent 0feaaaaca1
commit 0595920809
14 changed files with 121 additions and 96 deletions

View File

@@ -3,6 +3,7 @@ use crate::header::Depth;
use crate::privileges::UserPrivilege;
use crate::resource::PrincipalUri;
use crate::resource::Resource;
use crate::resource::ResourceName;
use crate::resource::ResourceService;
use crate::xml::MultistatusElement;
use crate::xml::PropfindElement;
@@ -12,6 +13,11 @@ use rustical_xml::PropName;
use rustical_xml::XmlDocument;
use tracing::instrument;
type RSMultistatus<R> = MultistatusElement<
<<R as ResourceService>::Resource as Resource>::Prop,
<<R as ResourceService>::MemberType as Resource>::Prop,
>;
#[instrument(skip(path, resource_service, puri))]
pub(crate) async fn axum_route_propfind<R: ResourceService>(
Path(path): Path<R::PathComponents>,
@@ -21,10 +27,7 @@ pub(crate) async fn axum_route_propfind<R: ResourceService>(
uri: OriginalUri,
Extension(puri): Extension<R::PrincipalUri>,
body: String,
) -> Result<
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
R::Error,
> {
) -> Result<RSMultistatus<R>, R::Error> {
route_propfind::<R>(
&path,
uri.path(),
@@ -45,10 +48,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
depth: &Depth,
resource_service: &R,
puri: &impl PrincipalUri,
) -> Result<
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
R::Error,
> {
) -> Result<RSMultistatus<R>, R::Error> {
let resource = resource_service.get_resource(path_components).await?;
let privileges = resource.get_user_privileges(principal)?;
if !privileges.has(&UserPrivilege::Read) {
@@ -75,9 +75,9 @@ pub(crate) async fn route_propfind<R: ResourceService>(
let mut member_responses = Vec::new();
if depth != &Depth::Zero {
for (subpath, member) in resource_service.get_members(path_components).await? {
for member in resource_service.get_members(path_components).await? {
member_responses.push(member.propfind_typed(
&format!("{}/{}", path.trim_end_matches('/'), subpath),
&format!("{}/{}", path.trim_end_matches('/'), member.get_name()),
&propfind_member.prop,
puri,
principal,

View File

@@ -28,6 +28,10 @@ impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
pub trait ResourcePropName: FromStr {}
impl<T: FromStr> ResourcePropName for T {}
pub trait ResourceName {
fn get_name(&self) -> String;
}
pub trait Resource: Clone + Send + 'static {
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + PropName + Send;
type Error: From<crate::Error>;

View File

@@ -10,7 +10,8 @@ use serde::Deserialize;
#[async_trait]
pub trait ResourceService: Clone + Sized + Send + Sync + AxumMethods + 'static {
type PathComponents: for<'de> Deserialize<'de> + Sized + Send + Sync + Clone + 'static; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>;
type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>
+ super::ResourceName;
type Resource: Resource<Error = Self::Error, Principal = Self::Principal>;
type Error: From<crate::Error> + Send + Sync + IntoResponse + 'static;
type Principal: Principal + FromRequestParts<Self>;
@@ -21,7 +22,7 @@ pub trait ResourceService: Clone + Sized + Send + Sync + AxumMethods + 'static {
async fn get_members(
&self,
_path: &Self::PathComponents,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
) -> Result<Vec<Self::MemberType>, Self::Error> {
Ok(vec![])
}

View File

@@ -3,7 +3,7 @@ use crate::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use crate::privileges::UserPrivilegeSet;
use crate::resource::{AxumMethods, PrincipalUri, Resource, ResourceService};
use crate::resource::{AxumMethods, PrincipalUri, Resource, ResourceName, ResourceService};
use crate::xml::{Resourcetype, ResourcetypeInner};
use async_trait::async_trait;
use axum::Router;
@@ -66,6 +66,8 @@ impl<
P: Principal + FromRequestParts<Self>,
PURI: PrincipalUri,
> ResourceService for RootResourceService<PRS, P, PURI>
where
PRS::Resource: ResourceName,
{
type PathComponents = ();
type MemberType = PRS::Resource;