mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 14:02:29 +00:00
Refactor how ResourceService works
This commit is contained in:
@@ -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(""))
|
||||
}
|
||||
|
||||
@@ -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())?);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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>),
|
||||
|
||||
Reference in New Issue
Block a user