Refactor how ResourceService works

This commit is contained in:
Lennart
2025-01-04 14:24:01 +01:00
parent 40c8624703
commit c19e4745f9
16 changed files with 203 additions and 250 deletions

View File

@@ -2,6 +2,7 @@ use crate::privileges::UserPrivilege;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::Error;
use actix_web::web::Data;
use actix_web::web::Path;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
@@ -9,27 +10,25 @@ use actix_web::Responder;
use rustical_store::auth::User;
pub async fn route_delete<R: ResourceService>(
path_components: Path<R::PathComponents>,
path: Path<R::PathComponents>,
req: HttpRequest,
user: User,
resource_service: Data<R>,
) -> Result<impl Responder, R::Error> {
let path_components = path_components.into_inner();
let no_trash = req
.headers()
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
let resource_service = R::new(&req, path_components.clone()).await?;
let resource = resource_service.get_resource().await?;
let resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?;
if !privileges.has(&UserPrivilege::Write) {
// TODO: Actually the spec wants us to look whether we have unbind access in the parent
// collection
return Err(Error::Unauthorized.into());
}
resource_service.delete_resource(!no_trash).await?;
resource_service.delete_resource(&path, !no_trash).await?;
Ok(HttpResponse::Ok().body(""))
}

View File

@@ -9,6 +9,7 @@ use crate::xml::PropElement;
use crate::xml::PropfindElement;
use crate::xml::PropfindType;
use crate::Error;
use actix_web::web::Data;
use actix_web::web::Path;
use actix_web::HttpRequest;
use rustical_store::auth::User;
@@ -16,15 +17,16 @@ use rustical_xml::de::XmlDocument;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(path_components, req, root_span))]
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
#[allow(clippy::type_complexity)]
pub(crate) async fn route_propfind<R: ResourceService>(
path_components: Path<R::PathComponents>,
path: Path<R::PathComponents>,
body: String,
req: HttpRequest,
user: User,
depth: Depth,
root_span: RootSpan,
resource_service: Data<R>,
) -> Result<
MultistatusElement<
EitherProp<<R::Resource as Resource>::Prop, CommonPropertiesProp>,
@@ -32,9 +34,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
>,
R::Error,
> {
let resource_service = R::new(&req, path_components.into_inner()).await?;
let resource = resource_service.get_resource().await?;
let resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?;
if !privileges.has(&UserPrivilege::Read) {
return Err(Error::Unauthorized.into());
@@ -61,7 +61,10 @@ pub(crate) async fn route_propfind<R: ResourceService>(
let mut member_responses = Vec::new();
if depth != Depth::Zero {
for (path, member) in resource_service.get_members(req.resource_map()).await? {
for (path, member) in resource_service
.get_members(&path, req.resource_map())
.await?
{
member_responses.push(member.propfind(&path, &props, &user, req.resource_map())?);
}
}

View File

@@ -6,6 +6,7 @@ use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::Error;
use actix_web::http::StatusCode;
use actix_web::web::Data;
use actix_web::{web::Path, HttpRequest};
use rustical_store::auth::User;
use rustical_xml::Unparsed;
@@ -71,24 +72,23 @@ struct PropertyupdateElement<T: XmlDeserialize> {
operations: Vec<Operation<T>>,
}
#[instrument(parent = root_span.id(), skip(path, req, root_span))]
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
pub(crate) async fn route_proppatch<R: ResourceService>(
path: Path<R::PathComponents>,
body: String,
req: HttpRequest,
user: User,
root_span: RootSpan,
resource_service: Data<R>,
) -> Result<MultistatusElement<String, String>, R::Error> {
let path_components = path.into_inner();
let href = req.path().to_owned();
let resource_service = R::new(&req, path_components.clone()).await?;
// Extract operations
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 mut resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?;
if !privileges.has(&UserPrivilege::Write) {
return Err(Error::Unauthorized.into());
@@ -141,7 +141,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
if props_not_found.is_empty() && props_conflict.is_empty() {
// Only save if no errors occured
resource_service.save_resource(resource).await?;
resource_service.save_resource(&path, resource).await?;
}
Ok(MultistatusElement {

View File

@@ -1,6 +1,7 @@
use std::str::FromStr;
use actix_web::{dev::ResourceMap, http::Method, web, HttpRequest, ResponseError};
use actix_web::web::Data;
use actix_web::{dev::ResourceMap, http::Method, web, ResponseError};
use async_trait::async_trait;
use serde::Deserialize;
@@ -14,23 +15,30 @@ pub trait ResourceService: Sized + 'static {
type Resource: Resource<Error = Self::Error>;
type Error: ResponseError + From<crate::Error>;
async fn new(
req: &HttpRequest,
path_components: Self::PathComponents,
) -> Result<Self, Self::Error>;
async fn get_members(
&self,
path: &Self::PathComponents,
_rmap: &ResourceMap,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(vec![])
}
async fn get_resource(&self) -> Result<Self::Resource, Self::Error>;
async fn save_resource(&self, _file: Self::Resource) -> Result<(), Self::Error> {
async fn get_resource(
&self,
path: &Self::PathComponents,
) -> Result<Self::Resource, Self::Error>;
async fn save_resource(
&self,
path: &Self::PathComponents,
file: Self::Resource,
) -> Result<(), Self::Error> {
Err(crate::Error::Unauthorized.into())
}
async fn delete_resource(&self, _use_trashbin: bool) -> Result<(), Self::Error> {
async fn delete_resource(
&self,
path: &Self::PathComponents,
_use_trashbin: bool,
) -> Result<(), Self::Error> {
Err(crate::Error::Unauthorized.into())
}
@@ -40,9 +48,10 @@ pub trait ResourceService: Sized + 'static {
}
#[inline]
fn actix_resource() -> actix_web::Resource {
fn actix_resource(self) -> actix_web::Resource {
Self::actix_additional_routes(
web::resource("")
.app_data(Data::new(self))
.name(Self::resource_name())
.route(
web::method(Method::from_str("PROPFIND").unwrap()).to(route_propfind::<Self>),

View File

@@ -1,7 +1,6 @@
use crate::privileges::UserPrivilegeSet;
use crate::resource::{Resource, ResourceService};
use actix_web::dev::ResourceMap;
use actix_web::HttpRequest;
use async_trait::async_trait;
use rustical_store::auth::User;
use rustical_xml::{XmlDeserialize, XmlSerialize};
@@ -61,9 +60,14 @@ impl<PR: Resource> Resource for RootResource<PR> {
}
}
#[derive(Default)]
pub struct RootResourceService<PR: Resource>(PhantomData<PR>);
impl<PR: Resource> Default for RootResourceService<PR> {
fn default() -> Self {
Self(PhantomData)
}
}
#[async_trait(?Send)]
impl<PR: Resource> ResourceService for RootResourceService<PR> {
type PathComponents = ();
@@ -71,14 +75,7 @@ impl<PR: Resource> ResourceService for RootResourceService<PR> {
type Resource = RootResource<PR>;
type Error = PR::Error;
async fn new(
_req: &HttpRequest,
_path_components: Self::PathComponents,
) -> Result<Self, Self::Error> {
Ok(Self(Default::default()))
}
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
async fn get_resource(&self, _: &()) -> Result<Self::Resource, Self::Error> {
Ok(RootResource::<PR>::default())
}
}