feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -33,7 +34,11 @@ func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
|
||||
func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := r.db.WithContext(ctx).First(&user, id).Error; err != nil {
|
||||
query := r.db.WithContext(ctx)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
@@ -45,7 +50,11 @@ func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, toke
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
var user model.User
|
||||
if err := r.db.WithContext(ctx).
|
||||
query := r.db.WithContext(ctx)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
Select("id, phone, status, token_version").
|
||||
First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -60,7 +69,11 @@ func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, toke
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateProfile(ctx context.Context, id uint64, nickname string, avatarURL string) (*model.User, error) {
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
query := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Updates(map[string]any{
|
||||
"nickname": nickname,
|
||||
"avatar_url": avatarURL,
|
||||
}).Error; err != nil {
|
||||
@@ -84,11 +97,13 @@ func (r *UserRepository) resolveSiteID(ctx context.Context, siteCode string) uin
|
||||
func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string, trackCode string, siteCode ...string) (*model.User, error) {
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
Nickname: "用户" + phone[len(phone)-4:],
|
||||
RealnameStatus: "unverified",
|
||||
@@ -100,16 +115,25 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
LastLoginAt: &now,
|
||||
}
|
||||
|
||||
err := r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "phone"}},
|
||||
onConflict := clause.OnConflict{
|
||||
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
||||
}).Create(&user).Error
|
||||
}
|
||||
if tenantID > 0 {
|
||||
onConflict.Columns = []clause.Column{{Name: "tenant_id"}, {Name: "phone"}}
|
||||
} else {
|
||||
onConflict.Columns = []clause.Column{{Name: "phone"}}
|
||||
}
|
||||
err := r.db.WithContext(ctx).Clauses(onConflict).Create(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var found model.User
|
||||
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&found).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&found).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &found, nil
|
||||
@@ -117,14 +141,22 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
|
||||
func (r *UserRepository) FindByPhone(ctx context.Context, phone string) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&user).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) SetPassword(ctx context.Context, userID uint64, hash string) error {
|
||||
return r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID).Updates(map[string]any{
|
||||
query := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.Updates(map[string]any{
|
||||
"password_hash": hash,
|
||||
"token_version": gorm.Expr("token_version + 1"),
|
||||
}).Error
|
||||
@@ -132,18 +164,23 @@ func (r *UserRepository) SetPassword(ctx context.Context, userID uint64, hash st
|
||||
|
||||
// RevokeTokens 使当前用户的所有 access/refresh token 立即失效。
|
||||
func (r *UserRepository) RevokeTokens(ctx context.Context, userID uint64) error {
|
||||
return r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID).
|
||||
UpdateColumn("token_version", gorm.Expr("token_version + 1")).Error
|
||||
query := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.UpdateColumn("token_version", gorm.Expr("token_version + 1")).Error
|
||||
}
|
||||
|
||||
func (r *UserRepository) RegisterWithPassword(ctx context.Context, phone string, hash string, trackCode string, siteCode ...string) (*model.User, error) {
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
PasswordHash: hash,
|
||||
Nickname: "用户" + phone[len(phone)-4:],
|
||||
|
||||
Reference in New Issue
Block a user