Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] Additional role for sharing

Neeraj Gupta3bf4f8047290
diff --git a/server/ente/access.go b/server/ente/access.go
index b7c11023d0..4133befeb9 100644
--- a/server/ente/access.go
+++ b/server/ente/access.go
@@ -6,6 +6,7 @@ const (
VIEWER CollectionParticipantRole = "VIEWER"
OWNER CollectionParticipantRole = "OWNER"
COLLABORATOR CollectionParticipantRole = "COLLABORATOR"
+ ADMIN CollectionParticipantRole = "ADMIN"
UNKNOWN CollectionParticipantRole = "UNKNOWN"
)
@@ -13,7 +14,7 @@ func (c *CollectionParticipantRole) CanAdd() bool {
if c == nil {
return false
}
- return *c == OWNER || *c == COLLABORATOR
+ return *c == OWNER || *c == COLLABORATOR || *c == ADMIN
}
// CanRemoveAny indicates if the role allows user to remove files added by others too
@@ -32,6 +33,8 @@ func ConvertStringToCollectionParticipantRole(value string) CollectionParticipan
return OWNER
case "COLLABORATOR":
return COLLABORATOR
+ case "ADMIN":
+ return ADMIN
default:
return UNKNOWN
}
diff --git a/server/migrations/109_add_admin_role_enum.down.sql b/server/migrations/109_add_admin_role_enum.down.sql
new file mode 100644
index 0000000000..43fafec030
--- /dev/null
+++ b/server/migrations/109_add_admin_role_enum.down.sql
@@ -0,0 +1,15 @@
+-- Remove ADMIN role from collection share role enum by recreating the type.
+-- Any existing ADMIN entries are downgraded to COLLABORATOR before the type change.
+UPDATE collection_shares
+SET role_type = 'COLLABORATOR'
+WHERE role_type = 'ADMIN';
+
+ALTER TABLE collection_shares ALTER COLUMN role_type DROP DEFAULT;
+ALTER TABLE collection_shares ALTER COLUMN role_type TYPE TEXT;
+
+DROP TYPE IF EXISTS role_enum;
+CREATE TYPE role_enum AS ENUM ('VIEWER', 'COLLABORATOR', 'OWNER');
+
+ALTER TABLE collection_shares
+ ALTER COLUMN role_type TYPE role_enum USING role_type::role_enum,
+ ALTER COLUMN role_type SET DEFAULT 'VIEWER';
diff --git a/server/migrations/109_add_admin_role_enum.up.sql b/server/migrations/109_add_admin_role_enum.up.sql
new file mode 100644
index 0000000000..8e99cf79c8
--- /dev/null
+++ b/server/migrations/109_add_admin_role_enum.up.sql
@@ -0,0 +1,2 @@
+-- Add ADMIN role to collection share role enum
+ALTER TYPE role_enum ADD VALUE IF NOT EXISTS 'ADMIN';
diff --git a/server/pkg/controller/collections/share.go b/server/pkg/controller/collections/share.go
index a01d7ce3aa..c64fe9d0c0 100644
--- a/server/pkg/controller/collections/share.go
+++ b/server/pkg/controller/collections/share.go
@@ -2,6 +2,8 @@ package collections
import (
"context"
+ "database/sql"
+ "errors"
"fmt"
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/controller/access"
@@ -40,10 +42,20 @@ func (c *CollectionController) Share(ctx *gin.Context, req ente.AlterShareReques
if !collection.AllowSharing() {
return nil, stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("sharing %s is not allowed", collection.Type))
}
+ shareActorID := collection.Owner.ID
if fromUserID != collection.Owner.ID {
- return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ shareeRole, err := c.CollectionRepo.GetCollectionShareeRole(cID, fromUserID)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ }
+ return nil, stacktrace.Propagate(err, "")
+ }
+ if shareeRole == nil || *shareeRole != ente.ADMIN {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ }
}
- err = c.CollectionRepo.Share(cID, fromUserID, toUserID, encryptedKey, role, time.Microseconds())
+ err = c.CollectionRepo.Share(cID, shareActorID, toUserID, encryptedKey, role, time.Microseconds())
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
@@ -110,8 +122,22 @@ func (c *CollectionController) UnShare(ctx *gin.Context, cID int64, fromUserID i
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
- isLeavingCollection := toUserID == fromUserID
- if fromUserID != collection.Owner.ID || isLeavingCollection {
+ if toUserID == fromUserID {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ }
+ if fromUserID != collection.Owner.ID {
+ shareeRole, err := c.CollectionRepo.GetCollectionShareeRole(cID, fromUserID)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ }
+ return nil, stacktrace.Propagate(err, "")
+ }
+ if shareeRole == nil || *shareeRole != ente.ADMIN {
+ return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
+ }
+ }
+ if toUserID == collection.Owner.ID {
return nil, stacktrace.Propagate(ente.ErrPermissionDenied, "")
}
err = c.CollectionRepo.UnShare(cID, toUserID)
diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.go
index dc403e8bf3..437f3981cf 100644
--- a/server/pkg/repo/collection.go
+++ b/server/pkg/repo/collection.go
@@ -387,7 +387,7 @@ func (repo *CollectionRepository) Share(
if err != nil {
return stacktrace.Propagate(err, "")
}
- if role != ente.VIEWER && role != ente.COLLABORATOR {
+ if role != ente.VIEWER && role != ente.COLLABORATOR && role != ente.ADMIN {
err = fmt.Errorf("invalid role %s", string(role))
return stacktrace.Propagate(err, "")
}