All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m31s
新增绑定邮箱和密码的接口,用于设备用户绑定已有邮箱账户 新增绑定邀请码接口,绑定成功后会为双方赠送订阅天数 修复API定义中的格式问题,统一缩进和对齐 移除不再使用的WebSocket路由
126 lines
4.6 KiB
Go
126 lines
4.6 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/perfect-panel/server/internal/logic/auth"
|
|
"github.com/perfect-panel/server/pkg/constant"
|
|
"github.com/perfect-panel/server/pkg/tool"
|
|
|
|
"github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
)
|
|
|
|
type BindEmailWithPasswordLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// NewBindEmailWithPasswordLogic Bind Email With Password
|
|
func NewBindEmailWithPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindEmailWithPasswordLogic {
|
|
return &BindEmailWithPasswordLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *BindEmailWithPasswordLogic) BindEmailWithPassword(req *types.BindEmailWithPasswordRequest) error {
|
|
// 获取当前设备用户
|
|
currentUser, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
|
if !ok {
|
|
logger.Error("current user is not found in context")
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
|
}
|
|
|
|
// 验证邮箱和密码是否匹配现有用户
|
|
emailUser, err := l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.UserNotExist), "email not registered: %v", req.Email)
|
|
}
|
|
logger.WithContext(l.ctx).Error(err)
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user by email failed: %v", err.Error())
|
|
}
|
|
|
|
// 验证密码
|
|
if !tool.VerifyPassWord(req.Password, emailUser.Password) {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.UserPasswordError), "password incorrect")
|
|
}
|
|
|
|
// 检查当前用户是否已经绑定了邮箱
|
|
currentEmailMethod, err := l.svcCtx.UserModel.FindUserAuthMethodByUserId(l.ctx, "email", currentUser.Id)
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "FindUserAuthMethodByUserId error")
|
|
}
|
|
|
|
// 如果当前用户已经绑定了邮箱,不允许重复绑定
|
|
if currentEmailMethod.Id > 0 {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.UserExist), "current user already has email bound")
|
|
}
|
|
|
|
// 检查该邮箱是否已经被其他用户绑定
|
|
existingEmailMethod, err := l.svcCtx.UserModel.FindUserAuthMethodByOpenID(l.ctx, "email", req.Email)
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "FindUserAuthMethodByOpenID error")
|
|
}
|
|
|
|
// 如果邮箱已经被其他用户绑定,需要进行数据迁移
|
|
if existingEmailMethod.Id > 0 && existingEmailMethod.UserId != currentUser.Id {
|
|
// 调用设备绑定逻辑,这会触发数据迁移
|
|
bindLogic := auth.NewBindDeviceLogic(l.ctx, l.svcCtx)
|
|
|
|
// 获取当前用户的设备标识符
|
|
deviceMethod, err := l.svcCtx.UserModel.FindUserAuthMethodByUserId(l.ctx, "device", currentUser.Id)
|
|
if err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "FindUserAuthMethodByUserId device error")
|
|
}
|
|
|
|
if deviceMethod.Id == 0 {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "current user has no device identifier")
|
|
}
|
|
|
|
// 执行设备重新绑定,这会触发数据迁移
|
|
if err := bindLogic.BindDeviceToUser(deviceMethod.AuthIdentifier, "", "", emailUser.Id); err != nil {
|
|
l.Errorw("failed to bind device to email user",
|
|
logger.Field("current_user_id", currentUser.Id),
|
|
logger.Field("email_user_id", emailUser.Id),
|
|
logger.Field("device_identifier", deviceMethod.AuthIdentifier),
|
|
logger.Field("error", err.Error()),
|
|
)
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "bind device to email user failed")
|
|
}
|
|
|
|
l.Infow("successfully bound device to email user with data migration",
|
|
logger.Field("current_user_id", currentUser.Id),
|
|
logger.Field("email_user_id", emailUser.Id),
|
|
logger.Field("device_identifier", deviceMethod.AuthIdentifier),
|
|
)
|
|
} else {
|
|
// 邮箱未被绑定,直接为当前用户创建邮箱绑定
|
|
emailMethod := &user.AuthMethods{
|
|
UserId: currentUser.Id,
|
|
AuthType: "email",
|
|
AuthIdentifier: req.Email,
|
|
Verified: true, // 通过密码验证,直接设为已验证
|
|
}
|
|
|
|
if err := l.svcCtx.UserModel.InsertUserAuthMethods(l.ctx, emailMethod); err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "InsertUserAuthMethods error")
|
|
}
|
|
|
|
l.Infow("successfully bound email to current user",
|
|
logger.Field("user_id", currentUser.Id),
|
|
logger.Field("email", req.Email),
|
|
)
|
|
}
|
|
|
|
return nil
|
|
} |