mirror of
https://github.com/lennart-k/rustical.git
synced 2026-01-30 17:38:22 +00:00
Improvement to access control
This commit is contained in:
@@ -16,7 +16,7 @@ use rustical_dav::{
|
||||
MultistatusElement,
|
||||
},
|
||||
};
|
||||
use rustical_store::{AddressObject, AddressbookStore};
|
||||
use rustical_store::{auth::User, AddressObject, AddressbookStore};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
@@ -64,6 +64,7 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore + ?Sized>(
|
||||
pub async fn handle_addressbook_multiget<AS: AddressbookStore + ?Sized>(
|
||||
addr_multiget: AddressbookMultigetRequest,
|
||||
req: HttpRequest,
|
||||
user: &User,
|
||||
principal: &str,
|
||||
cal_id: &str,
|
||||
addr_store: &AS,
|
||||
@@ -92,11 +93,13 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore + ?Sized>(
|
||||
let mut responses = Vec::new();
|
||||
for object in objects {
|
||||
let path = format!("{}/{}", req.path(), object.get_id());
|
||||
responses.push(AddressObjectResource::from(object).propfind(
|
||||
&path,
|
||||
props.clone(),
|
||||
req.resource_map(),
|
||||
)?);
|
||||
responses.push(
|
||||
AddressObjectResource {
|
||||
object,
|
||||
principal: principal.to_owned(),
|
||||
}
|
||||
.propfind(&path, props.clone(), user, req.resource_map())?,
|
||||
);
|
||||
}
|
||||
|
||||
let not_found_responses = not_found
|
||||
|
||||
@@ -47,6 +47,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
|
||||
handle_addressbook_multiget(
|
||||
addr_multiget,
|
||||
req,
|
||||
&user,
|
||||
&principal,
|
||||
&addressbook_id,
|
||||
addr_store.as_ref(),
|
||||
@@ -57,6 +58,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
|
||||
handle_sync_collection(
|
||||
sync_collection,
|
||||
req,
|
||||
&user,
|
||||
&principal,
|
||||
&addressbook_id,
|
||||
addr_store.as_ref(),
|
||||
|
||||
@@ -12,6 +12,7 @@ use rustical_dav::{
|
||||
},
|
||||
};
|
||||
use rustical_store::{
|
||||
auth::User,
|
||||
synctoken::{format_synctoken, parse_synctoken},
|
||||
AddressbookStore,
|
||||
};
|
||||
@@ -42,6 +43,7 @@ pub struct SyncCollectionRequest {
|
||||
pub async fn handle_sync_collection<AS: AddressbookStore + ?Sized>(
|
||||
sync_collection: SyncCollectionRequest,
|
||||
req: HttpRequest,
|
||||
user: &User,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
addr_store: &AS,
|
||||
@@ -69,11 +71,13 @@ pub async fn handle_sync_collection<AS: AddressbookStore + ?Sized>(
|
||||
vec![principal, addressbook_id, &object.get_id()],
|
||||
)
|
||||
.unwrap();
|
||||
responses.push(AddressObjectResource::from(object).propfind(
|
||||
&path,
|
||||
props.clone(),
|
||||
req.resource_map(),
|
||||
)?);
|
||||
responses.push(
|
||||
AddressObjectResource {
|
||||
object,
|
||||
principal: principal.to_owned(),
|
||||
}
|
||||
.propfind(&path, props.clone(), user, req.resource_map())?,
|
||||
);
|
||||
}
|
||||
|
||||
for object_id in deleted_objects {
|
||||
|
||||
@@ -41,55 +41,6 @@ pub struct Resourcetype {
|
||||
collection: (),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum UserPrivilege {
|
||||
Read,
|
||||
ReadAcl,
|
||||
Write,
|
||||
WriteAcl,
|
||||
WriteContent,
|
||||
ReadCurrentUserPrivilegeSet,
|
||||
Bind,
|
||||
Unbind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct UserPrivilegeWrapper {
|
||||
#[serde(rename = "$value")]
|
||||
privilege: UserPrivilege,
|
||||
}
|
||||
|
||||
impl From<UserPrivilege> for UserPrivilegeWrapper {
|
||||
fn from(value: UserPrivilege) -> Self {
|
||||
Self { privilege: value }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct UserPrivilegeSet {
|
||||
privilege: Vec<UserPrivilegeWrapper>,
|
||||
}
|
||||
|
||||
impl Default for UserPrivilegeSet {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
privilege: vec![
|
||||
UserPrivilege::Read.into(),
|
||||
UserPrivilege::ReadAcl.into(),
|
||||
UserPrivilege::Write.into(),
|
||||
UserPrivilege::WriteAcl.into(),
|
||||
UserPrivilege::WriteContent.into(),
|
||||
UserPrivilege::ReadCurrentUserPrivilegeSet.into(),
|
||||
UserPrivilege::Bind.into(),
|
||||
UserPrivilege::Unbind.into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum ReportMethod {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::methods::mkcol::route_mkcol;
|
||||
use super::methods::report::route_report_addressbook;
|
||||
use super::prop::{Resourcetype, SupportedAddressData, SupportedReportSet, UserPrivilegeSet};
|
||||
use super::prop::{Resourcetype, SupportedAddressData, SupportedReportSet};
|
||||
use crate::address_object::resource::AddressObjectResource;
|
||||
use crate::principal::PrincipalResource;
|
||||
use crate::Error;
|
||||
@@ -10,6 +10,7 @@ use actix_web::web;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
|
||||
use rustical_dav::xml::HrefElement;
|
||||
use rustical_store::auth::User;
|
||||
@@ -99,6 +100,7 @@ impl Resource for AddressbookResource {
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
user: &User,
|
||||
prop: Self::PropName,
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
Ok(match prop {
|
||||
@@ -107,12 +109,15 @@ impl Resource for AddressbookResource {
|
||||
}
|
||||
AddressbookPropName::CurrentUserPrincipal => {
|
||||
AddressbookProp::CurrentUserPrincipal(HrefElement::new(
|
||||
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
|
||||
PrincipalResource::get_principal_url(rmap, &self.0.principal),
|
||||
))
|
||||
}
|
||||
AddressbookPropName::Owner => AddressbookProp::Owner(HrefElement::new(
|
||||
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
|
||||
)),
|
||||
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())
|
||||
}
|
||||
@@ -120,9 +125,6 @@ impl Resource for AddressbookResource {
|
||||
AddressbookProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
|
||||
}
|
||||
AddressbookPropName::MaxResourceSize => AddressbookProp::MaxResourceSize(10000000),
|
||||
AddressbookPropName::CurrentUserPrivilegeSet => {
|
||||
AddressbookProp::CurrentUserPrivilegeSet(UserPrivilegeSet::default())
|
||||
}
|
||||
AddressbookPropName::SupportedReportSet => {
|
||||
AddressbookProp::SupportedReportSet(SupportedReportSet::default())
|
||||
}
|
||||
@@ -188,6 +190,10 @@ impl Resource for AddressbookResource {
|
||||
fn resource_name() -> &'static str {
|
||||
"carddav_addressbook"
|
||||
}
|
||||
|
||||
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
|
||||
Ok(UserPrivilegeSet::owner_only(self.0.principal == user.id))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
@@ -197,10 +203,7 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
type Resource = AddressbookResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_resource(&self, user: User) -> Result<Self::Resource, Error> {
|
||||
if self.principal != user.id {
|
||||
return Err(Error::Unauthorized);
|
||||
}
|
||||
async fn get_resource(&self) -> Result<Self::Resource, Error> {
|
||||
let addressbook = self
|
||||
.addr_store
|
||||
.get_addressbook(&self.principal, &self.addressbook_id)
|
||||
@@ -225,7 +228,10 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
|
||||
vec![&self.principal, &self.addressbook_id, object.get_id()],
|
||||
)
|
||||
.unwrap(),
|
||||
object.into(),
|
||||
AddressObjectResource {
|
||||
object,
|
||||
principal: self.principal.to_owned(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
|
||||
Reference in New Issue
Block a user