From 15692d60758b0baaf86f5a93c2f0f16eaeadf45b Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 29 Aug 2023 18:06:39 +0800 Subject: [PATCH] fix --- model/gmodel/fs_user_logic.go | 47 +++++++++++++++++- .../handler/debugauthdeletehandler.go | 35 ++++++++++++++ server/auth/internal/handler/routes.go | 5 ++ .../internal/logic/debugauthdeletelogic.go | 48 +++++++++++++++++++ .../logic/useremailconfirmationlogic.go | 6 +-- .../internal/logic/usergoogleloginlogic.go | 2 +- .../auth/internal/logic/userregisterlogic.go | 2 +- server/auth/internal/types/types.go | 4 ++ server_api/auth.api | 8 ++++ utils/auth/register.go | 8 ++++ 10 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 server/auth/internal/handler/debugauthdeletehandler.go create mode 100644 server/auth/internal/logic/debugauthdeletelogic.go diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index 257a683d..2248ce88 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -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 +} diff --git a/server/auth/internal/handler/debugauthdeletehandler.go b/server/auth/internal/handler/debugauthdeletehandler.go new file mode 100644 index 00000000..71bb5365 --- /dev/null +++ b/server/auth/internal/handler/debugauthdeletehandler.go @@ -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) + } + } +} diff --git a/server/auth/internal/handler/routes.go b/server/auth/internal/handler/routes.go index 2ed3bfad..121d86c8 100644 --- a/server/auth/internal/handler/routes.go +++ b/server/auth/internal/handler/routes.go @@ -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), + }, }, ) } diff --git a/server/auth/internal/logic/debugauthdeletelogic.go b/server/auth/internal/logic/debugauthdeletelogic.go new file mode 100644 index 00000000..a841c396 --- /dev/null +++ b/server/auth/internal/logic/debugauthdeletelogic.go @@ -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) +// } diff --git a/server/auth/internal/logic/useremailconfirmationlogic.go b/server/auth/internal/logic/useremailconfirmationlogic.go index a9cda652..85b6de3b 100644 --- a/server/auth/internal/logic/useremailconfirmationlogic.go +++ b/server/auth/internal/logic/useremailconfirmationlogic.go @@ -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 { diff --git a/server/auth/internal/logic/usergoogleloginlogic.go b/server/auth/internal/logic/usergoogleloginlogic.go index 2a4372ec..96879180 100644 --- a/server/auth/internal/logic/usergoogleloginlogic.go +++ b/server/auth/internal/logic/usergoogleloginlogic.go @@ -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(), diff --git a/server/auth/internal/logic/userregisterlogic.go b/server/auth/internal/logic/userregisterlogic.go index 3840dffe..513c2ac2 100644 --- a/server/auth/internal/logic/userregisterlogic.go +++ b/server/auth/internal/logic/userregisterlogic.go @@ -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{}{ diff --git a/server/auth/internal/types/types.go b/server/auth/internal/types/types.go index f052b217..e7f6d228 100644 --- a/server/auth/internal/types/types.go +++ b/server/auth/internal/types/types.go @@ -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"` diff --git a/server_api/auth.api b/server_api/auth.api index 4f5384dd..eeddb5e5 100644 --- a/server_api/auth.api +++ b/server_api/auth.api @@ -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"` diff --git a/utils/auth/register.go b/utils/auth/register.go index 45d64019..3aacc85d 100644 --- a/utils/auth/register.go +++ b/utils/auth/register.go @@ -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 需要继承