use crate::Error; use crate::privileges::UserPrivilege; use crate::resource::Resource; use crate::resource::ResourceService; use axum::extract::{Path, State}; use axum_extra::TypedHeader; use headers::{IfMatch, IfNoneMatch}; use http::HeaderMap; pub(crate) async fn axum_route_delete( Path(path): Path, State(resource_service): State, principal: R::Principal, mut if_match: Option>, mut if_none_match: Option>, header_map: HeaderMap, ) -> Result<(), R::Error> { // https://github.com/hyperium/headers/issues/204 if !header_map.contains_key("If-Match") { if_match = None; } if !header_map.contains_key("If-None-Match") { if_none_match = None; } let no_trash = header_map .get("X-No-Trashbin") .map(|val| matches!(val.to_str(), Ok("1"))) .unwrap_or(false); route_delete( &path, &principal, &resource_service, no_trash, if_match.map(|hdr| hdr.0), if_none_match.map(|hdr| hdr.0), ) .await } pub async fn route_delete( path_components: &R::PathComponents, principal: &R::Principal, resource_service: &R, no_trash: bool, if_match: Option, if_none_match: Option, ) -> Result<(), R::Error> { let resource = resource_service.get_resource(path_components, true).await?; // Kind of a bodge since we don't get unbind from the parent let privileges = resource.get_user_privileges(principal)?; if !privileges.has(&UserPrivilege::WriteProperties) { return Err(Error::Unauthorized.into()); } if let Some(if_match) = if_match { dbg!(&if_match); if !resource.satisfies_if_match(&if_match) { // Precondition failed return Err(crate::Error::PreconditionFailed.into()); } } if let Some(if_none_match) = if_none_match { if resource.satisfies_if_none_match(&if_none_match) { // Precondition failed return Err(crate::Error::PreconditionFailed.into()); } } resource_service .delete_resource(path_components, !no_trash) .await?; Ok(()) }