From 6f4bc4ba7bac83e114bc9300473cc70f05bffcac Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Sun, 30 Jun 2024 19:44:13 +0200 Subject: [PATCH] Move DELETE method to Resource framework --- README.md | 2 +- crates/caldav/src/calendar/methods/delete.rs | 36 -------------------- crates/caldav/src/calendar/methods/mod.rs | 1 - crates/caldav/src/calendar/resource.rs | 9 +++++ crates/caldav/src/event/methods.rs | 31 ----------------- crates/caldav/src/event/resource.rs | 15 +++++++- crates/caldav/src/lib.rs | 14 ++++---- crates/dav/src/delete.rs | 26 ++++++++++++++ crates/dav/src/lib.rs | 1 + crates/dav/src/resource.rs | 6 ++-- 10 files changed, 62 insertions(+), 79 deletions(-) delete mode 100644 crates/caldav/src/calendar/methods/delete.rs create mode 100644 crates/dav/src/delete.rs diff --git a/README.md b/README.md index 49a7efa..8c12326 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ a calendar server - [ ] ICS parsing - [x] Datetime parsing - [x] Implement PROPPATCH -- [ ] Auth +- [ ] Auth (There currently is no authentication at all in place for some routes) - [ ] Access control - [ ] preparation for different principal types (groups) - [ ] authentication rewrite? (argon2 is very slow for each request) diff --git a/crates/caldav/src/calendar/methods/delete.rs b/crates/caldav/src/calendar/methods/delete.rs deleted file mode 100644 index 5997f2b..0000000 --- a/crates/caldav/src/calendar/methods/delete.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::CalDavContext; -use crate::Error; -use actix_web::HttpRequest; -use actix_web::{ - web::{Data, Path}, - HttpResponse, -}; -use rustical_auth::{AuthInfoExtractor, CheckAuthentication}; -use rustical_store::CalendarStore; - -pub async fn route_delete_calendar( - context: Data>, - path: Path<(String, String)>, - auth: AuthInfoExtractor, - req: HttpRequest, -) -> Result { - let (principal, cid) = path.into_inner(); - if principal != auth.inner.user_id { - return Err(Error::Unauthorized); - } - - let no_trash = req - .headers() - .get("X-No-Trashbin") - .map(|val| matches!(val.to_str(), Ok("1"))) - .unwrap_or(false); - - context - .store - .write() - .await - .delete_calendar(&principal, &cid, !no_trash) - .await?; - - Ok(HttpResponse::Ok().body("")) -} diff --git a/crates/caldav/src/calendar/methods/mod.rs b/crates/caldav/src/calendar/methods/mod.rs index 35d5a85..436ff41 100644 --- a/crates/caldav/src/calendar/methods/mod.rs +++ b/crates/caldav/src/calendar/methods/mod.rs @@ -1,3 +1,2 @@ -pub mod delete; pub mod mkcalendar; pub mod report; diff --git a/crates/caldav/src/calendar/resource.rs b/crates/caldav/src/calendar/resource.rs index ba218ae..45127cd 100644 --- a/crates/caldav/src/calendar/resource.rs +++ b/crates/caldav/src/calendar/resource.rs @@ -289,4 +289,13 @@ impl ResourceService for CalendarResource { .await?; Ok(()) } + + async fn delete_file(&self, use_trashbin: bool) -> Result<(), Self::Error> { + self.cal_store + .write() + .await + .delete_calendar(&self.principal, &self.calendar_id, use_trashbin) + .await?; + Ok(()) + } } diff --git a/crates/caldav/src/event/methods.rs b/crates/caldav/src/event/methods.rs index 19b4b47..5f1e68c 100644 --- a/crates/caldav/src/event/methods.rs +++ b/crates/caldav/src/event/methods.rs @@ -8,37 +8,6 @@ use actix_web::HttpResponse; use rustical_auth::{AuthInfoExtractor, CheckAuthentication}; use rustical_store::CalendarStore; -pub async fn delete_event( - context: Data>, - path: Path<(String, String, String)>, - auth: AuthInfoExtractor, - req: HttpRequest, -) -> Result { - let (principal, mut cid, uid) = path.into_inner(); - - if auth.inner.user_id != principal { - return Ok(HttpResponse::Unauthorized().body("")); - } - - if cid.ends_with(".ics") { - cid.truncate(cid.len() - 4); - } - let no_trash = req - .headers() - .get("X-No-Trashbin") - .map(|val| matches!(val.to_str(), Ok("1"))) - .unwrap_or(false); - - context - .store - .write() - .await - .delete_event(&principal, &cid, &uid, !no_trash) - .await?; - - Ok(HttpResponse::Ok().body("")) -} - pub async fn get_event( context: Data>, path: Path<(String, String, String)>, diff --git a/crates/caldav/src/event/resource.rs b/crates/caldav/src/event/resource.rs index 2fab1a9..b1f253b 100644 --- a/crates/caldav/src/event/resource.rs +++ b/crates/caldav/src/event/resource.rs @@ -79,7 +79,11 @@ impl ResourceService for EventResource { _auth_info: &AuthInfo, path_components: Self::PathComponents, ) -> Result { - let (principal, cid, uid) = path_components; + let (principal, cid, mut uid) = path_components; + + if uid.ends_with(".ics") { + uid.truncate(uid.len() - 4); + } let cal_store = req .app_data::>>() @@ -109,4 +113,13 @@ impl ResourceService for EventResource { async fn save_file(&self, _file: Self::File) -> Result<(), Self::Error> { Err(Error::NotImplemented) } + + async fn delete_file(&self, use_trashbin: bool) -> Result<(), Self::Error> { + self.cal_store + .write() + .await + .delete_event(&self.principal, &self.cid, &self.uid, use_trashbin) + .await?; + Ok(()) + } } diff --git a/crates/caldav/src/lib.rs b/crates/caldav/src/lib.rs index 35e1a08..3c11d17 100644 --- a/crates/caldav/src/lib.rs +++ b/crates/caldav/src/lib.rs @@ -6,6 +6,7 @@ use event::resource::EventResource; use principal::PrincipalResource; use root::RootResource; use rustical_auth::CheckAuthentication; +use rustical_dav::delete::route_delete; use rustical_dav::propfind::{route_propfind, ServicePrefix}; use rustical_dav::proppatch::route_proppatch; use rustical_store::CalendarStore; @@ -81,14 +82,13 @@ pub fn configure_dav( proppatch_method() .to(route_proppatch::>), ) + .route( + web::method(Method::DELETE) + .to(route_delete::>), + ) .route(mkcalendar_method().to( calendar::methods::mkcalendar::route_mkcol_calendar::, - )) - .route( - web::method(Method::DELETE).to( - calendar::methods::delete::route_delete_calendar::, - ), - ), + )), ) .service( web::resource("/{event}") @@ -98,7 +98,7 @@ pub fn configure_dav( ) .route( web::method(Method::DELETE) - .to(event::methods::delete_event::), + .to(route_delete::>), ) .route( web::method(Method::GET).to(event::methods::get_event::), diff --git a/crates/dav/src/delete.rs b/crates/dav/src/delete.rs new file mode 100644 index 0000000..33a8e69 --- /dev/null +++ b/crates/dav/src/delete.rs @@ -0,0 +1,26 @@ +use crate::resource::ResourceService; +use actix_web::web::Path; +use actix_web::HttpRequest; +use actix_web::HttpResponse; +use actix_web::Responder; +use rustical_auth::{AuthInfoExtractor, CheckAuthentication}; + +pub async fn route_delete( + path_components: Path, + req: HttpRequest, + auth: AuthInfoExtractor, +) -> Result { + let auth_info = auth.inner; + 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, &auth_info, path_components.clone()).await?; + resource_service.delete_file(!no_trash).await?; + + Ok(HttpResponse::Ok().body("")) +} diff --git a/crates/dav/src/lib.rs b/crates/dav/src/lib.rs index 08450a4..793e7ef 100644 --- a/crates/dav/src/lib.rs +++ b/crates/dav/src/lib.rs @@ -1,3 +1,4 @@ +pub mod delete; pub mod depth_extractor; pub mod error; pub mod namespace; diff --git a/crates/dav/src/resource.rs b/crates/dav/src/resource.rs index c8ee6bb..da9c525 100644 --- a/crates/dav/src/resource.rs +++ b/crates/dav/src/resource.rs @@ -51,8 +51,6 @@ pub trait ResourceService: Sized { path_components: Self::PathComponents, ) -> Result; - async fn get_file(&self) -> Result; - async fn get_members( &self, _auth_info: AuthInfo, @@ -60,7 +58,11 @@ pub trait ResourceService: Sized { Ok(vec![]) } + async fn get_file(&self) -> Result; async fn save_file(&self, file: Self::File) -> Result<(), Self::Error>; + async fn delete_file(&self, _use_trashbin: bool) -> Result<(), Self::Error> { + Err(crate::Error::Unauthorized.into()) + } } #[derive(Serialize)]