feat(server): add project sharing access control

This commit is contained in:
2026-07-10 14:46:18 +08:00
parent 60130b3d9b
commit 9d47cb9bbd
41 changed files with 3069 additions and 17 deletions
+13
View File
@@ -7,6 +7,7 @@ import (
"img_infinite_canvas/internal/domain/design"
authmodule "img_infinite_canvas/internal/modules/auth"
sharingmodule "img_infinite_canvas/internal/modules/sharing"
)
type CodeError struct {
@@ -47,6 +48,18 @@ func Handle(ctx context.Context, err error) (int, any) {
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.human_verification_failed", "code": http.StatusBadRequest}
case errors.Is(err, authmodule.ErrProviderNotReady):
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.provider_not_ready", "code": http.StatusBadRequest}
case errors.Is(err, sharingmodule.ErrLoginRequired):
return http.StatusUnauthorized, map[string]any{"error": err.Error(), "errorCode": "share.login_required", "code": http.StatusUnauthorized}
case errors.Is(err, sharingmodule.ErrSelfInvite):
return http.StatusConflict, map[string]any{"error": err.Error(), "errorCode": "share.self_invite", "code": http.StatusConflict}
case errors.Is(err, sharingmodule.ErrMemberExists):
return http.StatusConflict, map[string]any{"error": err.Error(), "errorCode": "share.member_exists", "code": http.StatusConflict}
case errors.Is(err, sharingmodule.ErrForbidden):
return http.StatusForbidden, map[string]any{"error": err.Error(), "errorCode": "share.forbidden", "code": http.StatusForbidden}
case errors.Is(err, sharingmodule.ErrNotFound):
return http.StatusNotFound, map[string]any{"error": sharingmodule.ErrNotFound.Error(), "errorCode": "share.not_found", "code": http.StatusNotFound}
case errors.Is(err, sharingmodule.ErrInvalidInput):
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "share.invalid_input", "code": http.StatusBadRequest}
case errors.Is(err, context.Canceled):
return http.StatusRequestTimeout, map[string]any{"error": "request canceled", "code": http.StatusRequestTimeout}
default:
+36
View File
@@ -0,0 +1,36 @@
package errorx
import (
"context"
"net/http"
"testing"
sharingmodule "img_infinite_canvas/internal/modules/sharing"
)
func TestHandleShareInviteConflicts(t *testing.T) {
tests := []struct {
name string
err error
errorCode string
}{
{name: "self invite", err: sharingmodule.ErrSelfInvite, errorCode: "share.self_invite"},
{name: "member exists", err: sharingmodule.ErrMemberExists, errorCode: "share.member_exists"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status, body := Handle(context.Background(), tt.err)
if status != http.StatusConflict {
t.Fatalf("expected status %d, got %d", http.StatusConflict, status)
}
payload, ok := body.(map[string]any)
if !ok {
t.Fatalf("expected map response, got %T", body)
}
if payload["errorCode"] != tt.errorCode {
t.Fatalf("expected error code %q, got %v", tt.errorCode, payload["errorCode"])
}
})
}
}