|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"gofaster/internal/auth/model"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserRepository interface {
|
|
|
|
Create(ctx context.Context, user *model.User) error
|
|
|
|
GetByID(ctx context.Context, id uint) (*model.User, error)
|
|
|
|
GetByUsername(ctx context.Context, username string) (*model.User, error)
|
|
|
|
GetByEmail(ctx context.Context, email string) (*model.User, error)
|
|
|
|
Update(ctx context.Context, user *model.User) error
|
|
|
|
Delete(ctx context.Context, id uint) error
|
|
|
|
List(ctx context.Context, offset, limit int) ([]*model.User, int64, error)
|
|
|
|
|
|
|
|
// 登录相关方法
|
|
|
|
IncrementPasswordError(ctx context.Context, userID uint) error
|
|
|
|
ResetPasswordError(ctx context.Context, userID uint) error
|
|
|
|
UpdateLastLogin(ctx context.Context, userID uint, ip string) error
|
|
|
|
GetUserWithRoles(ctx context.Context, userID uint) (*model.User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type userRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserRepository(db *gorm.DB) UserRepository {
|
|
|
|
return &userRepository{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Create(ctx context.Context, user *model.User) error {
|
|
|
|
return r.db.WithContext(ctx).Create(user).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) GetByID(ctx context.Context, id uint) (*model.User, error) {
|
|
|
|
var user model.User
|
|
|
|
err := r.db.WithContext(ctx).First(&user, id).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) GetByUsername(ctx context.Context, username string) (*model.User, error) {
|
|
|
|
var user model.User
|
|
|
|
err := r.db.WithContext(ctx).Where("username = ?", username).First(&user).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*model.User, error) {
|
|
|
|
var user model.User
|
|
|
|
err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Update(ctx context.Context, user *model.User) error {
|
|
|
|
return r.db.WithContext(ctx).Save(user).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Delete(ctx context.Context, id uint) error {
|
|
|
|
return r.db.WithContext(ctx).Delete(&model.User{}, id).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) List(ctx context.Context, offset, limit int) ([]*model.User, int64, error) {
|
|
|
|
var users []*model.User
|
|
|
|
var total int64
|
|
|
|
|
|
|
|
err := r.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&users).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementPasswordError 增加密码错误次数
|
|
|
|
func (r *userRepository) IncrementPasswordError(ctx context.Context, userID uint) error {
|
|
|
|
return r.db.WithContext(ctx).Model(&model.User{}).
|
|
|
|
Where("id = ?", userID).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"password_error_count": gorm.Expr("password_error_count + 1"),
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetPasswordError 重置密码错误次数
|
|
|
|
func (r *userRepository) ResetPasswordError(ctx context.Context, userID uint) error {
|
|
|
|
return r.db.WithContext(ctx).Model(&model.User{}).
|
|
|
|
Where("id = ?", userID).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"password_error_count": 0,
|
|
|
|
"status": 1,
|
|
|
|
"locked_at": nil,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLastLogin 更新最后登录信息
|
|
|
|
func (r *userRepository) UpdateLastLogin(ctx context.Context, userID uint, ip string) error {
|
|
|
|
return r.db.WithContext(ctx).Model(&model.User{}).
|
|
|
|
Where("id = ?", userID).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"last_login_at": gorm.Expr("NOW()"),
|
|
|
|
"last_login_ip": ip,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserWithRoles 获取用户及其角色信息
|
|
|
|
func (r *userRepository) GetUserWithRoles(ctx context.Context, userID uint) (*model.User, error) {
|
|
|
|
var user model.User
|
|
|
|
err := r.db.WithContext(ctx).
|
|
|
|
Preload("Roles").
|
|
|
|
First(&user, userID).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|