Sharing backend: shares CRUD + ACL enforcement everywhere (#16)
Build image / build-and-push (push) Successful in 4s

Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #35.
This commit is contained in:
2026-07-19 03:49:54 +00:00
committed by steve
parent 48ba08e8f2
commit 2a86f87b50
11 changed files with 733 additions and 24 deletions
+29 -8
View File
@@ -23,7 +23,20 @@ func scanGarden(s scanner) (*domain.Garden, error) {
return &g, nil
}
// maxGardensListed caps ListGardensForOwner as a defensive backstop against a
// scanGardenWithRole reads a garden row plus a trailing my_role column into the
// computed Garden.MyRole field (used by the actor-scoped list).
func scanGardenWithRole(s scanner) (*domain.Garden, error) {
var g domain.Garden
if err := s.Scan(
&g.ID, &g.OwnerID, &g.Name, &g.WidthCM, &g.HeightCM,
&g.UnitPref, &g.Notes, &g.Version, &g.CreatedAt, &g.UpdatedAt, &g.MyRole,
); err != nil {
return nil, err
}
return &g, nil
}
// maxGardensListed caps the gardens list as a defensive backstop against a
// pathologically large result. At household scale a user has a handful of
// gardens, so this is never hit; genuine pagination is post-v1 if ever needed.
const maxGardensListed = 1000
@@ -56,13 +69,21 @@ func (d *DB) GetGarden(ctx context.Context, id int64) (*domain.Garden, error) {
return g, nil
}
// ListGardensForOwner returns the gardens owned by ownerID, newest first. The
// slice is always non-nil (an empty list, not null). Shared-with-me gardens are
// added in #16.
func (d *DB) ListGardensForOwner(ctx context.Context, ownerID int64) ([]domain.Garden, error) {
// ListGardensForActor returns the gardens an actor can see — those they own
// (my_role "owner") plus those shared with them (my_role = the share's role) —
// newest first. Always a non-nil slice.
func (d *DB) ListGardensForActor(ctx context.Context, actorID int64) ([]domain.Garden, error) {
rows, err := d.sql.QueryContext(ctx,
`SELECT `+gardenColumns+` FROM gardens WHERE owner_id = ? ORDER BY created_at DESC, id DESC LIMIT ?`,
ownerID, maxGardensListed,
`SELECT `+gardenColumns+`, my_role FROM (
SELECT `+gardenColumns+`, 'owner' AS my_role FROM gardens WHERE owner_id = ?
UNION ALL
SELECT `+qualifyColumns("g", gardenColumns)+`, s.role AS my_role
FROM gardens g JOIN garden_shares s ON s.garden_id = g.id
WHERE s.user_id = ?
)
ORDER BY created_at DESC, id DESC
LIMIT ?`,
actorID, actorID, maxGardensListed,
)
if err != nil {
return nil, fmt.Errorf("store: list gardens: %w", err)
@@ -71,7 +92,7 @@ func (d *DB) ListGardensForOwner(ctx context.Context, ownerID int64) ([]domain.G
gardens := []domain.Garden{}
for rows.Next() {
g, err := scanGarden(rows)
g, err := scanGardenWithRole(rows)
if err != nil {
return nil, fmt.Errorf("store: scan garden: %w", err)
}