caldav, carddav: fill ReadOnly from current-user-privilege-set in client
This commit is contained in:
parent
8ddea980d4
commit
ad1766c2ef
@ -70,7 +70,9 @@ type Calendar struct {
|
|||||||
MaxResourceSize int64
|
MaxResourceSize int64
|
||||||
SupportedComponentSet []string
|
SupportedComponentSet []string
|
||||||
// ReadOnly reports that the current user may only read this calendar. It
|
// ReadOnly reports that the current user may only read this calendar. It
|
||||||
// controls the DAV:current-user-privilege-set reported by the server.
|
// controls the DAV:current-user-privilege-set reported by the server. The
|
||||||
|
// client sets it when the server reports a DAV:current-user-privilege-set
|
||||||
|
// that doesn't grant DAV:write.
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -24,6 +24,7 @@ var calendarProps = []xml.Name{
|
|||||||
calendarDescriptionName,
|
calendarDescriptionName,
|
||||||
maxResourceSizeName,
|
maxResourceSizeName,
|
||||||
supportedCalendarComponentSetName,
|
supportedCalendarComponentSetName,
|
||||||
|
internal.CurrentUserPrivilegeSetName,
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
|
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
|
||||||
@ -141,6 +142,14 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var privSet internal.CurrentUserPrivilegeSet
|
||||||
|
readOnly := false
|
||||||
|
if err := resp.DecodeProp(&privSet); err == nil {
|
||||||
|
readOnly = privSet.ReadOnly()
|
||||||
|
} else if !internal.IsNotFound(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
compNames := make([]string, 0, len(supportedCompSet.Comp))
|
compNames := make([]string, 0, len(supportedCompSet.Comp))
|
||||||
for _, comp := range supportedCompSet.Comp {
|
for _, comp := range supportedCompSet.Comp {
|
||||||
compNames = append(compNames, comp.Name)
|
compNames = append(compNames, comp.Name)
|
||||||
@ -152,6 +161,7 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
|
|||||||
Description: desc.Description,
|
Description: desc.Description,
|
||||||
MaxResourceSize: maxResSize.Size,
|
MaxResourceSize: maxResSize.Size,
|
||||||
SupportedComponentSet: compNames,
|
SupportedComponentSet: compNames,
|
||||||
|
ReadOnly: readOnly,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
99
caldav/client_test.go
Normal file
99
caldav/client_test.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package caldav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const findCalendarsReadOnlyResponse = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||||
|
<d:response>
|
||||||
|
<d:href>/cal/read-only/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>
|
||||||
|
<d:current-user-privilege-set>
|
||||||
|
<d:privilege><d:read/></d:privilege>
|
||||||
|
</d:current-user-privilege-set>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
<d:response>
|
||||||
|
<d:href>/cal/write-content/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>
|
||||||
|
<d:current-user-privilege-set>
|
||||||
|
<d:privilege><d:read/></d:privilege>
|
||||||
|
<d:privilege><d:write-content/></d:privilege>
|
||||||
|
</d:current-user-privilege-set>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
<d:response>
|
||||||
|
<d:href>/cal/write/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>
|
||||||
|
<d:current-user-privilege-set>
|
||||||
|
<d:privilege><d:read/></d:privilege>
|
||||||
|
<d:privilege><d:write/></d:privilege>
|
||||||
|
<d:privilege><d:write-content/></d:privilege>
|
||||||
|
</d:current-user-privilege-set>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
<d:response>
|
||||||
|
<d:href>/cal/unknown/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:current-user-privilege-set/>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 404 Not Found</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
</d:multistatus>`
|
||||||
|
|
||||||
|
func TestFindCalendarsReadOnly(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusMultiStatus)
|
||||||
|
w.Write([]byte(findCalendarsReadOnlyResponse))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
cals, err := c.FindCalendars(context.Background(), "/cal/")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("FindCalendars: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := map[string]bool{
|
||||||
|
"/cal/read-only/": true,
|
||||||
|
"/cal/write-content/": true,
|
||||||
|
"/cal/write/": false,
|
||||||
|
"/cal/unknown/": false,
|
||||||
|
}
|
||||||
|
if len(cals) != len(want) {
|
||||||
|
t.Fatalf("got %d calendars, want %d", len(cals), len(want))
|
||||||
|
}
|
||||||
|
for _, cal := range cals {
|
||||||
|
if cal.ReadOnly != want[cal.Path] {
|
||||||
|
t.Errorf("%s: ReadOnly = %v, want %v", cal.Path, cal.ReadOnly, want[cal.Path])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -30,6 +30,8 @@ type AddressBook struct {
|
|||||||
SupportedAddressData []AddressDataType
|
SupportedAddressData []AddressDataType
|
||||||
// ReadOnly reports that the current user may only read this address book.
|
// ReadOnly reports that the current user may only read this address book.
|
||||||
// It controls the DAV:current-user-privilege-set reported by the server.
|
// It controls the DAV:current-user-privilege-set reported by the server.
|
||||||
|
// The client sets it when the server reports a
|
||||||
|
// DAV:current-user-privilege-set that doesn't grant DAV:write.
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -84,6 +84,7 @@ func (c *Client) FindAddressBooks(ctx context.Context, addressBookHomeSet string
|
|||||||
addressBookDescriptionName,
|
addressBookDescriptionName,
|
||||||
maxResourceSizeName,
|
maxResourceSizeName,
|
||||||
supportedAddressDataName,
|
supportedAddressDataName,
|
||||||
|
internal.CurrentUserPrivilegeSetName,
|
||||||
)
|
)
|
||||||
ms, err := c.ic.PropFind(ctx, addressBookHomeSet, internal.DepthOne, propfind)
|
ms, err := c.ic.PropFind(ctx, addressBookHomeSet, internal.DepthOne, propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -130,12 +131,21 @@ func (c *Client) FindAddressBooks(ctx context.Context, addressBookHomeSet string
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var privSet internal.CurrentUserPrivilegeSet
|
||||||
|
readOnly := false
|
||||||
|
if err := resp.DecodeProp(&privSet); err == nil {
|
||||||
|
readOnly = privSet.ReadOnly()
|
||||||
|
} else if !internal.IsNotFound(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
l = append(l, AddressBook{
|
l = append(l, AddressBook{
|
||||||
Path: path,
|
Path: path,
|
||||||
Name: dispName.Name,
|
Name: dispName.Name,
|
||||||
Description: desc.Description,
|
Description: desc.Description,
|
||||||
MaxResourceSize: maxResSize.Size,
|
MaxResourceSize: maxResSize.Size,
|
||||||
SupportedAddressData: decodeSupportedAddressData(&supported),
|
SupportedAddressData: decodeSupportedAddressData(&supported),
|
||||||
|
ReadOnly: readOnly,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
69
carddav/client_test.go
Normal file
69
carddav/client_test.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package carddav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const findAddressBooksReadOnlyResponse = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<d:multistatus xmlns:d="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
||||||
|
<d:response>
|
||||||
|
<d:href>/contacts/read-only/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><card:addressbook/></d:resourcetype>
|
||||||
|
<d:current-user-privilege-set>
|
||||||
|
<d:privilege><d:read/></d:privilege>
|
||||||
|
</d:current-user-privilege-set>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
<d:response>
|
||||||
|
<d:href>/contacts/all/</d:href>
|
||||||
|
<d:propstat>
|
||||||
|
<d:prop>
|
||||||
|
<d:resourcetype><d:collection/><card:addressbook/></d:resourcetype>
|
||||||
|
<d:current-user-privilege-set>
|
||||||
|
<d:privilege><d:all/></d:privilege>
|
||||||
|
<d:privilege><d:read/></d:privilege>
|
||||||
|
<d:privilege><d:write/></d:privilege>
|
||||||
|
</d:current-user-privilege-set>
|
||||||
|
</d:prop>
|
||||||
|
<d:status>HTTP/1.1 200 OK</d:status>
|
||||||
|
</d:propstat>
|
||||||
|
</d:response>
|
||||||
|
</d:multistatus>`
|
||||||
|
|
||||||
|
func TestFindAddressBooksReadOnly(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusMultiStatus)
|
||||||
|
w.Write([]byte(findAddressBooksReadOnlyResponse))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
abs, err := c.FindAddressBooks(context.Background(), "/contacts/")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("FindAddressBooks: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := map[string]bool{
|
||||||
|
"/contacts/read-only/": true,
|
||||||
|
"/contacts/all/": false,
|
||||||
|
}
|
||||||
|
if len(abs) != len(want) {
|
||||||
|
t.Fatalf("got %d address books, want %d", len(abs), len(want))
|
||||||
|
}
|
||||||
|
for _, ab := range abs {
|
||||||
|
if ab.ReadOnly != want[ab.Path] {
|
||||||
|
t.Errorf("%s: ReadOnly = %v, want %v", ab.Path, ab.ReadOnly, want[ab.Path])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -461,8 +461,8 @@ type Limit struct {
|
|||||||
|
|
||||||
// https://tools.ietf.org/html/rfc3744#section-5.4
|
// https://tools.ietf.org/html/rfc3744#section-5.4
|
||||||
type CurrentUserPrivilegeSet struct {
|
type CurrentUserPrivilegeSet struct {
|
||||||
XMLName xml.Name `xml:"DAV: current-user-privilege-set"`
|
XMLName xml.Name `xml:"DAV: current-user-privilege-set"`
|
||||||
Privilege []Privilege
|
Privilege []Privilege `xml:"DAV: privilege"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc3744#section-5.4
|
// https://tools.ietf.org/html/rfc3744#section-5.4
|
||||||
@ -481,3 +481,15 @@ func NewCurrentUserPrivilegeSet(readOnly bool) *CurrentUserPrivilegeSet {
|
|||||||
}
|
}
|
||||||
return &CurrentUserPrivilegeSet{Privilege: []Privilege{priv}}
|
return &CurrentUserPrivilegeSet{Privilege: []Privilege{priv}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadOnly reports whether the set doesn't grant DAV:write. The set lists
|
||||||
|
// aggregate privileges together with the ones they contain (RFC 3744 section
|
||||||
|
// 5.4), so DAV:write is listed whenever it is granted, also through DAV:all.
|
||||||
|
func (set *CurrentUserPrivilegeSet) ReadOnly() bool {
|
||||||
|
for _, priv := range set.Privilege {
|
||||||
|
if priv.Write != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user