Refactoring to move authentication out of the ResourceService layer

This commit is contained in:
Lennart
2024-09-29 15:01:46 +02:00
parent f2f66c95d2
commit 3469252cd3
8 changed files with 29 additions and 52 deletions

View File

@@ -3,14 +3,12 @@ use actix_web::web::Path;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use actix_web::Responder;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_auth::CheckAuthentication;
pub async fn route_delete<A: CheckAuthentication, R: ResourceService>(
path_components: Path<R::PathComponents>,
req: HttpRequest,
auth: AuthInfoExtractor<A>,
) -> Result<impl Responder, R::Error> {
let auth_info = auth.inner;
let path_components = path_components.into_inner();
let no_trash = req
@@ -19,7 +17,7 @@ pub async fn route_delete<A: CheckAuthentication, R: ResourceService>(
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
let resource_service = R::new(&req, path_components.clone()).await?;
resource_service.delete_resource(!no_trash).await?;
Ok(HttpResponse::Ok().body(""))

View File

@@ -54,11 +54,10 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService>(
R::Error,
> {
debug!("{body}");
let auth_info = auth.inner;
let prefix = prefix.into_inner();
let path = req.path().to_owned();
let resource_service = R::new(&req, &auth_info, path_components.into_inner()).await?;
let resource_service = R::new(&req, path_components.into_inner()).await?;
// A request body is optional. If empty we MUST return all props
let propfind: PropfindElement = if !body.is_empty() {
@@ -83,12 +82,12 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService>(
let mut member_responses = Vec::new();
if depth != Depth::Zero {
for (path, member) in resource_service.get_members(auth_info).await? {
for (path, member) in resource_service.get_members().await? {
member_responses.push(member.propfind(&prefix, path, props.clone()).await?);
}
}
let resource = resource_service.get_resource().await?;
let resource = resource_service.get_resource(auth.inner.user_id).await?;
let response = resource.propfind(&prefix, path, props).await?;
Ok(MultistatusElement {

View File

@@ -53,10 +53,9 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService>(
req: HttpRequest,
auth: AuthInfoExtractor<A>,
) -> Result<MultistatusElement<PropstatWrapper<String>, PropstatWrapper<String>>, R::Error> {
let auth_info = auth.inner;
let path_components = path.into_inner();
let href = req.path().to_owned();
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
let resource_service = R::new(&req, path_components.clone()).await?;
debug!("{body}");
@@ -76,7 +75,7 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService>(
})
.collect();
let mut resource = resource_service.get_resource().await?;
let mut resource = resource_service.get_resource(auth.inner.user_id).await?;
let mut props_ok = Vec::new();
let mut props_conflict = Vec::new();

View File

@@ -5,7 +5,6 @@ use actix_web::{http::StatusCode, HttpRequest, ResponseError};
use async_trait::async_trait;
use core::fmt;
use itertools::Itertools;
use rustical_auth::AuthInfo;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum::VariantNames;
@@ -47,18 +46,14 @@ pub trait ResourceService: Sized {
async fn new(
req: &HttpRequest,
auth_info: &AuthInfo,
path_components: Self::PathComponents,
) -> Result<Self, Self::Error>;
async fn get_members(
&self,
_auth_info: AuthInfo,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
async fn get_members(&self) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(vec![])
}
async fn get_resource(&self) -> Result<Self::Resource, Self::Error>;
async fn get_resource(&self, principal: String) -> Result<Self::Resource, Self::Error>;
async fn save_resource(&self, file: Self::Resource) -> Result<(), Self::Error>;
async fn delete_resource(&self, _use_trashbin: bool) -> Result<(), Self::Error> {
Err(crate::Error::Unauthorized.into())