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:
@@ -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