dav: Introduce resource extension for common properties

This commit is contained in:
Lennart
2024-11-03 22:32:21 +01:00
parent 31c7143dd8
commit 0c8d339ced
16 changed files with 492 additions and 262 deletions

View File

@@ -12,7 +12,11 @@ use actix_web::http::Method;
use actix_web::web;
use actix_web::{web::Data, HttpRequest};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use derive_more::derive::{From, Into, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
@@ -30,11 +34,10 @@ pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
pub calendar_id: String,
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarPropName {
Resourcetype,
CurrentUserPrincipal,
Owner,
Displayname,
CalendarColor,
@@ -44,14 +47,17 @@ pub enum CalendarPropName {
SupportedCalendarComponentSet,
SupportedCalendarData,
Getcontenttype,
// CurrentUserPrivilegeSet,
MaxResourceSize,
SupportedReportSet,
SyncToken,
Getctag,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum CalendarProp {
// WebDAV (RFC 2518)
@@ -59,12 +65,8 @@ pub enum CalendarProp {
Displayname(Option<String>),
Getcontenttype(String),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
// CurrentUserPrivilegeSet(UserPrivilegeSet),
// CalDAV (RFC 4791)
#[serde(rename = "IC:calendar-color", alias = "calendar-color")]
@@ -93,7 +95,13 @@ pub enum CalendarProp {
// Didn't find the spec
Getctag(String),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -111,6 +119,12 @@ impl Resource for CalendarResource {
type Prop = CalendarProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -119,11 +133,6 @@ impl Resource for CalendarResource {
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
CalendarPropName::Resourcetype => CalendarProp::Resourcetype(Resourcetype::default()),
CalendarPropName::CurrentUserPrincipal => {
CalendarProp::CurrentUserPrincipal(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
))
}
CalendarPropName::Owner => CalendarProp::Owner(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
)),
@@ -166,13 +175,13 @@ impl Resource for CalendarResource {
}
CalendarPropName::SyncToken => CalendarProp::SyncToken(self.0.format_synctoken()),
CalendarPropName::Getctag => CalendarProp::Getctag(self.0.format_synctoken()),
_ => panic!("we shouldn't end up here"),
})
}
fn set_prop(&mut self, prop: Self::Prop) -> Result<(), rustical_dav::Error> {
match prop {
CalendarProp::Resourcetype(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::CurrentUserPrincipal(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Displayname(displayname) => {
self.0.displayname = displayname;
@@ -205,13 +214,13 @@ impl Resource for CalendarResource {
CalendarProp::SyncToken(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
_ => panic!("we shouldn't end up here"),
}
}
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> {
match prop {
CalendarPropName::Resourcetype => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::CurrentUserPrincipal => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Owner => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Displayname => {
self.0.displayname = None;
@@ -243,6 +252,7 @@ impl Resource for CalendarResource {
CalendarPropName::SupportedReportSet => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
_ => panic!("we shouldn't end up here"),
}
}

View File

@@ -2,8 +2,10 @@ use super::methods::{get_event, put_event};
use crate::{principal::PrincipalResource, Error};
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use derive_more::derive::{From, Into, TryInto};
use rustical_dav::{
extension::BoxedExtension,
extensions::{CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName},
privileges::UserPrivilegeSet,
resource::{InvalidProperty, Resource, ResourceService},
xml::HrefElement,
@@ -21,18 +23,20 @@ pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
pub object_id: String,
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarObjectPropName {
Getetag,
CalendarData,
Getcontenttype,
CurrentUserPrincipal,
Owner,
CurrentUserPrivilegeSet,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum CalendarObjectProp {
// WebDAV (RFC 2518)
@@ -43,13 +47,15 @@ pub enum CalendarObjectProp {
#[serde(rename = "C:calendar-data")]
CalendarData(String),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
CurrentUserPrivilegeSet(UserPrivilegeSet),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -70,6 +76,12 @@ impl Resource for CalendarObjectResource {
type Prop = CalendarObjectProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -84,17 +96,10 @@ impl Resource for CalendarObjectResource {
CalendarObjectPropName::Getcontenttype => {
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
}
CalendarObjectPropName::CurrentUserPrincipal => {
CalendarObjectProp::CurrentUserPrincipal(HrefElement::new(
PrincipalResource::get_principal_url(rmap, &user.id),
))
}
CalendarObjectPropName::Owner => CalendarObjectProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.principal).into(),
),
CalendarObjectPropName::CurrentUserPrivilegeSet => {
CalendarObjectProp::CurrentUserPrivilegeSet(self.get_user_privileges(&user)?)
}
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -4,6 +4,11 @@ use actix_web::dev::ResourceMap;
use actix_web::web::Data;
use actix_web::HttpRequest;
use async_trait::async_trait;
use derive_more::derive::{From, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
@@ -30,7 +35,7 @@ pub struct Resourcetype {
collection: (),
}
#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum PrincipalProp {
// WebDAV (RFC 2518)
@@ -40,12 +45,8 @@ pub enum PrincipalProp {
#[serde(rename = "principal-URL")]
PrincipalUrl(HrefElement),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
CurrentUserPrivilegeSet(UserPrivilegeSet),
// CalDAV (RFC 4791)
#[serde(rename = "C:calendar-home-set")]
@@ -53,7 +54,12 @@ pub enum PrincipalProp {
#[serde(rename = "C:calendar-user-address-set")]
CalendarUserAddressSet(HrefElement),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -63,17 +69,19 @@ impl InvalidProperty for PrincipalProp {
}
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalPropName {
Resourcetype,
CurrentUserPrincipal,
Owner,
CurrentUserPrivilegeSet,
#[strum(serialize = "principal-URL")]
PrincipalUrl,
CalendarHomeSet,
CalendarUserAddressSet,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
impl PrincipalResource {
@@ -87,6 +95,12 @@ impl Resource for PrincipalResource {
type Prop = PrincipalProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -97,20 +111,15 @@ impl Resource for PrincipalResource {
Ok(match prop {
PrincipalPropName::Resourcetype => PrincipalProp::Resourcetype(Resourcetype::default()),
PrincipalPropName::CurrentUserPrincipal => {
PrincipalProp::CurrentUserPrincipal(principal_href)
}
PrincipalPropName::Owner => PrincipalProp::Owner(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&self.principal]).unwrap(),
)),
PrincipalPropName::CurrentUserPrivilegeSet => {
PrincipalProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
}
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
PrincipalPropName::CalendarHomeSet => PrincipalProp::CalendarHomeSet(principal_href),
PrincipalPropName::CalendarUserAddressSet => {
PrincipalProp::CalendarUserAddressSet(principal_href)
}
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -3,19 +3,25 @@ use crate::Error;
use actix_web::dev::ResourceMap;
use actix_web::HttpRequest;
use async_trait::async_trait;
use derive_more::derive::{From, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use strum::{EnumString, VariantNames};
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum RootPropName {
Resourcetype,
CurrentUserPrincipal,
CurrentUserPrivilegeSet,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Deserialize, Serialize, Default, Debug)]
@@ -24,19 +30,18 @@ pub struct Resourcetype {
collection: (),
}
#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum RootProp {
// WebDAV (RFC 2518)
Resourcetype(Resourcetype),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
#[serde(other)]
#[serde(untagged)]
Invalid,
}
@@ -54,6 +59,12 @@ impl Resource for RootResource {
type Prop = RootProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -62,12 +73,7 @@ impl Resource for RootResource {
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
RootPropName::Resourcetype => RootProp::Resourcetype(Resourcetype::default()),
RootPropName::CurrentUserPrincipal => RootProp::CurrentUserPrincipal(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&user.id]).unwrap(),
)),
RootPropName::CurrentUserPrivilegeSet => {
RootProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
}
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -1,8 +1,10 @@
use crate::{principal::PrincipalResource, Error};
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use derive_more::derive::{From, Into, TryInto};
use rustical_dav::{
extension::BoxedExtension,
extensions::{CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName},
privileges::UserPrivilegeSet,
resource::{InvalidProperty, Resource, ResourceService},
xml::HrefElement,
@@ -22,18 +24,20 @@ pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
pub object_id: String,
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum AddressObjectPropName {
Getetag,
AddressData,
Getcontenttype,
CurrentUserPrincipal,
Owner,
CurrentUserPrivilegeSet,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum AddressObjectProp {
// WebDAV (RFC 2518)
@@ -50,7 +54,13 @@ pub enum AddressObjectProp {
// CalDAV (RFC 4791)
#[serde(rename = "CARD:address-data")]
AddressData(String),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -71,6 +81,12 @@ impl Resource for AddressObjectResource {
type Prop = AddressObjectProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -85,15 +101,10 @@ impl Resource for AddressObjectResource {
AddressObjectPropName::Getcontenttype => {
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
}
AddressObjectPropName::CurrentUserPrincipal => AddressObjectProp::CurrentUserPrincipal(
HrefElement::new(PrincipalResource::get_principal_url(rmap, &user.id)),
),
AddressObjectPropName::Owner => AddressObjectProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.principal).into(),
),
AddressObjectPropName::CurrentUserPrivilegeSet => {
AddressObjectProp::CurrentUserPrivilegeSet(UserPrivilegeSet::all())
}
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -9,7 +9,11 @@ use actix_web::http::Method;
use actix_web::web;
use actix_web::{web::Data, HttpRequest};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use derive_more::derive::{From, Into, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
@@ -27,24 +31,25 @@ pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
pub addressbook_id: String,
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum AddressbookPropName {
Resourcetype,
Displayname,
Getcontenttype,
CurrentUserPrincipal,
Owner,
CurrentUserPrivilegeSet,
AddressbookDescription,
SupportedAddressData,
SupportedReportSet,
MaxResourceSize,
SyncToken,
Getctag,
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum AddressbookProp {
// WebDAV (RFC 2518)
@@ -52,12 +57,8 @@ pub enum AddressbookProp {
Displayname(Option<String>),
Getcontenttype(String),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
CurrentUserPrivilegeSet(UserPrivilegeSet),
// CardDAV (RFC 6352)
#[serde(
@@ -79,7 +80,12 @@ pub enum AddressbookProp {
// Didn't find the spec
Getctag(String),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -97,6 +103,12 @@ impl Resource for AddressbookResource {
type Prop = AddressbookProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -107,17 +119,9 @@ impl Resource for AddressbookResource {
AddressbookPropName::Resourcetype => {
AddressbookProp::Resourcetype(Resourcetype::default())
}
AddressbookPropName::CurrentUserPrincipal => {
AddressbookProp::CurrentUserPrincipal(HrefElement::new(
PrincipalResource::get_principal_url(rmap, &self.0.principal),
))
}
AddressbookPropName::Owner => AddressbookProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.0.principal).into(),
),
AddressbookPropName::CurrentUserPrivilegeSet => {
AddressbookProp::CurrentUserPrivilegeSet(UserPrivilegeSet::all())
}
AddressbookPropName::Displayname => {
AddressbookProp::Displayname(self.0.displayname.clone())
}
@@ -136,13 +140,13 @@ impl Resource for AddressbookResource {
}
AddressbookPropName::SyncToken => AddressbookProp::SyncToken(self.0.format_synctoken()),
AddressbookPropName::Getctag => AddressbookProp::Getctag(self.0.format_synctoken()),
_ => panic!("we shouldn't end up here"),
})
}
fn set_prop(&mut self, prop: Self::Prop) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookProp::Resourcetype(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::CurrentUserPrincipal(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Displayname(displayname) => {
self.0.displayname = displayname;
@@ -154,19 +158,18 @@ impl Resource for AddressbookResource {
}
AddressbookProp::Getcontenttype(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::MaxResourceSize(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::CurrentUserPrivilegeSet(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::SupportedReportSet(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::SupportedAddressData(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::SyncToken(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
_ => panic!("we shouldn't end up here"),
}
}
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookPropName::Resourcetype => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::CurrentUserPrincipal => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Owner => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Displayname => {
self.0.displayname = None;
@@ -178,11 +181,11 @@ impl Resource for AddressbookResource {
}
AddressbookPropName::Getcontenttype => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::MaxResourceSize => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::CurrentUserPrivilegeSet => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::SupportedReportSet => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::SupportedAddressData => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
_ => panic!("we shouldn't end up here"),
}
}

View File

@@ -4,6 +4,11 @@ use actix_web::dev::ResourceMap;
use actix_web::web::Data;
use actix_web::HttpRequest;
use async_trait::async_trait;
use derive_more::derive::{From, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
@@ -18,7 +23,7 @@ pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
addr_store: Arc<A>,
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PrincipalResource {
principal: String,
}
@@ -30,7 +35,7 @@ pub struct Resourcetype {
collection: (),
}
#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum PrincipalProp {
// WebDAV (RFC 2518)
@@ -40,15 +45,18 @@ pub enum PrincipalProp {
#[serde(rename = "principal-URL")]
PrincipalUrl(HrefElement),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// CardDAV (RFC 6352)
#[serde(rename = "CARD:addressbook-home-set")]
AddressbookHomeSet(HrefElement),
#[serde(rename = "CARD:principal-address")]
PrincipalAddress(Option<HrefElement>),
#[serde(other)]
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtRFC5397RFC3477(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
}
@@ -58,15 +66,18 @@ impl InvalidProperty for PrincipalProp {
}
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalPropName {
Resourcetype,
CurrentUserPrincipal,
#[strum(serialize = "principal-URL")]
PrincipalUrl,
AddressbookHomeSet,
PrincipalAddress,
#[from]
#[try_into]
#[strum(disabled)]
ExtRFC5397(CommonPropertiesPropName),
}
impl PrincipalResource {
@@ -80,6 +91,12 @@ impl Resource for PrincipalResource {
type Prop = PrincipalProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -90,14 +107,12 @@ impl Resource for PrincipalResource {
Ok(match prop {
PrincipalPropName::Resourcetype => PrincipalProp::Resourcetype(Resourcetype::default()),
PrincipalPropName::CurrentUserPrincipal => {
PrincipalProp::CurrentUserPrincipal(principal_href)
}
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
PrincipalPropName::AddressbookHomeSet => {
PrincipalProp::AddressbookHomeSet(principal_href)
}
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -3,19 +3,25 @@ use crate::Error;
use actix_web::dev::ResourceMap;
use actix_web::HttpRequest;
use async_trait::async_trait;
use derive_more::{From, TryInto};
use rustical_dav::extension::BoxedExtension;
use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use strum::{EnumString, VariantNames};
#[derive(EnumString, Debug, VariantNames, Clone)]
#[derive(EnumString, Debug, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum RootPropName {
Resourcetype,
CurrentUserPrincipal,
CurrentUserPrivilegeSet,
#[from]
#[try_into]
#[strum(disabled)]
ExtCommonProperties(CommonPropertiesPropName),
}
#[derive(Deserialize, Serialize, Default, Debug)]
@@ -24,17 +30,16 @@ pub struct Resourcetype {
collection: (),
}
#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum RootProp {
// WebDAV (RFC 2518)
Resourcetype(Resourcetype),
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtCommonProperties(CommonPropertiesProp),
#[serde(untagged)]
Invalid,
@@ -54,6 +59,12 @@ impl Resource for RootResource {
type Prop = RootProp;
type Error = Error;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(CommonPropertiesExtension::<
PrincipalResource,
>::default())]
}
fn get_prop(
&self,
rmap: &ResourceMap,
@@ -62,12 +73,7 @@ impl Resource for RootResource {
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
RootPropName::Resourcetype => RootProp::Resourcetype(Resourcetype::default()),
RootPropName::CurrentUserPrincipal => RootProp::CurrentUserPrincipal(
PrincipalResource::get_principal_url(rmap, &user.id).into(),
),
RootPropName::CurrentUserPrivilegeSet => {
RootProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
}
_ => panic!("we shouldn't end up here"),
})
}

View File

@@ -20,3 +20,4 @@ log = { workspace = true }
derive_more = { workspace = true }
tracing = { workspace = true }
tracing-actix-web = { workspace = true }
erased-serde = "0.4.5"

133
crates/dav/src/extension.rs Normal file
View File

@@ -0,0 +1,133 @@
use crate::resource::{Resource, ResourceProp, ResourcePropName};
use actix_web::dev::ResourceMap;
use derive_more::derive::Deref;
use rustical_store::auth::User;
use std::str::FromStr;
use strum::VariantNames;
pub trait ResourceExtension<R: Resource>: Clone {
type PropName: ResourcePropName;
type Prop: ResourceProp;
type Error: Into<R::Error> + From<crate::Error>;
fn list_props() -> &'static [&'static str] {
Self::PropName::VARIANTS
}
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: Self::PropName,
) -> Result<Self::Prop, Self::Error>;
fn remove_prop(&mut self, _prop: &Self::PropName) -> Result<(), Self::Error> {
Err(crate::Error::PropReadOnly.into())
}
}
pub struct ResourceExtensionWrapper;
pub trait BoxableExtension<R: Resource> {
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: R::PropName,
) -> Result<Option<R::Prop>, R::Error>;
fn propfind<'a>(
&self,
resource: &R,
props: Vec<&'a str>,
user: &User,
rmap: &ResourceMap,
) -> Result<(Vec<&'a str>, Vec<R::Prop>), R::Error>;
fn list_props(&self) -> &'static [&'static str];
}
impl<
R: Resource,
RE: ResourceExtension<
R,
PropName: Into<R::PropName> + TryFrom<R::PropName>,
Prop: Into<R::Prop> + TryFrom<R::Prop>,
Error: Into<R::Error>,
>,
> BoxableExtension<R> for RE
{
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: <R as Resource>::PropName,
) -> Result<Option<R::Prop>, R::Error> {
let prop: RE::PropName = if let Ok(prop) = prop.try_into() {
prop
} else {
return Ok(None);
};
let prop = ResourceExtension::<R>::get_prop(self, resource, rmap, user, prop)
.map_err(RE::Error::into)?;
Ok(Some(prop.into()))
}
fn propfind<'a>(
&self,
resource: &R,
props: Vec<&'a str>,
user: &User,
rmap: &ResourceMap,
) -> Result<(Vec<&'a str>, Vec<R::Prop>), R::Error> {
let (valid_props, invalid_props): (Vec<Option<RE::PropName>>, Vec<Option<&str>>) = props
.into_iter()
.map(|prop| {
if let Ok(valid_prop) = RE::PropName::from_str(prop) {
(Some(valid_prop), None)
} else {
(None, Some(prop))
}
})
.unzip();
let valid_props: Vec<RE::PropName> = valid_props.into_iter().flatten().collect();
let invalid_props: Vec<&str> = invalid_props.into_iter().flatten().collect();
let prop_responses = valid_props
.into_iter()
.map(|prop| self.get_prop(resource, rmap, user, prop))
.collect::<Result<Vec<_>, RE::Error>>()
.map_err(RE::Error::into)?
.into_iter()
.map(|prop| prop.into())
.collect::<Vec<_>>();
Ok((invalid_props, prop_responses))
}
fn list_props(&self) -> &'static [&'static str] {
Self::list_props()
}
}
#[derive(Deref)]
pub struct BoxedExtension<R>(Box<dyn BoxableExtension<R>>);
impl<R: Resource> BoxedExtension<R> {
pub fn from_ext<
RE: ResourceExtension<
R,
PropName: Into<R::PropName> + TryFrom<R::PropName>,
Prop: Into<R::Prop> + TryFrom<R::Prop>,
> + 'static,
>(
ext: RE,
) -> Self {
let boxed_ext: Box<dyn BoxableExtension<R>> = Box::new(ext);
BoxedExtension(boxed_ext)
}
}

View File

@@ -0,0 +1,77 @@
use std::marker::PhantomData;
use actix_web::dev::ResourceMap;
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use strum::{EnumString, VariantNames};
use crate::{
extension::ResourceExtension,
privileges::UserPrivilegeSet,
resource::{InvalidProperty, Resource},
xml::HrefElement,
};
#[derive(Debug, Clone)]
pub struct CommonPropertiesExtension<PR: Resource>(PhantomData<PR>);
impl<PR: Resource> Default for CommonPropertiesExtension<PR> {
fn default() -> Self {
Self(PhantomData)
}
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub enum CommonPropertiesProp {
// WebDAV Current Principal Extension (RFC 5397)
CurrentUserPrincipal(HrefElement),
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
#[serde(untagged)]
Invalid,
}
impl InvalidProperty for CommonPropertiesProp {
fn invalid_property(&self) -> bool {
matches!(self, Self::Invalid)
}
}
#[derive(EnumString, Debug, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum CommonPropertiesPropName {
CurrentUserPrincipal,
CurrentUserPrivilegeSet,
}
impl<R: Resource, PR: Resource> ResourceExtension<R> for CommonPropertiesExtension<PR>
where
R::PropName: TryInto<CommonPropertiesPropName>,
R::Prop: From<CommonPropertiesProp>,
{
type Prop = CommonPropertiesProp;
type PropName = CommonPropertiesPropName;
type Error = R::Error;
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: Self::PropName,
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
CommonPropertiesPropName::CurrentUserPrincipal => {
CommonPropertiesProp::CurrentUserPrincipal(
PR::get_url(rmap, &[&user.id]).unwrap().into(),
)
}
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
CommonPropertiesProp::CurrentUserPrivilegeSet(resource.get_user_privileges(user)?)
}
})
}
}

View File

@@ -1,5 +1,7 @@
pub mod depth_header;
pub mod error;
pub mod extension;
pub mod extensions;
pub mod methods;
pub mod namespace;
pub mod privileges;

View File

@@ -1,5 +1,6 @@
use crate::depth_header::Depth;
use crate::privileges::UserPrivilege;
use crate::resource::InvalidProperty;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::xml::multistatus::PropstatWrapper;

View File

@@ -1,3 +1,4 @@
use crate::extension::BoxedExtension;
use crate::methods::{route_delete, route_propfind, route_proppatch};
use crate::privileges::UserPrivilegeSet;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
@@ -17,13 +18,31 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum::VariantNames;
pub trait ResourceReadProp: Serialize + fmt::Debug + InvalidProperty {}
impl<T: Serialize + fmt::Debug + InvalidProperty> ResourceReadProp for T {}
pub trait ResourceProp: ResourceReadProp + for<'de> Deserialize<'de> {}
impl<T: ResourceReadProp + for<'de> Deserialize<'de>> ResourceProp for T {}
pub trait ResourcePropName: FromStr + VariantNames {}
impl<T: FromStr + VariantNames> ResourcePropName for T {}
pub trait Resource: Clone {
type PropName: FromStr + VariantNames;
type Prop: Serialize + for<'de> Deserialize<'de> + fmt::Debug + InvalidProperty;
type PropName: ResourcePropName;
type Prop: ResourceProp;
type Error: ResponseError + From<crate::Error>;
fn list_props() -> &'static [&'static str] {
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![]
}
fn list_props() -> Vec<&'static str> {
Self::PropName::VARIANTS
.iter()
.map(|&prop| prop)
// Bodge, since VariantNames somehow includes Ext... props despite the strum(disabled) flag
.filter(|prop| !prop.starts_with("ext-"))
.collect()
}
fn get_prop(
@@ -74,10 +93,18 @@ pub trait Resource: Clone {
Error::BadRequest("propname MUST be the only queried prop".to_owned()).into(),
);
}
let props: Vec<String> = Self::list_props()
let mut props: Vec<String> = Self::list_props()
.iter()
.map(|&prop| prop.to_string())
.collect();
for extension in Self::list_extensions() {
let ext_props: Vec<String> = extension
.list_props()
.iter()
.map(|&prop| prop.to_string())
.collect();
props.extend(ext_props);
}
return Ok(ResponseElement {
href: path.to_owned(),
propstat: vec![PropstatWrapper::TagList(PropstatElement {
@@ -95,6 +122,10 @@ pub trait Resource: Clone {
);
}
props = Self::list_props().into();
for extension in Self::list_extensions() {
let ext_props: Vec<&str> = extension.list_props().into();
props.extend(ext_props);
}
}
let (valid_props, invalid_props): (Vec<Option<Self::PropName>>, Vec<Option<&str>>) = props
@@ -108,13 +139,20 @@ pub trait Resource: Clone {
})
.unzip();
let valid_props: Vec<Self::PropName> = valid_props.into_iter().flatten().collect();
let invalid_props: Vec<&str> = invalid_props.into_iter().flatten().collect();
let mut invalid_props: Vec<&str> = invalid_props.into_iter().flatten().collect();
let prop_responses = valid_props
let mut prop_responses = valid_props
.into_iter()
.map(|prop| self.get_prop(rmap, user, &prop))
.collect::<Result<Vec<Self::Prop>, Self::Error>>()?;
for extension in Self::list_extensions() {
let (ext_invalid_props, ext_responses) =
extension.propfind(self, invalid_props, user, rmap)?;
invalid_props = ext_invalid_props;
prop_responses.extend(ext_responses);
}
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
status: format!("HTTP/1.1 {}", StatusCode::OK),
prop: PropTagWrapper {