User identity devkit

This commit is contained in:
LittleSheep 2024-09-11 22:42:45 +08:00
parent 68c6a55654
commit 395f97f3b5
3 changed files with 102 additions and 0 deletions

View File

@ -51,6 +51,20 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
return c.Next() return c.Next()
} }
func LinkAccountMiddleware[T any](model any, 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(model, val); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
c.Locals("user", adaptor(account))
}
}
return c.Next()
}
}
func (v *HyperConn) EnsureAuthenticated(c *fiber.Ctx) error { func (v *HyperConn) EnsureAuthenticated(c *fiber.Ctx) error {
if _, ok := c.Locals("p_user").(*proto.UserInfo); !ok { if _, ok := c.Locals("p_user").(*proto.UserInfo); !ok {
return fiber.NewError(fiber.StatusUnauthorized) return fiber.NewError(fiber.StatusUnauthorized)

75
pkg/hyper/identity.go Normal file
View File

@ -0,0 +1,75 @@
package hyper
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/database"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"github.com/samber/lo"
"gorm.io/gorm"
"reflect"
)
type BaseUser struct {
BaseModel
Name string `json:"name"`
Nick string `json:"nick"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
Description string `json:"description"`
EmailAddress string `json:"email_address"`
AffiliatedTo *uint `json:"affiliated_to"`
AutomatedBy *uint `json:"automated_by"`
}
func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
var account BaseUser
if userinfo == nil {
return account, fmt.Errorf("remote userinfo was not found")
}
if err := database.C.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
account = BaseUser{
BaseModel: BaseModel{
ID: uint(userinfo.GetId()),
},
Name: userinfo.Name,
Nick: userinfo.Nick,
Avatar: userinfo.Avatar,
Banner: userinfo.Banner,
Description: userinfo.GetDescription(),
EmailAddress: userinfo.Email,
}
if userinfo.AffiliatedTo != nil {
account.AffiliatedTo = lo.ToPtr(uint(*userinfo.AffiliatedTo))
}
if userinfo.AutomatedBy != nil {
account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy))
}
return account, database.C.Model(model).Save(&account).Error
}
return account, err
}
prev := account
account.Name = userinfo.Name
account.Nick = userinfo.Nick
account.Avatar = userinfo.Avatar
account.Banner = userinfo.Banner
account.Description = userinfo.GetDescription()
account.EmailAddress = userinfo.Email
if userinfo.AffiliatedTo != nil {
account.AffiliatedTo = lo.ToPtr(uint(*userinfo.AffiliatedTo))
}
if userinfo.AutomatedBy != nil {
account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy))
}
var err error
if !reflect.DeepEqual(prev, account) {
err = database.C.Model(model).Save(&account).Error
}
return account, err
}

13
pkg/hyper/models.go Normal file
View File

@ -0,0 +1,13 @@
package hyper
import (
"gorm.io/gorm"
"time"
)
type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}