Move users, calendars, and address books from config.yaml into the database

BREAKING CHANGE: the users:/config-based collection setup is gone. All
user, calendar, and address-book data now lives in the SQLite DB
(internal/db) and is managed exclusively via nidusctl or the web UI.
Existing deployments must recreate their users after upgrading:
  nidusctl user create <username>
  nidusctl calendar create <username> <name>
  nidusctl addressbook create <username> <name>

- internal/db: new users, calendars, addressbooks tables with FK cascade
  delete; foreign_keys pragma enabled; internal/db/users.go implements
  full CRUD + bcrypt auth (CreateUser, VerifyPassword, ListUsers,
  CreateCalendar/AddressBook, etc).
- internal/config: removed Users/UserConfig entirely.
- internal/auth: Basic Auth now checks credentials via db.DB instead of
  cfg.Users.
- internal/caldav, internal/carddav: ListCalendars/ListAddressBooks and
  Create/Delete now backed by the DB.
- internal/web: login uses db.VerifyPassword; new resources.go adds
  create/delete handlers for calendars/address books at
  /web/resources/{calendar,addressbook}; dashboard gained create forms
  and per-card delete buttons (templ + htmx, no hyperscript).
- tools/nidusctl: new user create/delete/list/passwd commands (masked
  interactive password prompt via golang.org/x/term) plus create/delete/
  list subcommands for calendar/addressbook.
- cmd/server/main.go: pre-creates on-disk collections from the DB at
  startup instead of cfg.Users; warns when no users exist yet.
- Updated tests to seed data via the DB; added resources_test.go for the
  new web UI handlers.
- README.md and .github/copilot-instructions.md updated to document the
  new nidusctl commands and the DB-backed architecture.

Verified end-to-end against a live test server: nidusctl user/calendar/
addressbook create, DAV Basic Auth PROPFIND, web login, dashboard
rendering, and web UI create/delete of resources all confirmed working.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-08-19 12:43:34 +02:00
co-authored by Copilot
parent 3dc49b6b22
commit 7d4f28de3c
25 changed files with 1440 additions and 365 deletions
+74 -63
View File
@@ -2,6 +2,7 @@ package web
import (
"context"
"fmt"
"net/http"
"github.com/yourusername/caldav-server/internal/db"
@@ -10,83 +11,93 @@ import (
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
username := userFromContext(r.Context())
user, ok := s.cfg.Users[username]
if !ok {
http.Error(w, "user not found in configuration", http.StatusInternalServerError)
resources, err := s.resourceCards(username)
if err != nil {
s.logger.Error("listing resources", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
var resources []templates.ResourceCard
for _, calName := range user.Calendars {
card := templates.ResourceCard{Kind: "calendar", Name: calName}
if s.dbase != nil {
shares, err := s.dbase.SharesOfCalendar(username, calName)
if err != nil {
s.logger.Warn("listing calendar shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: calName,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
}
resources = append(resources, card)
}
for _, bookName := range user.AddressBooks {
card := templates.ResourceCard{Kind: "addressbook", Name: bookName}
if s.dbase != nil {
shares, err := s.dbase.SharesOfAddressBook(username, bookName)
if err != nil {
s.logger.Warn("listing address book shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: bookName,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
}
resources = append(resources, card)
}
var sharedWithMe []templates.SharedWithMeItem
if s.dbase != nil {
calShares, err := s.dbase.CalendarsSharedWith(username)
if err != nil {
s.logger.Warn("listing calendars shared with user", "error", err)
}
for _, sh := range calShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "calendar", Owner: sh.Owner, Name: sh.CalendarName, Permission: string(sh.Permission),
})
}
bookShares, err := s.dbase.AddressBooksSharedWith(username)
if err != nil {
s.logger.Warn("listing address books shared with user", "error", err)
}
for _, sh := range bookShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "addressbook", Owner: sh.Owner, Name: sh.AddressBookName, Permission: string(sh.Permission),
})
}
calShares, err := s.dbase.CalendarsSharedWith(username)
if err != nil {
s.logger.Warn("listing calendars shared with user", "error", err)
}
for _, sh := range calShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "calendar", Owner: sh.Owner, Name: sh.CalendarName, Permission: string(sh.Permission),
})
}
bookShares, err := s.dbase.AddressBooksSharedWith(username)
if err != nil {
s.logger.Warn("listing address books shared with user", "error", err)
}
for _, sh := range bookShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "addressbook", Owner: sh.Owner, Name: sh.AddressBookName, Permission: string(sh.Permission),
})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_ = templates.Dashboard(username, resources, sharedWithMe).Render(context.Background(), w)
}
// resourceCards builds the full list of ResourceCards (calendars, then
// address books) owned by username, each with its current shares — used
// both for the initial dashboard render and to re-render the whole
// #resources list after a create/delete (since the set of cards changes,
// unlike a share update which only changes one card's contents).
func (s *Server) resourceCards(username string) ([]templates.ResourceCard, error) {
var resources []templates.ResourceCard
calNames, err := s.dbase.ListCalendars(username)
if err != nil {
return nil, fmt.Errorf("listing calendars: %w", err)
}
for _, calName := range calNames {
card := templates.ResourceCard{Kind: "calendar", Name: calName}
shares, err := s.dbase.SharesOfCalendar(username, calName)
if err != nil {
s.logger.Warn("listing calendar shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: calName,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
resources = append(resources, card)
}
bookNames, err := s.dbase.ListAddressBooks(username)
if err != nil {
return nil, fmt.Errorf("listing address books: %w", err)
}
for _, bookName := range bookNames {
card := templates.ResourceCard{Kind: "addressbook", Name: bookName}
shares, err := s.dbase.SharesOfAddressBook(username, bookName)
if err != nil {
s.logger.Warn("listing address book shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: bookName,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
resources = append(resources, card)
}
return resources, nil
}
// resourceCardFor rebuilds a single ResourceCard (used to re-render just
// the card an htmx request just changed, for partial updates).
func (s *Server) resourceCardFor(username, kind, name string) (templates.ResourceCard, error) {
card := templates.ResourceCard{Kind: kind, Name: name}
if s.dbase == nil {
return card, nil
}
var shares []templates.ShareRow
if kind == "calendar" {