rustical_dav/resource/
mod.rs1use crate::privileges::UserPrivilegeSet;
2use crate::xml::Resourcetype;
3use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
4use crate::xml::{TagList, multistatus::ResponseElement};
5use crate::{Error, Principal};
6use actix_web::dev::ResourceMap;
7use actix_web::http::header::{EntityTag, IfMatch, IfNoneMatch};
8use actix_web::{ResponseError, http::StatusCode};
9use itertools::Itertools;
10use quick_xml::name::Namespace;
11pub use resource_service::ResourceService;
12use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
13use std::str::FromStr;
14
15mod methods;
16mod resource_service;
17
18pub use resource_service::*;
19
20pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
21impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
22
23pub trait ResourcePropName: FromStr {}
24impl<T: FromStr> ResourcePropName for T {}
25
26pub trait Resource: Clone + 'static {
27 type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
28 type Error: ResponseError + From<crate::Error>;
29 type Principal: Principal;
30
31 fn get_resourcetype(&self) -> Resourcetype;
32
33 fn list_props() -> Vec<(Option<Namespace<'static>>, &'static str)> {
34 Self::Prop::variant_names()
35 }
36
37 fn get_prop(
38 &self,
39 rmap: &ResourceMap,
40 principal: &Self::Principal,
41 prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
42 ) -> Result<Self::Prop, Self::Error>;
43
44 fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
45 Err(crate::Error::PropReadOnly)
46 }
47
48 fn remove_prop(
49 &mut self,
50 _prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
51 ) -> Result<(), crate::Error> {
52 Err(crate::Error::PropReadOnly)
53 }
54
55 fn get_owner(&self) -> Option<&str> {
56 None
57 }
58
59 fn get_etag(&self) -> Option<String> {
60 None
61 }
62
63 fn satisfies_if_match(&self, if_match: &IfMatch) -> bool {
64 match if_match {
65 IfMatch::Any => true,
66 IfMatch::Items(items) if items.is_empty() => true,
69 IfMatch::Items(items) => {
70 if let Some(etag) = self.get_etag() {
71 let etag = EntityTag::new_strong(etag.to_owned());
72 return items.iter().any(|item| item.strong_eq(&etag));
73 }
74 false
75 }
76 }
77 }
78
79 fn satisfies_if_none_match(&self, if_none_match: &IfNoneMatch) -> bool {
80 match if_none_match {
81 IfNoneMatch::Any => false,
82 IfNoneMatch::Items(items) if items.is_empty() => false,
85 IfNoneMatch::Items(items) => {
86 if let Some(etag) = self.get_etag() {
87 let etag = EntityTag::new_strong(etag.to_owned());
88 return items.iter().all(|item| item.strong_ne(&etag));
89 }
90 true
91 }
92 }
93 }
94
95 fn get_user_privileges(
96 &self,
97 principal: &Self::Principal,
98 ) -> Result<UserPrivilegeSet, Self::Error>;
99
100 fn propfind(
101 &self,
102 path: &str,
103 props: &[&str],
104 principal: &Self::Principal,
105 rmap: &ResourceMap,
106 ) -> Result<ResponseElement<Self::Prop>, Self::Error> {
107 let mut props = props.to_vec();
108
109 if props.contains(&"propname") {
110 if props.len() != 1 {
111 return Err(
113 Error::BadRequest("propname MUST be the only queried prop".to_owned()).into(),
114 );
115 }
116
117 let props = Self::list_props()
118 .into_iter()
119 .map(|(ns, tag)| (ns.to_owned(), tag.to_string()))
120 .collect_vec();
121
122 return Ok(ResponseElement {
123 href: path.to_owned(),
124 propstat: vec![PropstatWrapper::TagList(PropstatElement {
125 prop: TagList::from(props),
126 status: StatusCode::OK,
127 })],
128 ..Default::default()
129 });
130 }
131
132 if props.contains(&"allprop") {
133 if props.len() != 1 {
134 return Err(
136 Error::BadRequest("allprop MUST be the only queried prop".to_owned()).into(),
137 );
138 }
139 props = Self::list_props()
140 .into_iter()
141 .map(|(_ns, tag)| tag)
142 .collect();
143 }
144
145 let mut valid_props = vec![];
146 let mut invalid_props = vec![];
147 for prop in props {
148 if let Ok(valid_prop) = <Self::Prop as EnumUnitVariants>::UnitVariants::from_str(prop) {
149 valid_props.push(valid_prop);
150 } else {
151 invalid_props.push(prop.to_string())
152 }
153 }
154
155 let prop_responses = valid_props
156 .into_iter()
157 .map(|prop| self.get_prop(rmap, principal, &prop))
158 .collect::<Result<Vec<_>, Self::Error>>()?;
159
160 let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
161 status: StatusCode::OK,
162 prop: PropTagWrapper(prop_responses),
163 })];
164 if !invalid_props.is_empty() {
165 propstats.push(PropstatWrapper::TagList(PropstatElement {
166 status: StatusCode::NOT_FOUND,
167 prop: invalid_props
168 .into_iter()
169 .map(|tag| (None, tag))
170 .collect_vec()
171 .into(),
172 }));
173 }
174 Ok(ResponseElement {
175 href: path.to_owned(),
176 propstat: propstats,
177 ..Default::default()
178 })
179 }
180}