mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 18:12:27 +00:00
175 lines
5.4 KiB
Rust
175 lines
5.4 KiB
Rust
use crate::privileges::UserPrivilegeSet;
|
|
use crate::xml::Resourcetype;
|
|
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
|
|
use crate::xml::{TagList, multistatus::ResponseElement};
|
|
use crate::{Error, Principal};
|
|
use headers::{ETag, IfMatch, IfNoneMatch};
|
|
use http::StatusCode;
|
|
use itertools::Itertools;
|
|
use quick_xml::name::Namespace;
|
|
pub use resource_service::ResourceService;
|
|
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
|
use std::collections::HashSet;
|
|
use std::str::FromStr;
|
|
|
|
mod methods;
|
|
mod principal_uri;
|
|
mod resource_service;
|
|
|
|
pub use principal_uri::PrincipalUri;
|
|
pub use resource_service::*;
|
|
|
|
pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
|
|
impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
|
|
|
|
pub trait ResourcePropName: FromStr {}
|
|
impl<T: FromStr> ResourcePropName for T {}
|
|
|
|
pub trait Resource: Clone + 'static {
|
|
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
|
|
type Error: From<crate::Error>;
|
|
type Principal: Principal;
|
|
|
|
fn get_resourcetype(&self) -> Resourcetype;
|
|
|
|
fn list_props() -> Vec<(Option<Namespace<'static>>, &'static str)> {
|
|
Self::Prop::variant_names()
|
|
}
|
|
|
|
fn get_prop(
|
|
&self,
|
|
principal_uri: &impl PrincipalUri,
|
|
principal: &Self::Principal,
|
|
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
|
|
) -> Result<Self::Prop, Self::Error>;
|
|
|
|
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
|
|
Err(crate::Error::PropReadOnly)
|
|
}
|
|
|
|
fn remove_prop(
|
|
&mut self,
|
|
_prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
|
|
) -> Result<(), crate::Error> {
|
|
Err(crate::Error::PropReadOnly)
|
|
}
|
|
|
|
fn get_owner(&self) -> Option<&str> {
|
|
None
|
|
}
|
|
|
|
fn get_etag(&self) -> Option<String> {
|
|
None
|
|
}
|
|
|
|
fn satisfies_if_match(&self, if_match: &IfMatch) -> bool {
|
|
if let Some(etag) = self.get_etag() {
|
|
if let Ok(etag) = ETag::from_str(&etag) {
|
|
if_match.precondition_passes(&etag)
|
|
} else {
|
|
if_match.is_any()
|
|
}
|
|
} else {
|
|
if_match.is_any()
|
|
}
|
|
}
|
|
|
|
fn satisfies_if_none_match(&self, if_none_match: &IfNoneMatch) -> bool {
|
|
if let Some(etag) = self.get_etag() {
|
|
if let Ok(etag) = ETag::from_str(&etag) {
|
|
if_none_match.precondition_passes(&etag)
|
|
} else {
|
|
if_none_match != &IfNoneMatch::any()
|
|
}
|
|
} else {
|
|
if_none_match != &IfNoneMatch::any()
|
|
}
|
|
}
|
|
|
|
fn get_user_privileges(
|
|
&self,
|
|
principal: &Self::Principal,
|
|
) -> Result<UserPrivilegeSet, Self::Error>;
|
|
|
|
fn propfind(
|
|
&self,
|
|
path: &str,
|
|
props: &[&str],
|
|
principal_uri: &impl PrincipalUri,
|
|
principal: &Self::Principal,
|
|
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
|
|
let mut props: HashSet<&str> = props.iter().cloned().collect();
|
|
|
|
if props.contains(&"propname") {
|
|
if props.len() != 1 {
|
|
// propname MUST be the only queried prop per spec
|
|
return Err(
|
|
Error::BadRequest("propname MUST be the only queried prop".to_owned()).into(),
|
|
);
|
|
}
|
|
|
|
let props = Self::list_props()
|
|
.into_iter()
|
|
.map(|(ns, tag)| (ns.to_owned(), tag.to_string()))
|
|
.collect_vec();
|
|
|
|
return Ok(ResponseElement {
|
|
href: path.to_owned(),
|
|
propstat: vec![PropstatWrapper::TagList(PropstatElement {
|
|
prop: TagList::from(props),
|
|
status: StatusCode::OK,
|
|
})],
|
|
..Default::default()
|
|
});
|
|
}
|
|
|
|
if props.contains(&"allprop") {
|
|
if props.len() != 1 {
|
|
// allprop MUST be the only queried prop per spec
|
|
return Err(
|
|
Error::BadRequest("allprop MUST be the only queried prop".to_owned()).into(),
|
|
);
|
|
}
|
|
props = Self::list_props()
|
|
.into_iter()
|
|
.map(|(_ns, tag)| tag)
|
|
.collect();
|
|
}
|
|
|
|
let mut valid_props = vec![];
|
|
let mut invalid_props = vec![];
|
|
for prop in props {
|
|
if let Ok(valid_prop) = <Self::Prop as EnumUnitVariants>::UnitVariants::from_str(prop) {
|
|
valid_props.push(valid_prop);
|
|
} else {
|
|
invalid_props.push(prop.to_string())
|
|
}
|
|
}
|
|
|
|
let prop_responses = valid_props
|
|
.into_iter()
|
|
.map(|prop| self.get_prop(principal_uri, principal, &prop))
|
|
.collect::<Result<Vec<_>, Self::Error>>()?;
|
|
|
|
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
|
|
status: StatusCode::OK,
|
|
prop: PropTagWrapper(prop_responses),
|
|
})];
|
|
if !invalid_props.is_empty() {
|
|
propstats.push(PropstatWrapper::TagList(PropstatElement {
|
|
status: StatusCode::NOT_FOUND,
|
|
prop: invalid_props
|
|
.into_iter()
|
|
.map(|tag| (None, tag))
|
|
.collect_vec()
|
|
.into(),
|
|
}));
|
|
}
|
|
Ok(ResponseElement {
|
|
href: path.to_owned(),
|
|
propstat: propstats,
|
|
..Default::default()
|
|
})
|
|
}
|
|
}
|