mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 14:42:30 +00:00
Refactor how ResourceService works
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
pub mod mkcalendar;
|
||||
pub mod post;
|
||||
pub mod report;
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
use actix_web::{web::Data, web::Path, Responder};
|
||||
use rustical_store::{auth::User, CalendarStore};
|
||||
use tracing::instrument;
|
||||
use tracing_actix_web::RootSpan;
|
||||
|
||||
#[instrument(parent = root_span.id(), skip(store, root_span))]
|
||||
pub async fn route_post<C: CalendarStore + ?Sized>(
|
||||
path: Path<(String, String)>,
|
||||
body: String,
|
||||
user: User,
|
||||
store: Data<C>,
|
||||
root_span: RootSpan,
|
||||
) -> impl Responder {
|
||||
"asd"
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
use super::methods::mkcalendar::route_mkcalendar;
|
||||
use super::methods::post::route_post;
|
||||
use super::methods::report::route_report_calendar;
|
||||
use super::prop::{
|
||||
SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
|
||||
SupportedReportSet, Transports,
|
||||
SupportedCalendarComponentSet, SupportedCalendarData, SupportedReportSet, Transports,
|
||||
};
|
||||
use crate::calendar_object::resource::CalendarObjectResource;
|
||||
use crate::principal::PrincipalResource;
|
||||
@@ -11,7 +9,6 @@ use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::web;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
@@ -27,8 +24,12 @@ use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
|
||||
cal_store: Arc<C>,
|
||||
principal: String,
|
||||
calendar_id: String,
|
||||
}
|
||||
|
||||
impl<C: CalendarStore + ?Sized> CalendarResourceService<C> {
|
||||
pub fn new(cal_store: Arc<C>) -> Self {
|
||||
Self { cal_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -243,10 +244,13 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
type Resource = CalendarResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal, cal_id): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Error> {
|
||||
let calendar = self
|
||||
.cal_store
|
||||
.get_calendar(&self.principal, &self.calendar_id)
|
||||
.get_calendar(principal, cal_id)
|
||||
.await
|
||||
.map_err(|_e| Error::NotFound)?;
|
||||
Ok(calendar.into())
|
||||
@@ -254,60 +258,45 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal, cal_id): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
Ok(self
|
||||
.cal_store
|
||||
.get_objects(&self.principal, &self.calendar_id)
|
||||
.get_objects(principal, cal_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|object| {
|
||||
(
|
||||
CalendarObjectResource::get_url(
|
||||
rmap,
|
||||
vec![&self.principal, &self.calendar_id, object.get_id()],
|
||||
)
|
||||
.unwrap(),
|
||||
CalendarObjectResource::get_url(rmap, vec![principal, cal_id, object.get_id()])
|
||||
.unwrap(),
|
||||
CalendarObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<C>>()
|
||||
.expect("no calendar store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
principal: path_components.0,
|
||||
calendar_id: path_components.1,
|
||||
cal_store,
|
||||
})
|
||||
}
|
||||
|
||||
async fn save_resource(&self, file: Self::Resource) -> Result<(), Self::Error> {
|
||||
async fn save_resource(
|
||||
&self,
|
||||
(principal, cal_id): &Self::PathComponents,
|
||||
file: Self::Resource,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.cal_store
|
||||
.update_calendar(
|
||||
self.principal.to_owned(),
|
||||
self.calendar_id.to_owned(),
|
||||
file.into(),
|
||||
)
|
||||
.update_calendar(principal.to_owned(), cal_id.to_owned(), file.into())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
(principal, cal_id): &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.cal_store
|
||||
.delete_calendar(&self.principal, &self.calendar_id, use_trashbin)
|
||||
.delete_calendar(principal, cal_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -319,6 +308,5 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
|
||||
res.route(report_method.to(route_report_calendar::<C>))
|
||||
.route(mkcalendar_method.to(route_mkcalendar::<C>))
|
||||
.post(route_post::<C>)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::methods::{get_event, put_event};
|
||||
use crate::{principal::PrincipalResource, Error};
|
||||
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::{
|
||||
@@ -15,9 +15,12 @@ use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
|
||||
cal_store: Arc<C>,
|
||||
principal: String,
|
||||
cal_id: String,
|
||||
object_id: String,
|
||||
}
|
||||
|
||||
impl<C: CalendarStore + ?Sized> CalendarObjectResourceService<C> {
|
||||
pub fn new(cal_store: Arc<C>) -> Self {
|
||||
Self { cal_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -117,44 +120,35 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
|
||||
type MemberType = CalendarObjectResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let CalendarObjectPathComponents {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
CalendarObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
object_id,
|
||||
} = path_components;
|
||||
|
||||
let cal_store = req
|
||||
.app_data::<Data<C>>()
|
||||
.expect("no calendar store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
cal_store,
|
||||
principal,
|
||||
cal_id,
|
||||
object_id,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
}: &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
let object = self
|
||||
.cal_store
|
||||
.get_object(&self.principal, &self.cal_id, &self.object_id)
|
||||
.get_object(principal, cal_id, object_id)
|
||||
.await?;
|
||||
Ok(CalendarObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
CalendarObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
object_id,
|
||||
}: &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.cal_store
|
||||
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
|
||||
.delete_object(principal, cal_id, object_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -52,17 +52,21 @@ pub fn configure_dav<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
||||
}),
|
||||
)
|
||||
.app_data(Data::from(store.clone()))
|
||||
.service(RootResourceService::<PrincipalResource>::actix_resource())
|
||||
.service(RootResourceService::<PrincipalResource>::default().actix_resource())
|
||||
.service(
|
||||
web::scope("/user").service(
|
||||
web::scope("/{principal}")
|
||||
.service(PrincipalResourceService::<C>::actix_resource())
|
||||
.service(PrincipalResourceService::<C>::new(store.clone()).actix_resource())
|
||||
.service(
|
||||
web::scope("/{calendar}")
|
||||
.service(CalendarResourceService::<C>::actix_resource())
|
||||
.service(
|
||||
CalendarResourceService::<C>::new(store.clone())
|
||||
.actix_resource(),
|
||||
)
|
||||
.service(
|
||||
web::scope("/{object}").service(
|
||||
CalendarObjectResourceService::<C>::actix_resource(),
|
||||
CalendarObjectResourceService::<C>::new(store.clone())
|
||||
.actix_resource(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use crate::calendar::resource::CalendarResource;
|
||||
use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::HttpRequest;
|
||||
use async_trait::async_trait;
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
@@ -14,10 +12,15 @@ use std::sync::Arc;
|
||||
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct PrincipalResourceService<C: CalendarStore + ?Sized> {
|
||||
principal: String,
|
||||
cal_store: Arc<C>,
|
||||
}
|
||||
|
||||
impl<C: CalendarStore + ?Sized> PrincipalResourceService<C> {
|
||||
pub fn new(cal_store: Arc<C>) -> Self {
|
||||
Self { cal_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PrincipalResource {
|
||||
principal: String,
|
||||
@@ -96,38 +99,26 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResourceService<C>
|
||||
type Resource = PrincipalResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
(principal,): Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<C>>()
|
||||
.expect("no calendar store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
cal_store,
|
||||
principal,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
Ok(PrincipalResource {
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
let calendars = self.cal_store.get_calendars(&self.principal).await?;
|
||||
let calendars = self.cal_store.get_calendars(principal).await?;
|
||||
Ok(calendars
|
||||
.into_iter()
|
||||
.map(|cal| {
|
||||
(
|
||||
CalendarResource::get_url(rmap, vec![&self.principal, &cal.id]).unwrap(),
|
||||
CalendarResource::get_url(rmap, vec![principal, &cal.id]).unwrap(),
|
||||
cal.into(),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -19,7 +19,7 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path.into_inner();
|
||||
|
||||
@@ -27,12 +27,14 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
|
||||
let addressbook = store.get_addressbook(&principal, &cal_id).await?;
|
||||
let addressbook = store.get_addressbook(&principal, &addressbook_id).await?;
|
||||
if user.id != addressbook.principal {
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
|
||||
let object = store.get_object(&principal, &cal_id, &object_id).await?;
|
||||
let object = store
|
||||
.get_object(&principal, &addressbook_id, &object_id)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.insert_header(("ETag", object.get_etag()))
|
||||
@@ -51,7 +53,7 @@ pub async fn put_object<AS: AddressbookStore + ?Sized>(
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id: addressbook_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path.into_inner();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{principal::PrincipalResource, Error};
|
||||
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::{
|
||||
@@ -16,9 +16,12 @@ use super::methods::{get_object, put_object};
|
||||
|
||||
pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
|
||||
addr_store: Arc<AS>,
|
||||
principal: String,
|
||||
cal_id: String,
|
||||
object_id: String,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> AddressObjectResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -89,7 +92,7 @@ impl Resource for AddressObjectResource {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddressObjectPathComponents {
|
||||
pub principal: String,
|
||||
pub cal_id: String,
|
||||
pub addressbook_id: String,
|
||||
pub object_id: String,
|
||||
}
|
||||
|
||||
@@ -99,13 +102,13 @@ impl<'de> Deserialize<'de> for AddressObjectPathComponents {
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
type Inner = (String, String, String);
|
||||
let (principal, calendar, mut object) = Inner::deserialize(deserializer)?;
|
||||
let (principal, addressbook_id, mut object) = Inner::deserialize(deserializer)?;
|
||||
if object.ends_with(".vcf") {
|
||||
object.truncate(object.len() - 4);
|
||||
}
|
||||
Ok(Self {
|
||||
principal,
|
||||
cal_id: calendar,
|
||||
addressbook_id,
|
||||
object_id: object,
|
||||
})
|
||||
}
|
||||
@@ -118,44 +121,35 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressObjectResourceSer
|
||||
type MemberType = AddressObjectResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let AddressObjectPathComponents {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
AddressObjectPathComponents {
|
||||
principal,
|
||||
cal_id,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
} = path_components;
|
||||
|
||||
let addr_store = req
|
||||
.app_data::<Data<AS>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
addr_store,
|
||||
principal,
|
||||
cal_id,
|
||||
object_id,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
}: &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
let object = self
|
||||
.addr_store
|
||||
.get_object(&self.principal, &self.cal_id, &self.object_id)
|
||||
.get_object(principal, addressbook_id, object_id)
|
||||
.await?;
|
||||
Ok(AddressObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
AddressObjectPathComponents {
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
}: &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
|
||||
.delete_object(principal, addressbook_id, object_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::web;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
@@ -21,8 +20,12 @@ use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
|
||||
addr_store: Arc<AS>,
|
||||
principal: String,
|
||||
addressbook_id: String,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> AddressbookResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
|
||||
@@ -157,10 +160,13 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
type Resource = AddressbookResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Error> {
|
||||
let addressbook = self
|
||||
.addr_store
|
||||
.get_addressbook(&self.principal, &self.addressbook_id)
|
||||
.get_addressbook(principal, addressbook_id)
|
||||
.await
|
||||
.map_err(|_e| Error::NotFound)?;
|
||||
Ok(addressbook.into())
|
||||
@@ -168,60 +174,48 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
Ok(self
|
||||
.addr_store
|
||||
.get_objects(&self.principal, &self.addressbook_id)
|
||||
.get_objects(principal, addressbook_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|object| {
|
||||
(
|
||||
AddressObjectResource::get_url(
|
||||
rmap,
|
||||
vec![&self.principal, &self.addressbook_id, object.get_id()],
|
||||
vec![principal, addressbook_id, object.get_id()],
|
||||
)
|
||||
.unwrap(),
|
||||
AddressObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let addr_store = req
|
||||
.app_data::<Data<AS>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
principal: path_components.0,
|
||||
addressbook_id: path_components.1,
|
||||
addr_store,
|
||||
})
|
||||
}
|
||||
|
||||
async fn save_resource(&self, file: Self::Resource) -> Result<(), Self::Error> {
|
||||
async fn save_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
file: Self::Resource,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.update_addressbook(
|
||||
self.principal.to_owned(),
|
||||
self.addressbook_id.to_owned(),
|
||||
file.into(),
|
||||
)
|
||||
.update_addressbook(principal.to_owned(), addressbook_id.to_owned(), file.into())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
|
||||
async fn delete_resource(
|
||||
&self,
|
||||
(principal, addressbook_id): &Self::PathComponents,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.addr_store
|
||||
.delete_addressbook(&self.principal, &self.addressbook_id, use_trashbin)
|
||||
.delete_addressbook(principal, addressbook_id, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -57,17 +57,21 @@ pub fn configure_dav<AP: AuthenticationProvider, A: AddressbookStore + ?Sized>(
|
||||
}),
|
||||
)
|
||||
.app_data(Data::from(store.clone()))
|
||||
.service(RootResourceService::<PrincipalResource>::actix_resource())
|
||||
.service(RootResourceService::<PrincipalResource>::default().actix_resource())
|
||||
.service(
|
||||
web::scope("/user").service(
|
||||
web::scope("/{principal}")
|
||||
.service(PrincipalResourceService::<A>::actix_resource())
|
||||
.service(PrincipalResourceService::<A>::new(store.clone()).actix_resource())
|
||||
.service(
|
||||
web::scope("/{addressbook}")
|
||||
.service(AddressbookResourceService::<A>::actix_resource())
|
||||
.service(
|
||||
AddressbookResourceService::<A>::new(store.clone())
|
||||
.actix_resource(),
|
||||
)
|
||||
.service(
|
||||
web::scope("/{object}").service(
|
||||
AddressObjectResourceService::<A>::actix_resource(),
|
||||
AddressObjectResourceService::<A>::new(store.clone())
|
||||
.actix_resource(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use crate::addressbook::resource::AddressbookResource;
|
||||
use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::HttpRequest;
|
||||
use async_trait::async_trait;
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
@@ -14,10 +12,15 @@ use std::sync::Arc;
|
||||
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
|
||||
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
|
||||
principal: String,
|
||||
addr_store: Arc<A>,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore + ?Sized> PrincipalResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PrincipalResource {
|
||||
principal: String,
|
||||
@@ -97,39 +100,26 @@ impl<A: AddressbookStore + ?Sized> ResourceService for PrincipalResourceService<
|
||||
type Resource = PrincipalResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: &HttpRequest,
|
||||
(principal,): Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let addr_store = req
|
||||
.app_data::<Data<A>>()
|
||||
.expect("no addressbook store in app_data!")
|
||||
.clone()
|
||||
.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
addr_store,
|
||||
principal,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Self::Error> {
|
||||
async fn get_resource(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
Ok(PrincipalResource {
|
||||
principal: self.principal.to_owned(),
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
let addressbooks = self.addr_store.get_addressbooks(&self.principal).await?;
|
||||
let addressbooks = self.addr_store.get_addressbooks(principal).await?;
|
||||
Ok(addressbooks
|
||||
.into_iter()
|
||||
.map(|addressbook| {
|
||||
(
|
||||
AddressbookResource::get_url(rmap, vec![&self.principal, &addressbook.id])
|
||||
.unwrap(),
|
||||
AddressbookResource::get_url(rmap, vec![principal, &addressbook.id]).unwrap(),
|
||||
addressbook.into(),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -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>),
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user