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 {