🐛 Fix insert account into wrong table
This commit is contained in:
parent
d734d617bf
commit
4a48b980f0
@ -1,17 +1,20 @@
|
|||||||
package hyper
|
package hyper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CookieAtk = "__hydrogen_atk"
|
const (
|
||||||
const CookieRtk = "__hydrogen_rtk"
|
CookieAtk = "__hydrogen_atk"
|
||||||
|
CookieRtk = "__hydrogen_rtk"
|
||||||
|
)
|
||||||
|
|
||||||
func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
|
func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
|
||||||
var atk string
|
var atk string
|
||||||
@ -52,10 +55,10 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
|
|||||||
return c.Next()
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
if val, ok := c.Locals("p_user").(*proto.UserInfo); ok {
|
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())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.Locals("user", adaptor(account))
|
c.Locals("user", adaptor(account))
|
||||||
|
@ -3,10 +3,11 @@ package hyper
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseUser struct {
|
type BaseUser struct {
|
||||||
@ -24,12 +25,15 @@ type BaseUser struct {
|
|||||||
|
|
||||||
// LinkAccount will help you build a BaseUser model from proto.UserInfo
|
// 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
|
// 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
|
var account BaseUser
|
||||||
if userinfo == nil {
|
if userinfo == nil {
|
||||||
return account, fmt.Errorf("remote userinfo was not found")
|
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) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
account = BaseUser{
|
account = BaseUser{
|
||||||
BaseModel: BaseModel{
|
BaseModel: BaseModel{
|
||||||
@ -48,7 +52,7 @@ func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, er
|
|||||||
if userinfo.AutomatedBy != nil {
|
if userinfo.AutomatedBy != nil {
|
||||||
account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy))
|
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
|
return account, err
|
||||||
}
|
}
|
||||||
@ -69,7 +73,7 @@ func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, er
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
if !reflect.DeepEqual(prev, account) {
|
if !reflect.DeepEqual(prev, account) {
|
||||||
err = tx.Model(model).Save(&account).Error
|
err = tx.Table(table).Save(&account).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
return account, err
|
return account, err
|
||||||
|
Loading…
Reference in New Issue
Block a user