This commit is contained in:
eson 2023-08-29 18:06:39 +08:00
parent 7a2b66f5ab
commit 15692d6075
10 changed files with 158 additions and 7 deletions

View File

@ -148,8 +148,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
user = &FsUser{}
var err error
userTx := tx.Model(user)
err = userTx.Where("email = ?", token.Email).Take(user).Error
err = tx.Model(user).Where("email = ?", token.Email).Take(user).Error
if err == gorm.ErrRecordNotFound {
FirstName := token.Extend["first_name"].(string)
@ -234,3 +233,47 @@ func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, use
}).Error
return err
}
func (u *FsUserModel) DebugAuthDelete(ctx context.Context, email string) (err error) {
err = u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
user := &FsUser{}
txUser := tx.Model(user)
err = txUser.Where("email = ?", email).Take(user).Error
if err == nil {
err = txUser.Where("email = ?", email).Delete(user).Error
if err != nil {
return err
}
txRes := tx.Model(&FsResource{})
txUserMaterial := tx.Model(&FsUserMaterial{})
txUserInfo := tx.Model(&FsUserInfo{})
// 继承guest_id的资源表
err = txRes.
Where("user_id = ?", user.Id).Delete(&FsResource{}).Error
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
err = txUserMaterial.
Where("user_id = ?", user.Id).
Delete(&FsUserMaterial{}).Error
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
err = txUserInfo.
Where("user_id = ?", user.Id).
Delete(&FsResource{}).Error
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
}
return nil
})
return err
}

View File

@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/auth/internal/logic"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
)
func DebugAuthDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RequestAuthDelete
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewDebugAuthDeleteLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.DebugAuthDelete(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -52,6 +52,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/auth/reset/password",
Handler: UserResetPasswordHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/debug/auth/delete",
Handler: DebugAuthDeleteHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,48 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DebugAuthDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDebugAuthDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DebugAuthDeleteLogic {
return &DebugAuthDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *DebugAuthDeleteLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *DebugAuthDeleteLogic) DebugAuthDelete(req *types.RequestAuthDelete, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
err := l.svcCtx.AllModels.FsUser.DebugAuthDelete(l.ctx, req.Email)
if err != nil {
return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
}
return resp.SetStatus(basic.CodeOK)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *DebugAuthDeleteLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }

View File

@ -105,7 +105,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
}
logx.Info(token.Platform)
switch token.Platform {
case "google":
case string(auth.PLATFORM_GOOGLE):
// 谷歌平台的注册流程
user, err := l.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token)
if err != nil {
@ -119,8 +119,8 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
}
logx.Info("success", token.TraceId)
case "facebook":
case "fusen":
case string(auth.PLATFORM_FACEBOOK):
case string(auth.PLATFORM_FUSEN):
// log.Println("aaaa", token)
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
if err != nil && err != gorm.ErrRecordNotFound {

View File

@ -93,7 +93,7 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
l.registerInfo = &auth.RegisterToken{
Password: base64.RawURLEncoding.EncodeToString(nonce),
Platform: "google",
Platform: string(auth.PLATFORM_GOOGLE),
OperateType: auth.OpTypeRegister,
TraceId: uuid.NewString(),
CreateAt: time.Now().UTC(),

View File

@ -47,7 +47,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.RequestUserRegister, userinf
Wid: req.Wid,
Email: req.Email,
Password: req.Password,
Platform: "fusen",
Platform: string(auth.PLATFORM_FUSEN),
TraceId: uuid.NewString(),
CreateAt: time.Now(),
Extend: map[string]interface{}{

View File

@ -5,6 +5,10 @@ import (
"fusenapi/utils/basic"
)
type RequestAuthDelete struct {
Email string `json:"email"`
}
type RequestUserLogin struct {
Email string `json:"email"`
Password string `json:"password"`

View File

@ -33,10 +33,18 @@ service auth {
@handler UserResetPasswordHandler
post /api/auth/reset/password(RequestUserResetPassword) returns (response);
@handler DebugAuthDeleteHandler
post /api/debug/auth/delete(RequestAuthDelete) returns (response);
}
type (
// RequestAuthDelete 用于debug
RequestAuthDelete {
Email string `json:"email"`
}
// UserAddAddressHandler 用户登录请求结构
RequestUserLogin {
Email string `json:"email"`

View File

@ -17,6 +17,14 @@ func init() {
gob.Register(map[string]interface{}{})
}
type RegisterPlatform string
const (
PLATFORM_GOOGLE = "google"
PLATFORM_FUSEN = "fusen"
PLATFORM_FACEBOOK = "facebook"
)
type RegisterToken struct {
OperateType // 操作的类型, 验证的token 必须要继承这个
GuestId int64 // guest_id 需要继承