mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-06-04 13:30:32 +08:00
feat: add GetUserByTenantID functionality to user repository and service
- Implemented GetUserByTenantID method in userRepository to retrieve the first user associated with a given tenant ID. - Added corresponding GetUserByTenantID method in userService to facilitate user retrieval by tenant ID. - Updated UserService and UserRepository interfaces to include the new method, ensuring consistency across the application. - Enhanced auth middleware to store user information in context based on tenant ID, improving user management. These changes enhance user retrieval capabilities by allowing access to user data based on tenant associations, improving overall application functionality.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user