From a4195418351972451c9377455d4a870f83112291 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 3 May 2022 19:32:46 +0200 Subject: [PATCH] caldav, carddav: stop hardcoding current user principal Add Backend.CurrentUserPrincipal to fetch the current user principal. --- caldav/server.go | 11 ++++++++--- carddav/server.go | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/caldav/server.go b/caldav/server.go index acfbd49..b34e72a 100644 --- a/caldav/server.go +++ b/caldav/server.go @@ -25,6 +25,7 @@ func CalendarHomeSetXML(path string) (xml.Name, xml.Marshaler) { // Backend is a CalDAV server backend. type Backend interface { + CurrentUserPrincipal(ctx context.Context) (string, error) Calendar(ctx context.Context) (*Calendar, error) GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error) ListCalendarObjects(ctx context.Context, req *CalendarCompRequest) ([]CalendarObject, error) @@ -225,7 +226,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i return nil, err } - resp, err := b.propfindCalendar(propfind, cal) + resp, err := b.propfindCalendar(r.Context(), propfind, cal) if err != nil { return nil, err } @@ -239,7 +240,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i return internal.NewMultistatus(resps...), nil } -func (b *backend) propfindCalendar(propfind *internal.Propfind, cal *Calendar) (*internal.Response, error) { +func (b *backend) propfindCalendar(ctx context.Context, propfind *internal.Propfind, cal *Calendar) (*internal.Response, error) { props := map[xml.Name]internal.PropfindFunc{ internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { return internal.NewResourceType(internal.CollectionName, calendarName), nil @@ -263,7 +264,11 @@ func (b *backend) propfindCalendar(propfind *internal.Propfind, cal *Calendar) ( }, // TODO: this should be set on all resources internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { - return &internal.CurrentUserPrincipal{Href: internal.Href{Path: "/"}}, nil + path, err := b.Backend.CurrentUserPrincipal(ctx) + if err != nil { + return nil, err + } + return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil }, } diff --git a/carddav/server.go b/carddav/server.go index 909c644..68ef609 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -34,6 +34,7 @@ type PutAddressObjectOptions struct { // Backend is a CardDAV server backend. type Backend interface { + CurrentUserPrincipal(ctx context.Context) (string, error) AddressBook(ctx context.Context) (*AddressBook, error) GetAddressObject(ctx context.Context, path string, req *AddressDataRequest) (*AddressObject, error) ListAddressObjects(ctx context.Context, req *AddressDataRequest) ([]AddressObject, error) @@ -305,7 +306,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i return nil, err } - resp, err := b.propfindAddressBook(propfind, ab) + resp, err := b.propfindAddressBook(r.Context(), propfind, ab) if err != nil { return nil, err } @@ -341,7 +342,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i return internal.NewMultistatus(resps...), nil } -func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBook) (*internal.Response, error) { +func (b *backend) propfindAddressBook(ctx context.Context, propfind *internal.Propfind, ab *AddressBook) (*internal.Response, error) { props := map[xml.Name]internal.PropfindFunc{ internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { return internal.NewResourceType(internal.CollectionName, addressBookName), nil @@ -362,7 +363,11 @@ func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBo }, // TODO: this should be set on all resources internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { - return &internal.CurrentUserPrincipal{Href: internal.Href{Path: "/"}}, nil + path, err := b.Backend.CurrentUserPrincipal(ctx) + if err != nil { + return nil, err + } + return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil }, }