mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 04:42:15 +00:00
Refactoring around routing and getting the principal uri (less dependence on actix)
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
use crate::{
|
||||
Principal,
|
||||
privileges::UserPrivilegeSet,
|
||||
resource::{NamedRoute, Resource},
|
||||
resource::{PrincipalUri, Resource},
|
||||
xml::{HrefElement, Resourcetype},
|
||||
};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
|
||||
@@ -28,11 +27,9 @@ pub enum CommonPropertiesProp {
|
||||
}
|
||||
|
||||
pub trait CommonPropertiesExtension: Resource {
|
||||
type PrincipalResource: NamedRoute;
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
principal_uri: &impl PrincipalUri,
|
||||
principal: &Self::Principal,
|
||||
prop: &CommonPropertiesPropName,
|
||||
) -> Result<CommonPropertiesProp, <Self as Resource>::Error> {
|
||||
@@ -42,21 +39,16 @@ pub trait CommonPropertiesExtension: Resource {
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrincipal => {
|
||||
CommonPropertiesProp::CurrentUserPrincipal(
|
||||
Self::PrincipalResource::get_url(rmap, [&principal.get_id()])
|
||||
.unwrap()
|
||||
.into(),
|
||||
principal_uri.principal_uri(principal.get_id()).into(),
|
||||
)
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
|
||||
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(principal)?)
|
||||
}
|
||||
CommonPropertiesPropName::Owner => {
|
||||
CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
|
||||
Self::PrincipalResource::get_url(rmap, [owner])
|
||||
.unwrap()
|
||||
.into()
|
||||
}))
|
||||
}
|
||||
CommonPropertiesPropName::Owner => CommonPropertiesProp::Owner(
|
||||
self.get_owner()
|
||||
.map(|owner| principal_uri.principal_uri(owner).into()),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -68,3 +60,5 @@ pub trait CommonPropertiesExtension: Resource {
|
||||
Err(crate::Error::PropReadOnly)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Resource> CommonPropertiesExtension for R {}
|
||||
|
||||
@@ -14,7 +14,7 @@ use rustical_xml::XmlDocument;
|
||||
use tracing::instrument;
|
||||
use tracing_actix_web::RootSpan;
|
||||
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
|
||||
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service, puri))]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
path: Path<R::PathComponents>,
|
||||
@@ -24,6 +24,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
depth: Depth,
|
||||
root_span: RootSpan,
|
||||
resource_service: Data<R>,
|
||||
puri: Data<R::PrincipalUri>,
|
||||
) -> Result<
|
||||
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
|
||||
R::Error,
|
||||
@@ -61,13 +62,13 @@ pub(crate) async fn route_propfind<R: ResourceService>(
|
||||
member_responses.push(member.propfind(
|
||||
&format!("{}/{}", req.path().trim_end_matches('/'), subpath),
|
||||
&props,
|
||||
puri.as_ref(),
|
||||
&user,
|
||||
req.resource_map(),
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
let response = resource.propfind(req.path(), &props, &user, req.resource_map())?;
|
||||
let response = resource.propfind(req.path(), &props, puri.as_ref(), &user)?;
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![response],
|
||||
|
||||
@@ -3,7 +3,6 @@ use crate::xml::Resourcetype;
|
||||
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
||||
use crate::xml::{TagList, multistatus::ResponseElement};
|
||||
use crate::{Error, Principal};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::http::header::{EntityTag, IfMatch, IfNoneMatch};
|
||||
use actix_web::{ResponseError, http::StatusCode};
|
||||
use itertools::Itertools;
|
||||
@@ -13,8 +12,10 @@ use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize}
|
||||
use std::str::FromStr;
|
||||
|
||||
mod methods;
|
||||
mod principal_uri;
|
||||
mod resource_service;
|
||||
|
||||
pub use principal_uri::PrincipalUri;
|
||||
pub use resource_service::*;
|
||||
|
||||
pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
|
||||
@@ -36,7 +37,7 @@ pub trait Resource: Clone + 'static {
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
principal_uri: &impl PrincipalUri,
|
||||
principal: &Self::Principal,
|
||||
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
|
||||
) -> Result<Self::Prop, Self::Error>;
|
||||
@@ -101,8 +102,8 @@ pub trait Resource: Clone + 'static {
|
||||
&self,
|
||||
path: &str,
|
||||
props: &[&str],
|
||||
principal_uri: &impl PrincipalUri,
|
||||
principal: &Self::Principal,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
|
||||
let mut props = props.to_vec();
|
||||
|
||||
@@ -154,7 +155,7 @@ pub trait Resource: Clone + 'static {
|
||||
|
||||
let prop_responses = valid_props
|
||||
.into_iter()
|
||||
.map(|prop| self.get_prop(rmap, principal, &prop))
|
||||
.map(|prop| self.get_prop(principal_uri, principal, &prop))
|
||||
.collect::<Result<Vec<_>, Self::Error>>()?;
|
||||
|
||||
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
|
||||
|
||||
3
crates/dav/src/resource/principal_uri.rs
Normal file
3
crates/dav/src/resource/principal_uri.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub trait PrincipalUri: 'static {
|
||||
fn principal_uri(&self, principal: &str) -> String;
|
||||
}
|
||||
@@ -1,17 +1,13 @@
|
||||
use super::methods::{route_delete, route_propfind, route_proppatch};
|
||||
use super::{PrincipalUri, Resource};
|
||||
use crate::Principal;
|
||||
use actix_web::dev::{AppService, HttpServiceFactory};
|
||||
use actix_web::error::UrlGenerationError;
|
||||
use actix_web::test::TestRequest;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::{ResponseError, dev::ResourceMap, http::Method, web};
|
||||
use actix_web::{ResponseError, http::Method, web};
|
||||
use async_trait::async_trait;
|
||||
use serde::Deserialize;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::Principal;
|
||||
|
||||
use super::Resource;
|
||||
use super::methods::{route_delete, route_propfind, route_proppatch};
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait ResourceService: Sized + 'static {
|
||||
type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>;
|
||||
@@ -19,6 +15,7 @@ pub trait ResourceService: Sized + 'static {
|
||||
type Resource: Resource<Error = Self::Error, Principal = Self::Principal>;
|
||||
type Error: ResponseError + From<crate::Error>;
|
||||
type Principal: Principal;
|
||||
type PrincipalUri: PrincipalUri;
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
@@ -31,6 +28,7 @@ pub trait ResourceService: Sized + 'static {
|
||||
&self,
|
||||
_path: &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error>;
|
||||
|
||||
async fn save_resource(
|
||||
&self,
|
||||
_path: &Self::PathComponents,
|
||||
@@ -38,6 +36,7 @@ pub trait ResourceService: Sized + 'static {
|
||||
) -> Result<(), Self::Error> {
|
||||
Err(crate::Error::Unauthorized.into())
|
||||
}
|
||||
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
_path: &Self::PathComponents,
|
||||
@@ -68,25 +67,6 @@ pub trait ResourceService: Sized + 'static {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NamedRoute {
|
||||
fn route_name() -> &'static str;
|
||||
|
||||
fn get_url<U, I>(rmap: &ResourceMap, elements: U) -> Result<String, UrlGenerationError>
|
||||
where
|
||||
U: IntoIterator<Item = I>,
|
||||
I: AsRef<str>,
|
||||
{
|
||||
Ok(rmap
|
||||
.url_for(
|
||||
&TestRequest::default().to_http_request(),
|
||||
Self::route_name(),
|
||||
elements,
|
||||
)?
|
||||
.path()
|
||||
.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ResourceServiceRoute<RS: ResourceService>(pub RS);
|
||||
|
||||
impl<RS: ResourceService> HttpServiceFactory for ResourceServiceRoute<RS> {
|
||||
|
||||
@@ -3,9 +3,8 @@ use crate::extensions::{
|
||||
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
|
||||
};
|
||||
use crate::privileges::UserPrivilegeSet;
|
||||
use crate::resource::{NamedRoute, Resource, ResourceService};
|
||||
use crate::resource::{PrincipalUri, Resource, ResourceService};
|
||||
use crate::xml::{Resourcetype, ResourcetypeInner};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use async_trait::async_trait;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -18,11 +17,7 @@ impl<PR: Resource, P: Principal> Default for RootResource<PR, P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<PR: Resource + NamedRoute, P: Principal> CommonPropertiesExtension for RootResource<PR, P> {
|
||||
type PrincipalResource = PR;
|
||||
}
|
||||
|
||||
impl<PR: Resource + NamedRoute, P: Principal> Resource for RootResource<PR, P> {
|
||||
impl<PR: Resource, P: Principal> Resource for RootResource<PR, P> {
|
||||
type Prop = CommonPropertiesProp;
|
||||
type Error = PR::Error;
|
||||
type Principal = P;
|
||||
@@ -36,11 +31,11 @@ impl<PR: Resource + NamedRoute, P: Principal> Resource for RootResource<PR, P> {
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
principal_uri: &impl PrincipalUri,
|
||||
user: &P,
|
||||
prop: &CommonPropertiesPropName,
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
CommonPropertiesExtension::get_prop(self, rmap, user, prop)
|
||||
CommonPropertiesExtension::get_prop(self, principal_uri, user, prop)
|
||||
}
|
||||
|
||||
fn get_user_privileges(&self, _user: &P) -> Result<UserPrivilegeSet, Self::Error> {
|
||||
@@ -49,23 +44,28 @@ impl<PR: Resource + NamedRoute, P: Principal> Resource for RootResource<PR, P> {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RootResourceService<PR: Resource, P: Principal>(PhantomData<PR>, PhantomData<P>);
|
||||
pub struct RootResourceService<PR: Resource, P: Principal, PURI: PrincipalUri>(
|
||||
PhantomData<PR>,
|
||||
PhantomData<P>,
|
||||
PhantomData<PURI>,
|
||||
);
|
||||
|
||||
impl<PR: Resource, P: Principal> Default for RootResourceService<PR, P> {
|
||||
impl<PR: Resource, P: Principal, PURI: PrincipalUri> Default for RootResourceService<PR, P, PURI> {
|
||||
fn default() -> Self {
|
||||
Self(PhantomData, PhantomData)
|
||||
Self(PhantomData, PhantomData, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<PR: Resource<Principal = P> + NamedRoute, P: Principal> ResourceService
|
||||
for RootResourceService<PR, P>
|
||||
impl<PR: Resource<Principal = P>, P: Principal, PURI: PrincipalUri> ResourceService
|
||||
for RootResourceService<PR, P, PURI>
|
||||
{
|
||||
type PathComponents = ();
|
||||
type MemberType = PR;
|
||||
type Resource = RootResource<PR, P>;
|
||||
type Error = PR::Error;
|
||||
type Principal = P;
|
||||
type PrincipalUri = PURI;
|
||||
|
||||
async fn get_resource(&self, _: &()) -> Result<Self::Resource, Self::Error> {
|
||||
Ok(RootResource::<PR, P>::default())
|
||||
|
||||
Reference in New Issue
Block a user