Add calendar/address-book sharing backend
Introduce internal/db, a small SQLite-backed store (pure-Go
modernc.org/sqlite, no CGO) at <data_dir>/nidus.db holding
calendar_shares and addressbook_shares grant tables (owner, resource
name, shared-with user, read/write permission). This is the first step
towards user management and a web UI: a real datastore that a future
admin CLI/UI can build on, instead of the static config.yaml.
Wire sharing into the CalDAV/CardDAV backends:
- ListCalendars/ListAddressBooks now also include resources shared with
the requesting user, exposed under the synthetic local name
"<owner>~<name>" in the grantee's own home-set — no separate account,
no data copying, the object still physically lives under the owner's
store.Store namespace.
- All read paths (Get/List/QueryCalendarObjects, address book
equivalents) resolve the synthetic name back to (owner, real name) and
require any share (read or write) to exist.
- All write paths (Put/Delete object, DeleteCalendar/AddressBook)
additionally require a write-permission share; read-only shares get a
403 Forbidden.
- CreateCalendar/CreateAddressBook remain scoped to the acting user's own
namespace — sharing an existing collection is done via ShareCalendar/
ShareAddressBook, not by creating one directly in someone else's name.
Add internal/db/shares_test.go (grant/lookup/update/unshare/list
semantics) and internal/{caldav,carddav}/backend_test.go (shared
calendar/address book visibility, write permission enforcement,
unauthorized access rejection). Update README (features, new "Sharing
calendars and address books" section, project layout, dependencies) and
copilot-instructions.md to document the new package and sharing model.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Package db provides a lightweight SQLite-backed store for data that
|
||||
// doesn't fit the plain-file model used by internal/store — currently
|
||||
// calendar/address-book sharing grants. It's intentionally small: no ORM,
|
||||
// just database/sql with hand-written queries, so it stays easy to
|
||||
// extend when user management and the web UI are added later.
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
// DB wraps a SQLite connection and exposes typed helpers for the
|
||||
// application's tables.
|
||||
type DB struct {
|
||||
conn *sql.DB
|
||||
}
|
||||
|
||||
// Open opens (creating if necessary) the SQLite database at path and runs
|
||||
// schema migrations.
|
||||
func Open(path string) (*DB, error) {
|
||||
conn, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening database %q: %w", path, err)
|
||||
}
|
||||
// SQLite only supports one writer at a time; a single connection avoids
|
||||
// "database is locked" errors under concurrent access.
|
||||
conn.SetMaxOpenConns(1)
|
||||
|
||||
d := &DB{conn: conn}
|
||||
if err := d.migrate(); err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("migrating database: %w", err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Close closes the underlying connection.
|
||||
func (d *DB) Close() error {
|
||||
return d.conn.Close()
|
||||
}
|
||||
|
||||
func (d *DB) migrate() error {
|
||||
const schema = `
|
||||
CREATE TABLE IF NOT EXISTS calendar_shares (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
owner TEXT NOT NULL,
|
||||
calendar_name TEXT NOT NULL,
|
||||
shared_with TEXT NOT NULL,
|
||||
permission TEXT NOT NULL CHECK (permission IN ('read', 'write')),
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (owner, calendar_name, shared_with)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS addressbook_shares (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
owner TEXT NOT NULL,
|
||||
addressbook_name TEXT NOT NULL,
|
||||
shared_with TEXT NOT NULL,
|
||||
permission TEXT NOT NULL CHECK (permission IN ('read', 'write')),
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (owner, addressbook_name, shared_with)
|
||||
);
|
||||
`
|
||||
_, err := d.conn.Exec(schema)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Permission is the access level granted by a share.
|
||||
type Permission string
|
||||
|
||||
const (
|
||||
PermRead Permission = "read"
|
||||
PermWrite Permission = "write"
|
||||
)
|
||||
|
||||
// ErrShareNotFound is returned when revoking a share that doesn't exist.
|
||||
var ErrShareNotFound = errors.New("share not found")
|
||||
|
||||
// CalendarShare describes a grant of access to owner's calendar to another
|
||||
// user.
|
||||
type CalendarShare struct {
|
||||
Owner string
|
||||
CalendarName string
|
||||
SharedWith string
|
||||
Permission Permission
|
||||
}
|
||||
|
||||
// ShareCalendar grants sharedWith access (read or write) to owner's
|
||||
// calendar calName. Calling it again for the same (owner, calName,
|
||||
// sharedWith) updates the permission.
|
||||
func (d *DB) ShareCalendar(owner, calName, sharedWith string, perm Permission) error {
|
||||
if perm != PermRead && perm != PermWrite {
|
||||
return fmt.Errorf("invalid permission %q", perm)
|
||||
}
|
||||
_, err := d.conn.Exec(`
|
||||
INSERT INTO calendar_shares (owner, calendar_name, shared_with, permission)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT (owner, calendar_name, shared_with)
|
||||
DO UPDATE SET permission = excluded.permission`,
|
||||
owner, calName, sharedWith, string(perm))
|
||||
if err != nil {
|
||||
return fmt.Errorf("sharing calendar: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnshareCalendar revokes sharedWith's access to owner's calendar calName.
|
||||
func (d *DB) UnshareCalendar(owner, calName, sharedWith string) error {
|
||||
res, err := d.conn.Exec(`
|
||||
DELETE FROM calendar_shares
|
||||
WHERE owner = ? AND calendar_name = ? AND shared_with = ?`,
|
||||
owner, calName, sharedWith)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unsharing calendar: %w", err)
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unsharing calendar: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrShareNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SharesOfCalendar lists everyone owner's calendar calName has been shared
|
||||
// with.
|
||||
func (d *DB) SharesOfCalendar(owner, calName string) ([]CalendarShare, error) {
|
||||
rows, err := d.conn.Query(`
|
||||
SELECT owner, calendar_name, shared_with, permission
|
||||
FROM calendar_shares
|
||||
WHERE owner = ? AND calendar_name = ?
|
||||
ORDER BY shared_with`,
|
||||
owner, calName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing calendar shares: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanCalendarShares(rows)
|
||||
}
|
||||
|
||||
// CalendarsSharedWith lists all calendars (from any owner) that have been
|
||||
// shared with user.
|
||||
func (d *DB) CalendarsSharedWith(user string) ([]CalendarShare, error) {
|
||||
rows, err := d.conn.Query(`
|
||||
SELECT owner, calendar_name, shared_with, permission
|
||||
FROM calendar_shares
|
||||
WHERE shared_with = ?
|
||||
ORDER BY owner, calendar_name`,
|
||||
user)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing calendars shared with user: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanCalendarShares(rows)
|
||||
}
|
||||
|
||||
// CalendarShare looks up the share record granting user access to
|
||||
// owner's calendar calName, if any.
|
||||
func (d *DB) CalendarShareFor(owner, calName, user string) (*CalendarShare, error) {
|
||||
row := d.conn.QueryRow(`
|
||||
SELECT owner, calendar_name, shared_with, permission
|
||||
FROM calendar_shares
|
||||
WHERE owner = ? AND calendar_name = ? AND shared_with = ?`,
|
||||
owner, calName, user)
|
||||
|
||||
var s CalendarShare
|
||||
var perm string
|
||||
if err := row.Scan(&s.Owner, &s.CalendarName, &s.SharedWith, &perm); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrShareNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("looking up calendar share: %w", err)
|
||||
}
|
||||
s.Permission = Permission(perm)
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func scanCalendarShares(rows *sql.Rows) ([]CalendarShare, error) {
|
||||
var shares []CalendarShare
|
||||
for rows.Next() {
|
||||
var s CalendarShare
|
||||
var perm string
|
||||
if err := rows.Scan(&s.Owner, &s.CalendarName, &s.SharedWith, &perm); err != nil {
|
||||
return nil, fmt.Errorf("scanning calendar share: %w", err)
|
||||
}
|
||||
s.Permission = Permission(perm)
|
||||
shares = append(shares, s)
|
||||
}
|
||||
return shares, rows.Err()
|
||||
}
|
||||
|
||||
// AddressBookShare describes a grant of access to owner's address book to
|
||||
// another user.
|
||||
type AddressBookShare struct {
|
||||
Owner string
|
||||
AddressBookName string
|
||||
SharedWith string
|
||||
Permission Permission
|
||||
}
|
||||
|
||||
// ShareAddressBook grants sharedWith access (read or write) to owner's
|
||||
// address book bookName.
|
||||
func (d *DB) ShareAddressBook(owner, bookName, sharedWith string, perm Permission) error {
|
||||
if perm != PermRead && perm != PermWrite {
|
||||
return fmt.Errorf("invalid permission %q", perm)
|
||||
}
|
||||
_, err := d.conn.Exec(`
|
||||
INSERT INTO addressbook_shares (owner, addressbook_name, shared_with, permission)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT (owner, addressbook_name, shared_with)
|
||||
DO UPDATE SET permission = excluded.permission`,
|
||||
owner, bookName, sharedWith, string(perm))
|
||||
if err != nil {
|
||||
return fmt.Errorf("sharing address book: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnshareAddressBook revokes sharedWith's access to owner's address book
|
||||
// bookName.
|
||||
func (d *DB) UnshareAddressBook(owner, bookName, sharedWith string) error {
|
||||
res, err := d.conn.Exec(`
|
||||
DELETE FROM addressbook_shares
|
||||
WHERE owner = ? AND addressbook_name = ? AND shared_with = ?`,
|
||||
owner, bookName, sharedWith)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unsharing address book: %w", err)
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unsharing address book: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrShareNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddressBooksSharedWith lists all address books (from any owner) that
|
||||
// have been shared with user.
|
||||
func (d *DB) AddressBooksSharedWith(user string) ([]AddressBookShare, error) {
|
||||
rows, err := d.conn.Query(`
|
||||
SELECT owner, addressbook_name, shared_with, permission
|
||||
FROM addressbook_shares
|
||||
WHERE shared_with = ?
|
||||
ORDER BY owner, addressbook_name`,
|
||||
user)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing address books shared with user: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var shares []AddressBookShare
|
||||
for rows.Next() {
|
||||
var s AddressBookShare
|
||||
var perm string
|
||||
if err := rows.Scan(&s.Owner, &s.AddressBookName, &s.SharedWith, &perm); err != nil {
|
||||
return nil, fmt.Errorf("scanning address book share: %w", err)
|
||||
}
|
||||
s.Permission = Permission(perm)
|
||||
shares = append(shares, s)
|
||||
}
|
||||
return shares, rows.Err()
|
||||
}
|
||||
|
||||
// AddressBookShareFor looks up the share record granting user access to
|
||||
// owner's address book bookName, if any.
|
||||
func (d *DB) AddressBookShareFor(owner, bookName, user string) (*AddressBookShare, error) {
|
||||
row := d.conn.QueryRow(`
|
||||
SELECT owner, addressbook_name, shared_with, permission
|
||||
FROM addressbook_shares
|
||||
WHERE owner = ? AND addressbook_name = ? AND shared_with = ?`,
|
||||
owner, bookName, user)
|
||||
|
||||
var s AddressBookShare
|
||||
var perm string
|
||||
if err := row.Scan(&s.Owner, &s.AddressBookName, &s.SharedWith, &perm); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrShareNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("looking up address book share: %w", err)
|
||||
}
|
||||
s.Permission = Permission(perm)
|
||||
return &s, nil
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func openTestDB(t *testing.T) *DB {
|
||||
t.Helper()
|
||||
dbase, err := Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { dbase.Close() })
|
||||
return dbase
|
||||
}
|
||||
|
||||
func TestShareCalendarAndLookup(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
if err := dbase.ShareCalendar("alice", "work", "bob", PermRead); err != nil {
|
||||
t.Fatalf("ShareCalendar: %v", err)
|
||||
}
|
||||
|
||||
share, err := dbase.CalendarShareFor("alice", "work", "bob")
|
||||
if err != nil {
|
||||
t.Fatalf("CalendarShareFor: %v", err)
|
||||
}
|
||||
if share.Permission != PermRead {
|
||||
t.Errorf("Permission = %q, want %q", share.Permission, PermRead)
|
||||
}
|
||||
|
||||
// Re-sharing with a different permission updates in place rather than
|
||||
// erroring or duplicating.
|
||||
if err := dbase.ShareCalendar("alice", "work", "bob", PermWrite); err != nil {
|
||||
t.Fatalf("ShareCalendar (update): %v", err)
|
||||
}
|
||||
share, err = dbase.CalendarShareFor("alice", "work", "bob")
|
||||
if err != nil {
|
||||
t.Fatalf("CalendarShareFor after update: %v", err)
|
||||
}
|
||||
if share.Permission != PermWrite {
|
||||
t.Errorf("Permission after update = %q, want %q", share.Permission, PermWrite)
|
||||
}
|
||||
|
||||
shares, err := dbase.SharesOfCalendar("alice", "work")
|
||||
if err != nil {
|
||||
t.Fatalf("SharesOfCalendar: %v", err)
|
||||
}
|
||||
if len(shares) != 1 || shares[0].SharedWith != "bob" {
|
||||
t.Errorf("SharesOfCalendar = %+v, want single share with bob", shares)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarShareForNotFound(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
_, err := dbase.CalendarShareFor("alice", "work", "bob")
|
||||
if !errors.Is(err, ErrShareNotFound) {
|
||||
t.Errorf("err = %v, want ErrShareNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnshareCalendar(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
if err := dbase.ShareCalendar("alice", "work", "bob", PermRead); err != nil {
|
||||
t.Fatalf("ShareCalendar: %v", err)
|
||||
}
|
||||
if err := dbase.UnshareCalendar("alice", "work", "bob"); err != nil {
|
||||
t.Fatalf("UnshareCalendar: %v", err)
|
||||
}
|
||||
if _, err := dbase.CalendarShareFor("alice", "work", "bob"); !errors.Is(err, ErrShareNotFound) {
|
||||
t.Errorf("share still present after unshare: err = %v", err)
|
||||
}
|
||||
|
||||
// Unsharing a non-existent share reports ErrShareNotFound.
|
||||
if err := dbase.UnshareCalendar("alice", "work", "bob"); !errors.Is(err, ErrShareNotFound) {
|
||||
t.Errorf("UnshareCalendar (already gone) = %v, want ErrShareNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarsSharedWith(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
if err := dbase.ShareCalendar("alice", "work", "bob", PermRead); err != nil {
|
||||
t.Fatalf("ShareCalendar: %v", err)
|
||||
}
|
||||
if err := dbase.ShareCalendar("carol", "family", "bob", PermWrite); err != nil {
|
||||
t.Fatalf("ShareCalendar: %v", err)
|
||||
}
|
||||
// A share for a different user shouldn't show up for bob.
|
||||
if err := dbase.ShareCalendar("alice", "personal", "dave", PermRead); err != nil {
|
||||
t.Fatalf("ShareCalendar: %v", err)
|
||||
}
|
||||
|
||||
shares, err := dbase.CalendarsSharedWith("bob")
|
||||
if err != nil {
|
||||
t.Fatalf("CalendarsSharedWith: %v", err)
|
||||
}
|
||||
if len(shares) != 2 {
|
||||
t.Fatalf("len(shares) = %d, want 2", len(shares))
|
||||
}
|
||||
}
|
||||
|
||||
func TestShareCalendarInvalidPermission(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
if err := dbase.ShareCalendar("alice", "work", "bob", Permission("admin")); err == nil {
|
||||
t.Error("expected error for invalid permission, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShareAddressBookAndLookup(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
if err := dbase.ShareAddressBook("alice", "contacts", "bob", PermRead); err != nil {
|
||||
t.Fatalf("ShareAddressBook: %v", err)
|
||||
}
|
||||
|
||||
share, err := dbase.AddressBookShareFor("alice", "contacts", "bob")
|
||||
if err != nil {
|
||||
t.Fatalf("AddressBookShareFor: %v", err)
|
||||
}
|
||||
if share.Permission != PermRead {
|
||||
t.Errorf("Permission = %q, want %q", share.Permission, PermRead)
|
||||
}
|
||||
|
||||
if err := dbase.UnshareAddressBook("alice", "contacts", "bob"); err != nil {
|
||||
t.Fatalf("UnshareAddressBook: %v", err)
|
||||
}
|
||||
if _, err := dbase.AddressBookShareFor("alice", "contacts", "bob"); !errors.Is(err, ErrShareNotFound) {
|
||||
t.Errorf("share still present after unshare: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddressBooksSharedWith(t *testing.T) {
|
||||
dbase := openTestDB(t)
|
||||
|
||||
if err := dbase.ShareAddressBook("alice", "contacts", "bob", PermRead); err != nil {
|
||||
t.Fatalf("ShareAddressBook: %v", err)
|
||||
}
|
||||
if err := dbase.ShareAddressBook("carol", "friends", "bob", PermWrite); err != nil {
|
||||
t.Fatalf("ShareAddressBook: %v", err)
|
||||
}
|
||||
|
||||
shares, err := dbase.AddressBooksSharedWith("bob")
|
||||
if err != nil {
|
||||
t.Fatalf("AddressBooksSharedWith: %v", err)
|
||||
}
|
||||
if len(shares) != 2 {
|
||||
t.Fatalf("len(shares) = %d, want 2", len(shares))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user