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(""))
}