mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
implement principal types
This commit is contained in:
@@ -15,7 +15,7 @@ use principal::{PrincipalResource, PrincipalResourceService};
|
||||
use rustical_dav::resource::{NamedRoute, ResourceService};
|
||||
use rustical_dav::resources::RootResourceService;
|
||||
use rustical_store::{
|
||||
auth::{AuthenticationMiddleware, AuthenticationProvider},
|
||||
auth::{AuthenticationMiddleware, AuthenticationProvider, UserStore},
|
||||
AddressbookStore, SubscriptionStore,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
@@ -27,6 +27,7 @@ pub mod principal;
|
||||
|
||||
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
|
||||
auth_provider: Arc<AP>,
|
||||
user_store: Arc<impl UserStore>,
|
||||
store: Arc<A>,
|
||||
subscription_store: Arc<S>,
|
||||
) -> impl HttpServiceFactory {
|
||||
@@ -59,7 +60,7 @@ pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: Subsc
|
||||
web::scope("/principal").service(
|
||||
web::scope("/{principal}")
|
||||
.service(
|
||||
PrincipalResourceService::new(store.clone())
|
||||
PrincipalResourceService::new(store.clone(), user_store)
|
||||
.actix_resource()
|
||||
.name(PrincipalResource::route_name()),
|
||||
)
|
||||
|
||||
@@ -6,24 +6,28 @@ use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
|
||||
use rustical_dav::privileges::UserPrivilegeSet;
|
||||
use rustical_dav::resource::{NamedRoute, Resource, ResourceService};
|
||||
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
|
||||
use rustical_store::auth::User;
|
||||
use rustical_store::auth::{User, UserStore};
|
||||
use rustical_store::AddressbookStore;
|
||||
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct PrincipalResourceService<A: AddressbookStore> {
|
||||
pub struct PrincipalResourceService<A: AddressbookStore, US: UserStore> {
|
||||
addr_store: Arc<A>,
|
||||
user_store: Arc<US>,
|
||||
}
|
||||
|
||||
impl<A: AddressbookStore> PrincipalResourceService<A> {
|
||||
pub fn new(addr_store: Arc<A>) -> Self {
|
||||
Self { addr_store }
|
||||
impl<A: AddressbookStore, US: UserStore> PrincipalResourceService<A, US> {
|
||||
pub fn new(addr_store: Arc<A>, user_store: Arc<US>) -> Self {
|
||||
Self {
|
||||
addr_store,
|
||||
user_store,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PrincipalResource {
|
||||
principal: String,
|
||||
principal: User,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
|
||||
@@ -84,7 +88,7 @@ impl Resource for PrincipalResource {
|
||||
user: &User,
|
||||
prop: &PrincipalPropWrapperName,
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal));
|
||||
let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal.id));
|
||||
|
||||
let home_set = AddressbookHomeSet(
|
||||
user.memberships()
|
||||
@@ -98,7 +102,7 @@ impl Resource for PrincipalResource {
|
||||
PrincipalPropWrapperName::Principal(prop) => {
|
||||
PrincipalPropWrapper::Principal(match prop {
|
||||
PrincipalPropName::Displayname => {
|
||||
PrincipalProp::Displayname(self.principal.to_owned())
|
||||
PrincipalProp::Displayname(self.principal.id.to_owned())
|
||||
}
|
||||
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
|
||||
PrincipalPropName::AddressbookHomeSet => {
|
||||
@@ -115,18 +119,18 @@ impl Resource for PrincipalResource {
|
||||
}
|
||||
|
||||
fn get_owner(&self) -> Option<&str> {
|
||||
Some(&self.principal)
|
||||
Some(&self.principal.id)
|
||||
}
|
||||
|
||||
fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
|
||||
Ok(UserPrivilegeSet::owner_only(
|
||||
user.is_principal(&self.principal),
|
||||
user.is_principal(&self.principal.id),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<A: AddressbookStore> ResourceService for PrincipalResourceService<A> {
|
||||
impl<A: AddressbookStore, US: UserStore> ResourceService for PrincipalResourceService<A, US> {
|
||||
type PathComponents = (String,);
|
||||
type MemberType = AddressbookResource;
|
||||
type Resource = PrincipalResource;
|
||||
@@ -136,9 +140,12 @@ impl<A: AddressbookStore> ResourceService for PrincipalResourceService<A> {
|
||||
&self,
|
||||
(principal,): &Self::PathComponents,
|
||||
) -> Result<Self::Resource, Self::Error> {
|
||||
Ok(PrincipalResource {
|
||||
principal: principal.to_owned(),
|
||||
})
|
||||
let user = self
|
||||
.user_store
|
||||
.get_user(principal)
|
||||
.await?
|
||||
.ok_or(crate::Error::NotFound)?;
|
||||
Ok(PrincipalResource { principal: user })
|
||||
}
|
||||
|
||||
async fn get_members(
|
||||
|
||||
Reference in New Issue
Block a user