caldav, carddav: stop hardcoding current user principal

Add Backend.CurrentUserPrincipal to fetch the current user
principal.
This commit is contained in:
Simon Ser 2022-05-03 19:32:46 +02:00
parent c7b85859a2
commit a419541835
2 changed files with 16 additions and 6 deletions

View File

@ -25,6 +25,7 @@ func CalendarHomeSetXML(path string) (xml.Name, xml.Marshaler) {
// Backend is a CalDAV server backend. // Backend is a CalDAV server backend.
type Backend interface { type Backend interface {
CurrentUserPrincipal(ctx context.Context) (string, error)
Calendar(ctx context.Context) (*Calendar, error) Calendar(ctx context.Context) (*Calendar, error)
GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error) GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error)
ListCalendarObjects(ctx context.Context, 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 return nil, err
} }
resp, err := b.propfindCalendar(propfind, cal) resp, err := b.propfindCalendar(r.Context(), propfind, cal)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -239,7 +240,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
return internal.NewMultistatus(resps...), nil 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{ props := map[xml.Name]internal.PropfindFunc{
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return internal.NewResourceType(internal.CollectionName, calendarName), nil 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 // TODO: this should be set on all resources
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { 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
}, },
} }

View File

@ -34,6 +34,7 @@ type PutAddressObjectOptions struct {
// Backend is a CardDAV server backend. // Backend is a CardDAV server backend.
type Backend interface { type Backend interface {
CurrentUserPrincipal(ctx context.Context) (string, error)
AddressBook(ctx context.Context) (*AddressBook, error) AddressBook(ctx context.Context) (*AddressBook, error)
GetAddressObject(ctx context.Context, path string, req *AddressDataRequest) (*AddressObject, error) GetAddressObject(ctx context.Context, path string, req *AddressDataRequest) (*AddressObject, error)
ListAddressObjects(ctx context.Context, 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 return nil, err
} }
resp, err := b.propfindAddressBook(propfind, ab) resp, err := b.propfindAddressBook(r.Context(), propfind, ab)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -341,7 +342,7 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
return internal.NewMultistatus(resps...), nil 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{ props := map[xml.Name]internal.PropfindFunc{
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return internal.NewResourceType(internal.CollectionName, addressBookName), nil 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 // TODO: this should be set on all resources
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { 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
}, },
} }