From 4a48b980f0b3eb5abbf936a0f497cdad3007138f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 14 Sep 2024 21:35:39 +0800 Subject: [PATCH] :bug: Fix insert account into wrong table --- pkg/hyper/auth_adaptor.go | 13 ++++++++----- pkg/hyper/identity.go | 14 +++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/hyper/auth_adaptor.go b/pkg/hyper/auth_adaptor.go index a7e4473..7fae969 100644 --- a/pkg/hyper/auth_adaptor.go +++ b/pkg/hyper/auth_adaptor.go @@ -1,17 +1,20 @@ package hyper import ( - "gorm.io/gorm" "strings" "time" + "gorm.io/gorm" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" ) -const CookieAtk = "__hydrogen_atk" -const CookieRtk = "__hydrogen_rtk" +const ( + CookieAtk = "__hydrogen_atk" + CookieRtk = "__hydrogen_rtk" +) func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error { var atk string @@ -52,10 +55,10 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error { return c.Next() } -func LinkAccountMiddleware[T any](tx *gorm.DB, model any, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error { +func LinkAccountMiddleware[T any](tx *gorm.DB, table string, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error { if val, ok := c.Locals("p_user").(*proto.UserInfo); ok { - if account, err := LinkAccount(tx, model, val); err != nil { + if account, err := LinkAccount(tx, table, val); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { c.Locals("user", adaptor(account)) diff --git a/pkg/hyper/identity.go b/pkg/hyper/identity.go index a6b4334..e35d476 100644 --- a/pkg/hyper/identity.go +++ b/pkg/hyper/identity.go @@ -3,10 +3,11 @@ package hyper import ( "errors" "fmt" + "reflect" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/samber/lo" "gorm.io/gorm" - "reflect" ) type BaseUser struct { @@ -24,12 +25,15 @@ type BaseUser struct { // LinkAccount will help you build a BaseUser model from proto.UserInfo // And also will help you to update the info in your database, so that this function requires a database context -func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, error) { +func LinkAccount(tx *gorm.DB, table string, userinfo *proto.UserInfo) (BaseUser, error) { var account BaseUser if userinfo == nil { return account, fmt.Errorf("remote userinfo was not found") } - if err := tx.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil { + if err := tx. + Where("id = ?", userinfo.GetId()). + Table(table). + First(&account).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { account = BaseUser{ BaseModel: BaseModel{ @@ -48,7 +52,7 @@ func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, er if userinfo.AutomatedBy != nil { account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy)) } - return account, tx.Model(model).Save(&account).Error + return account, tx.Table(table).Save(&account).Error } return account, err } @@ -69,7 +73,7 @@ func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, er var err error if !reflect.DeepEqual(prev, account) { - err = tx.Model(model).Save(&account).Error + err = tx.Table(table).Save(&account).Error } return account, err