release 1.1.0 : sync V2 (upload, download, purge, placements, rétention, groupes)
Release / release-apk (push) Successful in 9m25s
Release / release-docker (push) Successful in 33s
CI / docker (push) Successful in 7s
CI / lint-backend (push) Successful in 1m29s
CI / lint-mobile (push) Failing after 7m46s
CI / test-backend (push) Successful in 1m30s
CI / test-mobile (push) Successful in 9m27s
CI / build-backend (push) Successful in 47s
Release / release-apk (push) Successful in 9m25s
Release / release-docker (push) Successful in 33s
CI / docker (push) Successful in 7s
CI / lint-backend (push) Successful in 1m29s
CI / lint-mobile (push) Failing after 7m46s
CI / test-backend (push) Successful in 1m30s
CI / test-mobile (push) Successful in 9m27s
CI / build-backend (push) Successful in 47s
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Group is a named set of users (V2-6) : sharing to a group grants every
|
||||
// member. One level only (no nested groups). The creator owns the group
|
||||
// (member management + deletion) ; membership confers no admin rights.
|
||||
type Group struct {
|
||||
ID string
|
||||
Name string
|
||||
CreatedBy string
|
||||
}
|
||||
|
||||
// Groups implements group persistence.
|
||||
type Groups struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Create stores a group owned by ownerID and returns its id.
|
||||
func (g *Groups) Create(name, ownerID string) (string, error) {
|
||||
id := NewID()
|
||||
_, err := g.DB.Exec(
|
||||
`INSERT INTO groups (id, name, created_by) VALUES ($1, $2, $3)`,
|
||||
id, name, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Get returns a group by id. No-rows → ErrNotFound.
|
||||
func (g *Groups) Get(groupID string) (Group, error) {
|
||||
var group Group
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT id, name, created_by FROM groups WHERE id = $1`,
|
||||
groupID,
|
||||
).Scan(&group.ID, &group.Name, &group.CreatedBy)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Group{}, ErrNotFound
|
||||
}
|
||||
return group, err
|
||||
}
|
||||
|
||||
// IsOwner reports whether userID created the group.
|
||||
func (g *Groups) IsOwner(groupID, userID string) (bool, error) {
|
||||
var n int
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT COUNT(*) FROM groups WHERE id = $1 AND created_by = $2`,
|
||||
groupID, userID,
|
||||
).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// IsMember reports group membership (owner excluded : ownership is separate).
|
||||
func (g *Groups) IsMember(groupID, userID string) (bool, error) {
|
||||
var n int
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT COUNT(*) FROM group_members WHERE group_id = $1 AND user_id = $2`,
|
||||
groupID, userID,
|
||||
).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// GroupWithRole is a group listing row (owned or joined).
|
||||
type GroupWithRole struct {
|
||||
Group
|
||||
Role string // "owner" | "member"
|
||||
}
|
||||
|
||||
// ListForUser returns groups the user owns or joins, owned first.
|
||||
func (g *Groups) ListForUser(userID string) ([]GroupWithRole, error) {
|
||||
rows, err := g.DB.Query(
|
||||
`SELECT id, name, created_by,
|
||||
CASE WHEN created_by = $1 THEN 'owner' ELSE 'member' END AS role
|
||||
FROM groups
|
||||
WHERE created_by = $1 OR id IN (SELECT group_id FROM group_members WHERE user_id = $1)
|
||||
ORDER BY created_at ASC`,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []GroupWithRole
|
||||
for rows.Next() {
|
||||
var gr GroupWithRole
|
||||
if err := rows.Scan(&gr.ID, &gr.Name, &gr.CreatedBy, &gr.Role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, gr)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AddMember inserts a membership. Idempotent on replay (ON CONFLICT DO
|
||||
// NOTHING) ; returns rows-affected style via ErrNotFound when the group or
|
||||
// the user does not exist (FK violation mapped).
|
||||
func (g *Groups) AddMember(groupID, userID string) error {
|
||||
_, err := g.DB.Exec(
|
||||
`INSERT INTO group_members (group_id, user_id) VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
groupID, userID,
|
||||
)
|
||||
if isForeignKeyViolation(err) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveMember deletes a membership. Missing row → ErrNotFound.
|
||||
func (g *Groups) RemoveMember(groupID, userID string) error {
|
||||
result, err := g.DB.Exec(
|
||||
`DELETE FROM group_members WHERE group_id = $1 AND user_id = $2`,
|
||||
groupID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Member is a group membership row (owner view).
|
||||
type Member struct {
|
||||
UserID string
|
||||
Username string
|
||||
}
|
||||
|
||||
// ListMembers returns memberships of a group, ordered by username.
|
||||
func (g *Groups) ListMembers(groupID string) ([]Member, error) {
|
||||
rows, err := g.DB.Query(
|
||||
`SELECT u.id, COALESCE(u.username, u.id)
|
||||
FROM group_members gm
|
||||
JOIN users u ON u.id = gm.user_id AND u.deleted_at IS NULL
|
||||
WHERE gm.group_id = $1
|
||||
ORDER BY COALESCE(u.username, u.id) ASC`,
|
||||
groupID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Member
|
||||
for rows.Next() {
|
||||
var m Member
|
||||
if err := rows.Scan(&m.UserID, &m.Username); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Errors specific to groups.
|
||||
var ErrGroupNotFound = errors.New("group not found")
|
||||
|
||||
// Delete removes a group (owner only — caller checks). Shares targeting it
|
||||
// cascade (FK), placements are untouched (device-scoped, not group-scoped).
|
||||
func (g *Groups) Delete(groupID, ownerID string) error {
|
||||
result, err := g.DB.Exec(
|
||||
`DELETE FROM groups WHERE id = $1 AND created_by = $2`,
|
||||
groupID, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -13,6 +13,7 @@ type Repository struct {
|
||||
Users *Users
|
||||
Shares *Shares
|
||||
Placements *Placements
|
||||
Groups *Groups
|
||||
}
|
||||
|
||||
func NewRepository(conn *sql.DB) *Repository {
|
||||
@@ -24,5 +25,6 @@ func NewRepository(conn *sql.DB) *Repository {
|
||||
Users: &Users{DB: conn},
|
||||
Shares: &Shares{DB: conn},
|
||||
Placements: &Placements{DB: conn},
|
||||
Groups: &Groups{DB: conn},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ const grantedCTE = `
|
||||
sh.inherit, 0
|
||||
FROM shares sh
|
||||
JOIN resources r ON r.resource_id = sh.resource_id
|
||||
WHERE sh.grantee_user_id = $1 AND sh.revoked_at IS NULL
|
||||
WHERE ` + granteeMatch + ` AND sh.revoked_at IS NULL
|
||||
AND (sh.expires_at IS NULL OR sh.expires_at > NOW())
|
||||
AND r.deleted_at IS NULL
|
||||
),
|
||||
|
||||
@@ -71,11 +71,19 @@ type ResourceAccess struct {
|
||||
Inherit bool
|
||||
}
|
||||
|
||||
// Shares implements share persistence for user-to-user grants.
|
||||
// Shares implements share persistence for user-to-user AND user-to-group
|
||||
// grants (V2-6). A grant targets exactly one principal (shares XOR CHECK) :
|
||||
// `grantee_user_id` for a user, `grantee_group_id` for a group (every member
|
||||
// inherits, one level, no nesting).
|
||||
type Shares struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// granteeMatch matches the grants visible to a user: direct user grants plus
|
||||
// grants to any group they belong to. `$1` is always the user id in the
|
||||
// enclosing query (snapshot, enforcement, visibility CTEs).
|
||||
const granteeMatch = `(sh.grantee_user_id = $1 OR sh.grantee_group_id IN (SELECT group_id FROM group_members WHERE user_id = $1))`
|
||||
|
||||
// Upsert inserts or updates an active share. On conflict (active row for
|
||||
// same resource + grantee) the access, inherit, and expires_at are updated.
|
||||
func (s *Shares) Upsert(resourceID, granteeUserID, access string, inherit bool, expiresAt *time.Time, createdBy string) error {
|
||||
@@ -96,12 +104,13 @@ func (s *Shares) Upsert(resourceID, granteeUserID, access string, inherit bool,
|
||||
return err
|
||||
}
|
||||
|
||||
// Revoke soft-revokes the active share between a resource and a grantee.
|
||||
func (s *Shares) Revoke(resourceID, granteeUserID string) error {
|
||||
// Revoke soft-revokes the active share between a resource and a grantee
|
||||
// (user id or group id — ids are unguessable 32-hex, globally unique).
|
||||
func (s *Shares) Revoke(resourceID, granteeID string) error {
|
||||
result, err := s.DB.Exec(
|
||||
`UPDATE shares SET revoked_at = NOW(), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND grantee_user_id = $2 AND revoked_at IS NULL`,
|
||||
resourceID, granteeUserID,
|
||||
WHERE resource_id = $1 AND (grantee_user_id = $2 OR grantee_group_id = $2) AND revoked_at IS NULL`,
|
||||
resourceID, granteeID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -153,7 +162,7 @@ seeded(id, type, updated_at, parent_id, grant_rank, created_by, expires_at, gran
|
||||
sh.created_by, sh.expires_at, sh.inherit, r.name, TRUE
|
||||
FROM shares sh
|
||||
JOIN resources r ON r.resource_id = sh.resource_id
|
||||
WHERE sh.grantee_user_id = $1 AND sh.revoked_at IS NULL
|
||||
WHERE `+granteeMatch+` AND sh.revoked_at IS NULL
|
||||
AND (sh.expires_at IS NULL OR sh.expires_at > NOW())
|
||||
AND r.deleted_at IS NULL
|
||||
),
|
||||
@@ -263,7 +272,7 @@ direct AS (
|
||||
SELECT CASE sh.access WHEN 'viewer' THEN 1 WHEN 'commenter' THEN 2 WHEN 'editor' THEN 3 ELSE 0 END AS rank
|
||||
FROM shares sh
|
||||
JOIN resources r ON r.resource_id = sh.resource_id
|
||||
WHERE sh.resource_id = $2 AND sh.grantee_user_id = $1
|
||||
WHERE sh.resource_id = $2 AND `+granteeMatch+`
|
||||
AND sh.revoked_at IS NULL
|
||||
AND (sh.expires_at IS NULL OR sh.expires_at > NOW())
|
||||
AND r.deleted_at IS NULL
|
||||
@@ -276,7 +285,7 @@ inherited(id, grant_rank, depth) AS (
|
||||
SELECT r.parent_id,
|
||||
COALESCE((SELECT CASE sh.access WHEN 'viewer' THEN 1 WHEN 'commenter' THEN 2 WHEN 'editor' THEN 3 ELSE 0 END
|
||||
FROM shares sh
|
||||
WHERE sh.resource_id = r.parent_id AND sh.grantee_user_id = $1
|
||||
WHERE sh.resource_id = r.parent_id AND `+granteeMatch+`
|
||||
AND sh.revoked_at IS NULL AND sh.inherit
|
||||
AND (sh.expires_at IS NULL OR sh.expires_at > NOW())), 0),
|
||||
0
|
||||
@@ -286,7 +295,7 @@ inherited(id, grant_rank, depth) AS (
|
||||
SELECT r.parent_id,
|
||||
COALESCE((SELECT CASE sh.access WHEN 'viewer' THEN 1 WHEN 'commenter' THEN 2 WHEN 'editor' THEN 3 ELSE 0 END
|
||||
FROM shares sh
|
||||
WHERE sh.resource_id = ag.id AND sh.grantee_user_id = $1
|
||||
WHERE sh.resource_id = ag.id AND `+granteeMatch+`
|
||||
AND sh.revoked_at IS NULL AND sh.inherit
|
||||
AND (sh.expires_at IS NULL OR sh.expires_at > NOW())), 0),
|
||||
ag.depth + 1
|
||||
@@ -455,14 +464,44 @@ func (s *Shares) ListActiveLinksForResource(resourceID string) ([]LinkInfo, erro
|
||||
return links, rows.Err()
|
||||
}
|
||||
|
||||
// ShareGrant is one active user-to-user grant on a resource (owner view).
|
||||
// ShareGrant is one active grant on a resource (owner view). Exactly one of
|
||||
// GranteeUserID / GranteeGroupID is set (mirrors the shares XOR CHECK).
|
||||
type ShareGrant struct {
|
||||
GranteeUserID string
|
||||
Username string
|
||||
Access string
|
||||
Inherit bool
|
||||
ExpiresAt *time.Time
|
||||
UpdatedAt time.Time
|
||||
GranteeUserID string
|
||||
GranteeGroupID string
|
||||
Username string
|
||||
Access string
|
||||
Inherit bool
|
||||
ExpiresAt *time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// GrantKind returns "group" for group-targeted grants, "user" otherwise.
|
||||
func (g ShareGrant) GrantKind() string {
|
||||
if g.GranteeGroupID != "" {
|
||||
return "group"
|
||||
}
|
||||
return "user"
|
||||
}
|
||||
|
||||
// UpsertGroup inserts or updates an active group-targeted share. Same
|
||||
// idempotent semantics as Upsert, keyed by (resource, group).
|
||||
func (s *Shares) UpsertGroup(resourceID, groupID, access string, inherit bool, expiresAt *time.Time, createdBy string) error {
|
||||
var expires any
|
||||
if expiresAt != nil {
|
||||
expires = *expiresAt
|
||||
}
|
||||
_, err := s.DB.Exec(
|
||||
`INSERT INTO shares (id, resource_id, grantee_group_id, access, inherit, created_by, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (resource_id, grantee_group_id) WHERE revoked_at IS NULL AND grantee_group_id IS NOT NULL
|
||||
DO UPDATE SET access = EXCLUDED.access,
|
||||
inherit = EXCLUDED.inherit,
|
||||
expires_at = EXCLUDED.expires_at,
|
||||
updated_at = NOW()`,
|
||||
NewID(), resourceID, groupID, access, inherit, createdBy, expires,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListActiveForResource returns the active (non-revoked) grants on a resource,
|
||||
@@ -470,10 +509,12 @@ type ShareGrant struct {
|
||||
// the caller filters display. Empty (not error) when nothing is shared.
|
||||
func (s *Shares) ListActiveForResource(resourceID string) ([]ShareGrant, error) {
|
||||
rows, err := s.DB.Query(
|
||||
`SELECT s.grantee_user_id, COALESCE(u.username, s.grantee_user_id),
|
||||
`SELECT s.grantee_user_id, s.grantee_group_id,
|
||||
COALESCE(u.username, gr.name, COALESCE(s.grantee_user_id, s.grantee_group_id)),
|
||||
s.access, s.inherit, s.expires_at, s.updated_at
|
||||
FROM shares s
|
||||
LEFT JOIN users u ON u.id = s.grantee_user_id AND u.deleted_at IS NULL
|
||||
LEFT JOIN groups gr ON gr.id = s.grantee_group_id
|
||||
WHERE s.resource_id = $1 AND s.revoked_at IS NULL
|
||||
ORDER BY s.created_at ASC`,
|
||||
resourceID,
|
||||
@@ -485,12 +526,20 @@ func (s *Shares) ListActiveForResource(resourceID string) ([]ShareGrant, error)
|
||||
var grants []ShareGrant
|
||||
for rows.Next() {
|
||||
var (
|
||||
g ShareGrant
|
||||
expiresAt sql.NullTime
|
||||
g ShareGrant
|
||||
granteeUser sql.NullString
|
||||
granteeGrp sql.NullString
|
||||
expiresAt sql.NullTime
|
||||
)
|
||||
if err := rows.Scan(&g.GranteeUserID, &g.Username, &g.Access, &g.Inherit, &expiresAt, &g.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&granteeUser, &granteeGrp, &g.Username, &g.Access, &g.Inherit, &expiresAt, &g.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if granteeUser.Valid {
|
||||
g.GranteeUserID = granteeUser.String
|
||||
}
|
||||
if granteeGrp.Valid {
|
||||
g.GranteeGroupID = granteeGrp.String
|
||||
}
|
||||
if expiresAt.Valid {
|
||||
g.ExpiresAt = &expiresAt.Time
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user