Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Reuse authorized collection data

Neeraj Gupta53ff074ba0e6
diff --git a/server/pkg/controller/collections/collection.go b/server/pkg/controller/collections/collection.go
index 5f45fe0c9f..fbfd246c6d 100644
--- a/server/pkg/controller/collections/collection.go
+++ b/server/pkg/controller/collections/collection.go
@@ -89,7 +89,7 @@ func (c *CollectionController) GetCollection(ctx *gin.Context, userID int64, cID
if err != nil {
return ente.Collection{}, stacktrace.Propagate(err, "")
}
- collection, err := c.CollectionRepo.GetWithSharingDetailsForUser(cID, userID)
+ collection, err := c.CollectionRepo.WithSharingDetailsForUser(resp.Collection, userID)
if err != nil {
return ente.Collection{}, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/controller/collections/collection_test.go b/server/pkg/controller/collections/collection_test.go
new file mode 100644
index 0000000000..cce204e419
--- /dev/null
+++ b/server/pkg/controller/collections/collection_test.go
@@ -0,0 +1,54 @@
+package collections
+
+import (
+ "net/http/httptest"
+ "testing"
+
+ "github.com/ente/museum/ente"
+ "github.com/ente/museum/pkg/controller/access"
+ "github.com/gin-gonic/gin"
+)
+
+func TestGetCollectionForViewer(t *testing.T) {
+ db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)
+ collectionID := createShareTestCollection(t, collectionRepo, ownerID)
+ addShareTestShare(t, collectionRepo, collectionID, ownerID, shareeID, ente.VIEWER)
+ if _, err := db.Exec(
+ `INSERT INTO public_collection_tokens (collection_id, access_token, valid_till, device_limit, min_role)
+ VALUES ($1, 'collaborator-link', 0, 0, $2)`,
+ collectionID, ente.COLLABORATOR,
+ ); err != nil {
+ t.Fatal(err)
+ }
+ controller := &CollectionController{
+ AccessCtrl: access.NewAccessController(collectionRepo, nil),
+ CollectionRepo: collectionRepo,
+ }
+ ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
+
+ ownerCollection, err := controller.GetCollection(ctx, ownerID, collectionID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ wantURL := collectionRepo.CollectionLinkRepo.GetAlbumUrl(ente.Photos, "collaborator-link")
+ if len(ownerCollection.PublicURLs) != 1 || ownerCollection.PublicURLs[0].URL != wantURL {
+ t.Fatalf("owner public URLs = %+v, want %q", ownerCollection.PublicURLs, wantURL)
+ }
+
+ collection, err := controller.GetCollection(ctx, shareeID, collectionID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if collection.EncryptedKey != "share-key" {
+ t.Fatalf("encrypted key = %q, want recipient's key", collection.EncryptedKey)
+ }
+ if len(collection.PublicURLs) != 0 {
+ t.Fatalf("viewer received restricted public URLs: %+v", collection.PublicURLs)
+ }
+ if collection.Owner.Email != "[email protected]" {
+ t.Fatalf("owner email = %q", collection.Owner.Email)
+ }
+ if len(collection.Sharees) != 1 || collection.Sharees[0].ID != shareeID {
+ t.Fatalf("unexpected sharees: %+v", collection.Sharees)
+ }
+}
diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.go
index 26ceaf28c1..76bd063754 100644
--- a/server/pkg/repo/collection.go
+++ b/server/pkg/repo/collection.go
@@ -92,12 +92,8 @@ func (repo *CollectionRepository) Get(collectionID int64) (ente.Collection, erro
return c, nil
}
-func (repo *CollectionRepository) GetWithSharingDetailsForUser(collectionID int64, actorUserID int64) (ente.Collection, error) {
- c, err := repo.Get(collectionID)
- if err != nil {
- return c, stacktrace.Propagate(err, "")
- }
- sharees, err := repo.GetSharees(collectionID)
+func (repo *CollectionRepository) WithSharingDetailsForUser(c ente.Collection, actorUserID int64) (ente.Collection, error) {
+ sharees, err := repo.GetSharees(c.ID)
if err != nil {
return ente.Collection{}, stacktrace.Propagate(err, "failed to get sharees info")
}
@@ -122,12 +118,12 @@ func (repo *CollectionRepository) GetWithSharingDetailsForUser(collectionID int6
if actorUserID != c.Owner.ID {
var encryptedKey sql.NullString
err := repo.DB.QueryRow(`SELECT encrypted_key FROM collection_shares WHERE collection_id = $1 AND to_user_id = $2 AND is_deleted = $3`,
- collectionID, actorUserID, false).Scan(&encryptedKey)
+ c.ID, actorUserID, false).Scan(&encryptedKey)
if err != nil {
return ente.Collection{}, stacktrace.Propagate(err, "failed to fetch sharee encrypted key")
}
if !encryptedKey.Valid {
- return ente.Collection{}, stacktrace.Propagate(fmt.Errorf("share key missing for user %d collection %d", actorUserID, collectionID), "")
+ return ente.Collection{}, stacktrace.Propagate(fmt.Errorf("share key missing for user %d collection %d", actorUserID, c.ID), "")
}
c.EncryptedKey = encryptedKey.String
}