From 1901d193ce99382db93e46cffa9223ff6f636508 Mon Sep 17 00:00:00 2001 From: Anton Skorochod Date: Tue, 23 Jun 2026 23:04:19 +0200 Subject: [PATCH] caldav, carddav: derive current-user-privilege-set from a read-only flag The server hard-coded DAV:read + DAV:write in current-user-privilege-set for every collection (with a TODO). Add a ReadOnly field to Calendar and AddressBook that the backend sets per request; report DAV:write only when it is false, so clients (e.g. DAVx5, Apple) correctly mark read-only collections. Default (false) keeps the previous read-write behavior. --- caldav/caldav.go | 3 +++ caldav/server.go | 8 +------- carddav/carddav.go | 3 +++ carddav/server.go | 8 +------- internal/elements.go | 10 ++++++++++ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/caldav/caldav.go b/caldav/caldav.go index 04b8399..1df6c66 100644 --- a/caldav/caldav.go +++ b/caldav/caldav.go @@ -69,6 +69,9 @@ type Calendar struct { Description string MaxResourceSize int64 SupportedComponentSet []string + // ReadOnly reports that the current user may only read this calendar. It + // controls the DAV:current-user-privilege-set reported by the server. + ReadOnly bool } type CalendarCompRequest struct { diff --git a/caldav/server.go b/caldav/server.go index 1a25479..0606c7b 100644 --- a/caldav/server.go +++ b/caldav/server.go @@ -546,13 +546,7 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF Comp: components, }, nil }, - // TODO: Gather UserPrivilege from backend to control read-and-write - internal.CurrentUserPrivilegeSetName: internal.PropFindValue(&internal.CurrentUserPrivilegeSet{ - Privilege: []internal.Privilege{{ - Read: &struct{}{}, - Write: &struct{}{}, - }}, - }), + internal.CurrentUserPrivilegeSetName: internal.PropFindValue(internal.NewCurrentUserPrivilegeSet(cal.ReadOnly)), } if cal.Name != "" { diff --git a/carddav/carddav.go b/carddav/carddav.go index ebfd66d..d6a1bf5 100644 --- a/carddav/carddav.go +++ b/carddav/carddav.go @@ -28,6 +28,9 @@ type AddressBook struct { Description string MaxResourceSize int64 SupportedAddressData []AddressDataType + // ReadOnly reports that the current user may only read this address book. + // It controls the DAV:current-user-privilege-set reported by the server. + ReadOnly bool } func (ab *AddressBook) SupportsAddressData(contentType, version string) bool { diff --git a/carddav/server.go b/carddav/server.go index ea5326f..7520b02 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -497,13 +497,7 @@ func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.Pr {ContentType: vcard.MIMEType, Version: "4.0"}, }, }), - // TODO: Gather UserPrivilege from backend to control read-and-write - internal.CurrentUserPrivilegeSetName: internal.PropFindValue(&internal.CurrentUserPrivilegeSet{ - Privilege: []internal.Privilege{{ - Read: &struct{}{}, - Write: &struct{}{}, - }}, - }), + internal.CurrentUserPrivilegeSetName: internal.PropFindValue(internal.NewCurrentUserPrivilegeSet(ab.ReadOnly)), } if ab.Name != "" { diff --git a/internal/elements.go b/internal/elements.go index 77df514..c2bf6a9 100644 --- a/internal/elements.go +++ b/internal/elements.go @@ -471,3 +471,13 @@ type Privilege struct { Read *struct{} `xml:"DAV: read,omitempty"` Write *struct{} `xml:"DAV: write,omitempty"` } + +// NewCurrentUserPrivilegeSet returns a privilege set granting DAV:read, plus +// DAV:write unless readOnly is set. +func NewCurrentUserPrivilegeSet(readOnly bool) *CurrentUserPrivilegeSet { + priv := Privilege{Read: &struct{}{}} + if !readOnly { + priv.Write = &struct{}{} + } + return &CurrentUserPrivilegeSet{Privilege: []Privilege{priv}} +}