543e9709be
Security - Reject URL :id when it does not match the caller's active tenant on every /tenants/:id/members/* and /tenants/:id/leave endpoint. Without this an Owner of tenant A could drive operations on tenant B simply by changing the URL, because RequireRole resolves the caller's role against the active tenant from context, not the URL param. The cross-tenant superuser carve-out mirrors middleware/rbac.go (CanAccessAllTenants + EnableCrossTenantAccess required). - AddMember no longer records the synthetic "system-<tenantID>" caller (X-API-Key auth path) as invited_by, matching the same filter KB.CreatorID got in PR 2. - AuthHandler now logs a loud one-shot error when constructed with a nil/incomplete config so a wiring bug that silently disables the invite_only gate is visible at boot. Race fixes - New repo helpers DemoteOwnerAtomically / RemoveOwnerAtomically wrap the count-other-owners + write step in a transaction that holds SELECT … FOR UPDATE locks on every other active Owner row. The service routes Owner demotion / Owner removal through them, closing the TOCTOU window where two concurrent demotes of two different Owners could each observe count=2 and both commit, leaving the tenant ownerless. Performance - ListMembers hydration now batches user lookups via a new GetUsersByIDs interface method (one SELECT … WHERE id IN (…) instead of N round-trips). Failure degrades to "no email/username" rather than dropping rows. Frontend - canManage also fires for cross-tenant superusers (authStore.canAccessAllTenants), mirroring the backend carve-out so the UI controls match what the server actually allows. Tests - Cross-tenant URL guard regression cases for List / Add / Update / Remove / Leave. - Superuser bypass tests (flag on -> service reached, flag off -> 403). - Batched lookup test ensures GetUsersByIDs is called exactly once and GetUserByID is never used as a fallback. - Synthetic caller invited_by filter regression test. - Service-layer atomic last-Owner regression tests for both UpdateRole and RemoveMember.
207 lines
6.3 KiB
Go
207 lines
6.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
ErrUserNotFound = errors.New("user not found")
|
|
ErrUserAlreadyExists = errors.New("user already exists")
|
|
ErrTokenNotFound = errors.New("token not found")
|
|
)
|
|
|
|
// userRepository implements user repository interface
|
|
type userRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserRepository creates a new user repository
|
|
func NewUserRepository(db *gorm.DB) interfaces.UserRepository {
|
|
return &userRepository{db: db}
|
|
}
|
|
|
|
// CreateUser creates a user
|
|
func (r *userRepository) CreateUser(ctx context.Context, user *types.User) error {
|
|
return r.db.WithContext(ctx).Create(user).Error
|
|
}
|
|
|
|
// GetUserByID gets a user by ID
|
|
func (r *userRepository) GetUserByID(ctx context.Context, id string) (*types.User, error) {
|
|
var user types.User
|
|
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// GetUsersByIDs batch-fetches users by id with a single SELECT … WHERE id IN (…)
|
|
// and projects the result into a map keyed by user id. Returns an empty
|
|
// map for an empty input slice. Missing ids are silently absent from
|
|
// the result (consistent with the interface contract used by tenant
|
|
// member hydration).
|
|
func (r *userRepository) GetUsersByIDs(ctx context.Context, ids []string) (map[string]*types.User, error) {
|
|
out := make(map[string]*types.User, len(ids))
|
|
if len(ids) == 0 {
|
|
return out, nil
|
|
}
|
|
var users []*types.User
|
|
if err := r.db.WithContext(ctx).Where("id IN ?", ids).Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
for _, u := range users {
|
|
out[u.ID] = u
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// GetUserByEmail gets a user by email
|
|
func (r *userRepository) GetUserByEmail(ctx context.Context, email string) (*types.User, error) {
|
|
var user types.User
|
|
if err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// GetUserByUsername gets a user by username
|
|
func (r *userRepository) GetUserByUsername(ctx context.Context, username string) (*types.User, error) {
|
|
var user types.User
|
|
if err := r.db.WithContext(ctx).Where("username = ?", username).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
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
|
|
}
|
|
|
|
// DeleteUser deletes a user
|
|
func (r *userRepository) DeleteUser(ctx context.Context, id string) error {
|
|
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&types.User{}).Error
|
|
}
|
|
|
|
// ListUsers lists users with pagination
|
|
func (r *userRepository) ListUsers(ctx context.Context, offset, limit int) ([]*types.User, error) {
|
|
var users []*types.User
|
|
query := r.db.WithContext(ctx).Order("created_at DESC")
|
|
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
|
|
if err := query.Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
// SearchUsers searches users by username or email
|
|
func (r *userRepository) SearchUsers(ctx context.Context, query string, limit int) ([]*types.User, error) {
|
|
var users []*types.User
|
|
searchPattern := "%" + query + "%"
|
|
|
|
dbQuery := r.db.WithContext(ctx).
|
|
Where("username ILIKE ? OR email ILIKE ?", searchPattern, searchPattern).
|
|
Where("is_active = ?", true).
|
|
Order("username ASC")
|
|
|
|
if limit > 0 {
|
|
dbQuery = dbQuery.Limit(limit)
|
|
} else {
|
|
dbQuery = dbQuery.Limit(20) // default limit
|
|
}
|
|
|
|
if err := dbQuery.Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
// authTokenRepository implements auth token repository interface
|
|
type authTokenRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAuthTokenRepository creates a new auth token repository
|
|
func NewAuthTokenRepository(db *gorm.DB) interfaces.AuthTokenRepository {
|
|
return &authTokenRepository{db: db}
|
|
}
|
|
|
|
// CreateToken creates an auth token
|
|
func (r *authTokenRepository) CreateToken(ctx context.Context, token *types.AuthToken) error {
|
|
return r.db.WithContext(ctx).Create(token).Error
|
|
}
|
|
|
|
// GetTokenByValue gets a token by its value
|
|
func (r *authTokenRepository) GetTokenByValue(ctx context.Context, tokenValue string) (*types.AuthToken, error) {
|
|
var token types.AuthToken
|
|
if err := r.db.WithContext(ctx).Where("token = ?", tokenValue).First(&token).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrTokenNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &token, nil
|
|
}
|
|
|
|
// GetTokensByUserID gets all tokens for a user
|
|
func (r *authTokenRepository) GetTokensByUserID(ctx context.Context, userID string) ([]*types.AuthToken, error) {
|
|
var tokens []*types.AuthToken
|
|
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&tokens).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return tokens, nil
|
|
}
|
|
|
|
// UpdateToken updates a token
|
|
func (r *authTokenRepository) UpdateToken(ctx context.Context, token *types.AuthToken) error {
|
|
return r.db.WithContext(ctx).Save(token).Error
|
|
}
|
|
|
|
// DeleteToken deletes a token
|
|
func (r *authTokenRepository) DeleteToken(ctx context.Context, id string) error {
|
|
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&types.AuthToken{}).Error
|
|
}
|
|
|
|
// DeleteExpiredTokens deletes all expired tokens
|
|
func (r *authTokenRepository) DeleteExpiredTokens(ctx context.Context) error {
|
|
return r.db.WithContext(ctx).Where("expires_at < NOW()").Delete(&types.AuthToken{}).Error
|
|
}
|
|
|
|
// RevokeTokensByUserID revokes all tokens for a user
|
|
func (r *authTokenRepository) RevokeTokensByUserID(ctx context.Context, userID string) error {
|
|
return r.db.WithContext(ctx).Model(&types.AuthToken{}).Where("user_id = ?", userID).Update("is_revoked", true).Error
|
|
}
|