outsource owner property

This commit is contained in:
Lennart
2024-11-03 23:00:20 +01:00
parent f1997f2c18
commit f2d39e3351
8 changed files with 44 additions and 61 deletions

View File

@@ -19,7 +19,6 @@ use rustical_dav::extensions::{
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
use rustical_store::auth::User;
use rustical_store::{Calendar, CalendarStore};
use serde::{Deserialize, Serialize};
@@ -37,7 +36,6 @@ pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
#[derive(EnumString, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarPropName {
Owner,
Displayname,
CalendarColor,
CalendarDescription,
@@ -63,9 +61,6 @@ pub enum CalendarProp {
Displayname(Option<String>),
Getcontenttype(String),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
// CalDAV (RFC 4791)
#[serde(rename = "IC:calendar-color", alias = "calendar-color")]
CalendarColor(Option<String>),
@@ -126,14 +121,11 @@ impl Resource for CalendarResource {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
_rmap: &ResourceMap,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
CalendarPropName::Owner => CalendarProp::Owner(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
)),
CalendarPropName::Displayname => CalendarProp::Displayname(self.0.displayname.clone()),
CalendarPropName::CalendarColor => CalendarProp::CalendarColor(self.0.color.clone()),
CalendarPropName::CalendarDescription => {
@@ -179,7 +171,6 @@ impl Resource for CalendarResource {
fn set_prop(&mut self, prop: Self::Prop) -> Result<(), rustical_dav::Error> {
match prop {
CalendarProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::Displayname(displayname) => {
self.0.displayname = displayname;
Ok(())
@@ -217,7 +208,6 @@ impl Resource for CalendarResource {
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> {
match prop {
CalendarPropName::Owner => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Displayname => {
self.0.displayname = None;
Ok(())
@@ -257,6 +247,10 @@ impl Resource for CalendarResource {
"caldav_calendar"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.0.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.0.principal == user.id))
}

View File

@@ -8,7 +8,6 @@ use rustical_dav::{
extensions::{CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName},
privileges::UserPrivilegeSet,
resource::{InvalidProperty, Resource, ResourceService},
xml::HrefElement,
};
use rustical_store::{auth::User, CalendarObject, CalendarStore};
use serde::{Deserialize, Serialize};
@@ -29,7 +28,6 @@ pub enum CalendarObjectPropName {
Getetag,
CalendarData,
Getcontenttype,
Owner,
#[from]
#[try_into]
#[strum(disabled)]
@@ -47,9 +45,6 @@ pub enum CalendarObjectProp {
#[serde(rename = "C:calendar-data")]
CalendarData(String),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
@@ -92,7 +87,7 @@ impl Resource for CalendarObjectResource {
fn get_prop(
&self,
rmap: &ResourceMap,
_rmap: &ResourceMap,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
@@ -104,9 +99,6 @@ impl Resource for CalendarObjectResource {
CalendarObjectPropName::Getcontenttype => {
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
}
CalendarObjectPropName::Owner => CalendarObjectProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.principal).into(),
),
_ => panic!("we shouldn't end up here"),
})
}
@@ -116,6 +108,10 @@ impl Resource for CalendarObjectResource {
"caldav_calendar_object"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.principal == user.id))
}

View File

@@ -38,16 +38,10 @@ pub struct Resourcetype {
#[derive(Deserialize, Serialize, From, TryInto)]
#[serde(rename_all = "kebab-case")]
pub enum PrincipalProp {
// WebDAV (RFC 2518)
Resourcetype(Resourcetype),
// WebDAV Access Control (RFC 3744)
#[serde(rename = "principal-URL")]
PrincipalUrl(HrefElement),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
// CalDAV (RFC 4791)
#[serde(rename = "C:calendar-home-set")]
CalendarHomeSet(HrefElement),
@@ -72,8 +66,6 @@ impl InvalidProperty for PrincipalProp {
#[derive(EnumString, VariantNames, Clone, From, TryInto)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalPropName {
Resourcetype,
Owner,
#[strum(serialize = "principal-URL")]
PrincipalUrl,
CalendarHomeSet,
@@ -105,16 +97,12 @@ impl Resource for PrincipalResource {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
let principal_href = HrefElement::new(Self::get_url(rmap, vec![&self.principal]).unwrap());
Ok(match prop {
PrincipalPropName::Resourcetype => PrincipalProp::Resourcetype(Resourcetype::default()),
PrincipalPropName::Owner => PrincipalProp::Owner(HrefElement::new(
PrincipalResource::get_url(rmap, vec![&self.principal]).unwrap(),
)),
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
PrincipalPropName::CalendarHomeSet => PrincipalProp::CalendarHomeSet(principal_href),
PrincipalPropName::CalendarUserAddressSet => {
@@ -129,6 +117,10 @@ impl Resource for PrincipalResource {
"caldav_principal"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.principal == user.id))
}

View File

@@ -7,7 +7,6 @@ use rustical_dav::{
extensions::{CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName},
privileges::UserPrivilegeSet,
resource::{InvalidProperty, Resource, ResourceService},
xml::HrefElement,
};
use rustical_store::{auth::User, AddressObject, AddressbookStore};
use serde::{Deserialize, Serialize};
@@ -30,7 +29,6 @@ pub enum AddressObjectPropName {
Getetag,
AddressData,
Getcontenttype,
Owner,
#[from]
#[try_into]
#[strum(disabled)]
@@ -44,13 +42,6 @@ pub enum AddressObjectProp {
Getetag(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 = "CARD:address-data")]
AddressData(String),
@@ -109,9 +100,6 @@ impl Resource for AddressObjectResource {
AddressObjectPropName::Getcontenttype => {
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
}
AddressObjectPropName::Owner => AddressObjectProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.principal).into(),
),
_ => panic!("we shouldn't end up here"),
})
}
@@ -121,6 +109,10 @@ impl Resource for AddressObjectResource {
"carddav_address_object"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.principal == user.id))
}

View File

@@ -16,7 +16,6 @@ use rustical_dav::extensions::{
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
use rustical_dav::xml::HrefElement;
use rustical_store::auth::User;
use rustical_store::{Addressbook, AddressbookStore};
use serde::{Deserialize, Serialize};
@@ -36,7 +35,6 @@ pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
pub enum AddressbookPropName {
Displayname,
Getcontenttype,
Owner,
AddressbookDescription,
SupportedAddressData,
SupportedReportSet,
@@ -55,9 +53,6 @@ pub enum AddressbookProp {
Displayname(Option<String>),
Getcontenttype(String),
// WebDAV Access Control (RFC 3744)
Owner(HrefElement),
// CardDAV (RFC 6352)
#[serde(
rename = "CARD:addressbook-description",
@@ -110,14 +105,11 @@ impl Resource for AddressbookResource {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
_rmap: &ResourceMap,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
AddressbookPropName::Owner => AddressbookProp::Owner(
PrincipalResource::get_principal_url(rmap, &self.0.principal).into(),
),
AddressbookPropName::Displayname => {
AddressbookProp::Displayname(self.0.displayname.clone())
}
@@ -142,7 +134,6 @@ impl Resource for AddressbookResource {
fn set_prop(&mut self, prop: Self::Prop) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookProp::Owner(_) => Err(rustical_dav::Error::PropReadOnly),
AddressbookProp::Displayname(displayname) => {
self.0.displayname = displayname;
Ok(())
@@ -164,7 +155,6 @@ impl Resource for AddressbookResource {
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookPropName::Owner => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Displayname => {
self.0.displayname = None;
Ok(())
@@ -188,6 +178,10 @@ impl Resource for AddressbookResource {
"carddav_addressbook"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.0.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.0.principal == user.id))
}

View File

@@ -51,7 +51,7 @@ pub enum PrincipalProp {
#[serde(skip_deserializing, untagged)]
#[from]
#[try_into]
ExtRFC5397RFC3477(CommonPropertiesProp<PrincipalResource>),
ExtCommonProperties(CommonPropertiesProp<PrincipalResource>),
#[serde(untagged)]
Invalid,
@@ -73,7 +73,7 @@ pub enum PrincipalPropName {
#[from]
#[try_into]
#[strum(disabled)]
ExtRFC5397(CommonPropertiesPropName),
ExtCommonProperties(CommonPropertiesPropName),
}
impl PrincipalResource {
@@ -97,7 +97,7 @@ impl Resource for PrincipalResource {
fn get_prop(
&self,
rmap: &ResourceMap,
user: &User,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal));
@@ -117,6 +117,10 @@ impl Resource for PrincipalResource {
"carddav_principal"
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.principal == user.id))
}

View File

@@ -30,6 +30,7 @@ pub enum CommonPropertiesProp<R: Resource> {
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
Owner(Option<HrefElement>),
#[serde(untagged)]
Invalid,
@@ -47,6 +48,7 @@ pub enum CommonPropertiesPropName {
Resourcetype,
CurrentUserPrincipal,
CurrentUserPrivilegeSet,
Owner,
}
impl<R: Resource, PR: Resource> ResourceExtension<R> for CommonPropertiesExtension<PR>
@@ -77,6 +79,11 @@ where
CommonPropertiesPropName::CurrentUserPrivilegeSet => {
CommonPropertiesProp::CurrentUserPrivilegeSet(resource.get_user_privileges(user)?)
}
CommonPropertiesPropName::Owner => CommonPropertiesProp::Owner(
resource
.get_owner()
.map(|owner| PR::get_url(rmap, &[owner]).unwrap().into()),
),
})
}
}

View File

@@ -62,6 +62,10 @@ pub trait Resource: Clone {
fn resource_name() -> &'static str;
fn get_owner(&self) -> Option<&str> {
None
}
fn get_url<U, I>(rmap: &ResourceMap, elements: U) -> Result<String, UrlGenerationError>
where
U: IntoIterator<Item = I>,