api use
CI / lint-backend (push) Successful in 1m24s
CI / lint-mobile (push) Failing after 7m28s
CI / test-backend (push) Successful in 1m25s
CI / test-mobile (push) Successful in 9m9s
CI / build-backend (push) Successful in 45s
CI / docker (push) Successful in 33s
CI / lint-backend (push) Successful in 1m24s
CI / lint-mobile (push) Failing after 7m28s
CI / test-backend (push) Successful in 1m25s
CI / test-mobile (push) Successful in 9m9s
CI / build-backend (push) Successful in 45s
CI / docker (push) Successful in 33s
This commit is contained in:
@@ -18,6 +18,8 @@ var expectedRoutes = []string{
|
||||
"GET /api/v1/files/:id",
|
||||
"GET /api/v1/files/:id/ocr",
|
||||
"GET /api/v1/files/:id/content",
|
||||
"GET /api/v1/files/:id/shares",
|
||||
"GET /api/v1/files/:id/links",
|
||||
"DELETE /api/v1/files/:id",
|
||||
"POST /api/v1/files/upload",
|
||||
|
||||
|
||||
@@ -747,3 +747,86 @@ func TestFilesContentStreaming(t *testing.T) {
|
||||
t.Errorf("content-sans-token: attendu 401, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesSharesLinksListing(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "sl"), "shares-list-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "sg"), "shares-list-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "sh"), "shares-list-password-c", outsiderDevice)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "shared.txt", "", 4, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
|
||||
// Vide au départ (pas d'erreur).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env := expectOK(t, rec, "shares-empty")
|
||||
var emptyShares []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyShares); err != nil {
|
||||
t.Fatalf("shares-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyShares) != 0 {
|
||||
t.Errorf("shares-empty = %d grants, attendu 0", len(emptyShares))
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-empty")
|
||||
var emptyLinks []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyLinks); err != nil {
|
||||
t.Fatalf("links-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyLinks) != 0 {
|
||||
t.Errorf("links-empty = %d links, attendu 0", len(emptyLinks))
|
||||
}
|
||||
|
||||
// Un grant + un lien côté serveur (comme après des ops `share`/`create_link`).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "editor", true, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
linkToken := repository.NewID()
|
||||
if err := repo.Shares.CreateLink(linkToken, fileID, "viewer", nil, owner); err != nil {
|
||||
t.Fatalf("create link: %v", err)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "shares-owner")
|
||||
var grants []struct {
|
||||
GranteeUserID string `json:"granteeUserId"`
|
||||
Username string `json:"username"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &grants); err != nil {
|
||||
t.Fatalf("shares-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(grants) != 1 || grants[0].GranteeUserID != grantee || grants[0].Access != "editor" || grants[0].Username == "" {
|
||||
t.Errorf("shares-owner = %+v, attendu 1 grant editor du grantee", grants)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-owner")
|
||||
var links []struct {
|
||||
Token string `json:"token"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &links); err != nil {
|
||||
t.Fatalf("links-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(links) != 1 || links[0].Token != linkToken || links[0].Access != "viewer" {
|
||||
t.Errorf("links-owner = %+v, attendu 1 lien viewer", links)
|
||||
}
|
||||
|
||||
// Granté et outsider : 404 (pas d'énumération).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "links-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-outsider")
|
||||
|
||||
// ID invalide → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/shares", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-invalid-id")
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||
protected.GET("/files/:id/content", FilesContent)
|
||||
protected.GET("/files/:id/shares", SharesList)
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
protected.DELETE("/files/:id", FilesDelete)
|
||||
protected.POST("/files/upload", FilesUpload)
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// SharesList exposes the active user-to-user grants of a resource the caller
|
||||
// OWNS : GET /files/:id/shares. Non-owner (or unknown resource) → 404
|
||||
// NOT_FOUND (no enumeration, same scoping as the share outbox ops).
|
||||
func SharesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
grants, err := Store.ListShares(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, grants)
|
||||
}
|
||||
|
||||
// LinksList exposes the active public links of a resource the caller OWNS :
|
||||
// GET /files/:id/links. Same owner-only scoping as SharesList.
|
||||
func LinksList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
links, err := Store.ListLinks(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, links)
|
||||
}
|
||||
@@ -455,5 +455,49 @@ 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).
|
||||
type ShareGrant struct {
|
||||
GranteeUserID string
|
||||
Username string
|
||||
Access string
|
||||
Inherit bool
|
||||
ExpiresAt *time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// ListActiveForResource returns the active (non-revoked) grants on a resource,
|
||||
// oldest first. Expired grants are included (the owner sees what will lapse);
|
||||
// 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),
|
||||
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
|
||||
WHERE s.resource_id = $1 AND s.revoked_at IS NULL
|
||||
ORDER BY s.created_at ASC`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var grants []ShareGrant
|
||||
for rows.Next() {
|
||||
var (
|
||||
g ShareGrant
|
||||
expiresAt sql.NullTime
|
||||
)
|
||||
if err := rows.Scan(&g.GranteeUserID, &g.Username, &g.Access, &g.Inherit, &expiresAt, &g.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expiresAt.Valid {
|
||||
g.ExpiresAt = &expiresAt.Time
|
||||
}
|
||||
grants = append(grants, g)
|
||||
}
|
||||
return grants, rows.Err()
|
||||
}
|
||||
|
||||
// Errors specific to shares.
|
||||
var ErrGranteeNotFound = errors.New("grantee user not found")
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// ShareGrantDTO serializes one active grant for GET /files/:id/shares.
|
||||
// Milliseconds epoch (null = never), mirroring the permissions snapshot
|
||||
// (docs/api-v1.md §6.2) for cheap client-side expiry checks.
|
||||
type ShareGrantDTO struct {
|
||||
GranteeUserID string `json:"granteeUserId"`
|
||||
Username string `json:"username"`
|
||||
Access string `json:"access"`
|
||||
Inherit bool `json:"inherit"`
|
||||
ExpiresAt *int64 `json:"expiresAt"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// LinkDTO serializes one active public link for GET /files/:id/links —
|
||||
// same shape as the public GET /shares/links/:token resolution.
|
||||
type LinkDTO struct {
|
||||
Token string `json:"token"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
ResourceType string `json:"resourceType"`
|
||||
Name string `json:"name"`
|
||||
Access string `json:"access"`
|
||||
ExpiresAt *string `json:"expiresAt"`
|
||||
}
|
||||
|
||||
// ListShares returns the active grants on a resource the user OWNS.
|
||||
// Non-owner (or unknown resource) → repository.ErrNotFound (no enumeration,
|
||||
// same scoping as the share outbox ops).
|
||||
func (s *Resources) ListShares(userID, resourceID string) ([]ShareGrantDTO, error) {
|
||||
ownerID, err := s.Repo.GetResourceOwner(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ownerID != userID {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
grants, err := s.Repository.Shares.ListActiveForResource(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]ShareGrantDTO, 0, len(grants))
|
||||
for _, g := range grants {
|
||||
var expires *int64
|
||||
if g.ExpiresAt != nil {
|
||||
ms := g.ExpiresAt.UnixMilli()
|
||||
expires = &ms
|
||||
}
|
||||
out = append(out, ShareGrantDTO{
|
||||
GranteeUserID: g.GranteeUserID,
|
||||
Username: g.Username,
|
||||
Access: g.Access,
|
||||
Inherit: g.Inherit,
|
||||
ExpiresAt: expires,
|
||||
UpdatedAt: g.UpdatedAt.UnixMilli(),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListLinks returns the active public links of a resource the user OWNS.
|
||||
// Same owner-only scoping as ListShares.
|
||||
func (s *Resources) ListLinks(userID, resourceID string) ([]LinkDTO, error) {
|
||||
ownerID, err := s.Repo.GetResourceOwner(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ownerID != userID {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
links, err := s.Repository.Shares.ListActiveLinksForResource(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]LinkDTO, 0, len(links))
|
||||
for _, l := range links {
|
||||
var expires *string
|
||||
if l.ExpiresAt != nil {
|
||||
// Même forme que ShareLinkGet (time.Time marshaled en RFC3339).
|
||||
str := l.ExpiresAt.UTC().Format(time.RFC3339)
|
||||
expires = &str
|
||||
}
|
||||
out = append(out, LinkDTO{
|
||||
Token: l.Token,
|
||||
ResourceID: l.ResourceID,
|
||||
ResourceType: l.ResourceType,
|
||||
Name: l.Name,
|
||||
Access: l.Access,
|
||||
ExpiresAt: expires,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user