Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: add GetUserByTenantID functionality to user repository and service

wizardchen4ddc468fea6f
diff --git a/internal/application/repository/user.go b/internal/application/repository/user.go
index 9d1f415d6..d3fa6b6ed 100644
--- a/internal/application/repository/user.go
+++ b/internal/application/repository/user.go
@@ -66,6 +66,18 @@ func (r *userRepository) GetUserByUsername(ctx context.Context, username string)
return &user, nil
}
+// GetUserByTenantID gets the first user (owner) of a tenant
+func (r *userRepository) GetUserByTenantID(ctx context.Context, tenantID uint64) (*types.User, error) {
+ var user types.User
+ if err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Order("created_at ASC").First(&user).Error; err != nil {
+ if errors.Is(err, gorm.ErrRecordNotFound) {
+ return nil, ErrUserNotFound
+ }
+ return nil, err
+ }
+ return &user, nil
+}
+
// UpdateUser updates a user
func (r *userRepository) UpdateUser(ctx context.Context, user *types.User) error {
return r.db.WithContext(ctx).Save(user).Error
diff --git a/internal/application/service/user.go b/internal/application/service/user.go
index 012d264a5..df86157b1 100644
--- a/internal/application/service/user.go
+++ b/internal/application/service/user.go
@@ -213,6 +213,11 @@ func (s *userService) GetUserByUsername(ctx context.Context, username string) (*
return s.userRepo.GetUserByUsername(ctx, username)
}
+// GetUserByTenantID gets the first user (owner) of a tenant
+func (s *userService) GetUserByTenantID(ctx context.Context, tenantID uint64) (*types.User, error) {
+ return s.userRepo.GetUserByTenantID(ctx, tenantID)
+}
+
// UpdateUser updates user information
func (s *userService) UpdateUser(ctx context.Context, user *types.User) error {
user.UpdatedAt = time.Now()
diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go
index a90fdf2c9..9f26de643 100644
--- a/internal/middleware/auth.go
+++ b/internal/middleware/auth.go
@@ -181,15 +181,27 @@ func Auth(
return
}
- // Store tenant ID in context
+ // 存储租户和用户信息到上下文
c.Set(types.TenantIDContextKey.String(), tenantID)
c.Set(types.TenantInfoContextKey.String(), t)
- c.Request = c.Request.WithContext(
- context.WithValue(
- context.WithValue(c.Request.Context(), types.TenantIDContextKey, tenantID),
- types.TenantInfoContextKey, t,
- ),
+
+ ctx := context.WithValue(
+ context.WithValue(c.Request.Context(), types.TenantIDContextKey, tenantID),
+ types.TenantInfoContextKey, t,
)
+
+ // 通过 TenantID 关联查询用户
+ user, err := userService.GetUserByTenantID(c.Request.Context(), tenantID)
+ if err == nil && user != nil {
+ c.Set(types.UserContextKey.String(), user)
+ c.Set(types.UserIDContextKey.String(), user.ID)
+ ctx = context.WithValue(
+ context.WithValue(ctx, types.UserContextKey, user),
+ types.UserIDContextKey, user.ID,
+ )
+ }
+
+ c.Request = c.Request.WithContext(ctx)
c.Next()
return
}
diff --git a/internal/types/interfaces/user.go b/internal/types/interfaces/user.go
index 53d736e6e..b2edb1ca2 100644
--- a/internal/types/interfaces/user.go
+++ b/internal/types/interfaces/user.go
@@ -18,6 +18,8 @@ type UserService interface {
GetUserByEmail(ctx context.Context, email string) (*types.User, error)
// GetUserByUsername gets a user by username
GetUserByUsername(ctx context.Context, username string) (*types.User, error)
+ // GetUserByTenantID gets the first user (owner) of a tenant
+ GetUserByTenantID(ctx context.Context, tenantID uint64) (*types.User, error)
// UpdateUser updates user information
UpdateUser(ctx context.Context, user *types.User) error
// DeleteUser deletes a user
@@ -50,6 +52,8 @@ type UserRepository interface {
GetUserByEmail(ctx context.Context, email string) (*types.User, error)
// GetUserByUsername gets a user by username
GetUserByUsername(ctx context.Context, username string) (*types.User, error)
+ // GetUserByTenantID gets the first user (owner) of a tenant
+ GetUserByTenantID(ctx context.Context, tenantID uint64) (*types.User, error)
// UpdateUser updates a user
UpdateUser(ctx context.Context, user *types.User) error
// DeleteUser deletes a user