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
+44
View File
@@ -25,6 +25,7 @@ import (
authmodule "img_infinite_canvas/internal/modules/auth"
jobmodule "img_infinite_canvas/internal/modules/job"
realtimemodule "img_infinite_canvas/internal/modules/realtime"
sharingmodule "img_infinite_canvas/internal/modules/sharing"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -33,6 +34,7 @@ type ServiceContext struct {
Config config.Config
DesignService *application.DesignService
AuthService *authmodule.Service
ShareService *sharingmodule.Service
RealtimeBus realtimemodule.Bus
jobQueue design.JobQueue
jobWorker backgroundWorker
@@ -49,6 +51,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
repo := newProjectRepository(c)
cacheStore := newCacheStore(c)
authService := newAuthService(c, cacheStore)
shareService := newShareService(c)
repo = withCache(c, repo, cacheStore)
realtimeBus := newRealtimeBus(c)
repo = withRealtime(repo, realtimeBus)
@@ -75,6 +78,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
Config: c,
DesignService: service,
AuthService: authService,
ShareService: shareService,
RealtimeBus: realtimeBus,
jobQueue: queue,
jobWorker: worker,
@@ -150,6 +154,46 @@ func (s *ServiceContext) Close() {
logx.Errorf("close auth service failed: %v", err)
}
}
if s.ShareService != nil {
if err := s.ShareService.Close(); err != nil {
logx.Errorf("close sharing service failed: %v", err)
}
}
}
func newShareService(c config.Config) *sharingmodule.Service {
var store sharingmodule.Store
switch c.Storage.Driver {
case "", "memory":
store = sharingmodule.NewMemoryStore()
case "postgres":
if strings.TrimSpace(c.Storage.DataSource) == "" {
logx.Must(application.ErrMissingDataSource)
}
postgresStore, err := sharingmodule.NewPostgresStore(context.Background(), c.Storage.DataSource)
if err != nil {
logx.Must(err)
}
store = postgresStore
default:
logx.Must(application.ErrUnsupportedStorage)
store = sharingmodule.NewMemoryStore()
}
secret := strings.TrimSpace(c.Sharing.EncryptionSecret)
if envName := strings.TrimSpace(c.Sharing.EncryptionSecretEnv); envName != "" {
if envValue := strings.TrimSpace(os.Getenv(envName)); envValue != "" {
secret = envValue
}
}
if secret == "" {
secret = strings.TrimSpace(c.Auth.TokenSecret)
}
service, err := sharingmodule.NewService(store, secret)
if err != nil {
_ = store.Close()
logx.Must(err)
}
return service
}
func newAuthService(c config.Config, cacheStore cacheinfra.Store) *authmodule.Service {