diff --git a/pkg/hyper/auth_adaptor.go b/pkg/hyper/auth_adaptor.go index 51cd497..8322585 100644 --- a/pkg/hyper/auth_adaptor.go +++ b/pkg/hyper/auth_adaptor.go @@ -51,6 +51,20 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error { 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 { if _, ok := c.Locals("p_user").(*proto.UserInfo); !ok { return fiber.NewError(fiber.StatusUnauthorized) diff --git a/pkg/hyper/identity.go b/pkg/hyper/identity.go new file mode 100644 index 0000000..d46a717 --- /dev/null +++ b/pkg/hyper/identity.go @@ -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 +} diff --git a/pkg/hyper/models.go b/pkg/hyper/models.go new file mode 100644 index 0000000..38fca37 --- /dev/null +++ b/pkg/hyper/models.go @@ -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"` +}