From d4aef5277f7fa10517bc6865661c1e2a207de6d9 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 28 Jan 2024 16:17:38 +0800 Subject: [PATCH] :sparkles: Profiles --- .idea/dataSources.xml | 12 ++++++++++ pkg/database/migrator.go | 1 + pkg/models/accounts.go | 2 ++ pkg/models/profiles.go | 14 ++++++++++++ pkg/server/accounts_api.go | 46 +++++++++++++++++++------------------- pkg/server/profiles_api.go | 12 ---------- pkg/server/startup.go | 2 +- pkg/services/accounts.go | 37 ++++++++++++++++++++++++++++-- 8 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 pkg/models/profiles.go delete mode 100644 pkg/server/profiles_api.go diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..8904924 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/hy_passport + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/pkg/database/migrator.go b/pkg/database/migrator.go index b87808a..93a0e47 100644 --- a/pkg/database/migrator.go +++ b/pkg/database/migrator.go @@ -9,6 +9,7 @@ func RunMigration(source *gorm.DB) error { if err := source.AutoMigrate( &models.Account{}, &models.AuthFactor{}, + &models.AccountProfile{}, &models.AccountContact{}, &models.AuthSession{}, &models.AuthChallenge{}, diff --git a/pkg/models/accounts.go b/pkg/models/accounts.go index fc883f1..6060059 100644 --- a/pkg/models/accounts.go +++ b/pkg/models/accounts.go @@ -19,10 +19,12 @@ type Account struct { Name string `json:"name" gorm:"uniqueIndex"` Nick string `json:"nick"` State AccountState `json:"state"` + Profile AccountProfile `json:"profile"` Session []AuthSession `json:"sessions"` Challenges []AuthChallenge `json:"challenges"` Factors []AuthFactor `json:"factors"` Contacts []AccountContact `json:"contacts"` + ConfirmedAt *time.Time `json:"confirmed_at"` Permissions datatypes.JSONType[[]string] `json:"permissions"` } diff --git a/pkg/models/profiles.go b/pkg/models/profiles.go new file mode 100644 index 0000000..d2da447 --- /dev/null +++ b/pkg/models/profiles.go @@ -0,0 +1,14 @@ +package models + +import "time" + +type AccountProfile struct { + BaseModel + + FirstName string `json:"first_name"` + MiddleName string `json:"middle_name"` + LastName string `json:"last_name"` + Experience uint64 `json:"experience"` + Birthday *time.Time `json:"birthday"` + AccountID uint `json:"account_id"` +} diff --git a/pkg/server/accounts_api.go b/pkg/server/accounts_api.go index 2f707a7..53c9a1b 100644 --- a/pkg/server/accounts_api.go +++ b/pkg/server/accounts_api.go @@ -3,10 +3,24 @@ package server import ( "code.smartsheep.studio/hydrogen/passport/pkg/database" "code.smartsheep.studio/hydrogen/passport/pkg/models" - "code.smartsheep.studio/hydrogen/passport/pkg/security" + "code.smartsheep.studio/hydrogen/passport/pkg/services" "github.com/gofiber/fiber/v2" + "gorm.io/gorm/clause" ) +func getPrincipal(c *fiber.Ctx) error { + user := c.Locals("principal").(models.Account) + + var data models.Account + if err := database.C.Where(&models.Account{ + BaseModel: models.BaseModel{ID: user.ID}, + }).Preload(clause.Associations).First(&data).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(data) +} + func doRegister(c *fiber.Ctx) error { var data struct { Name string `json:"name"` @@ -19,28 +33,14 @@ func doRegister(c *fiber.Ctx) error { return err } - user := models.Account{ - Name: data.Name, - Nick: data.Nick, - State: models.PendingAccountState, - Factors: []models.AuthFactor{ - { - Type: models.PasswordAuthFactor, - Secret: security.HashPassword(data.Password), - }, - }, - Contacts: []models.AccountContact{ - { - Type: models.EmailAccountContact, - Content: data.Email, - VerifiedAt: nil, - }, - }, - } - - if err := database.C.Create(&user).Error; err != nil { + if user, err := services.CreateAccount( + data.Name, + data.Nick, + data.Email, + data.Password, + ); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.JSON(user) } - - return c.JSON(user) } diff --git a/pkg/server/profiles_api.go b/pkg/server/profiles_api.go deleted file mode 100644 index 8d82612..0000000 --- a/pkg/server/profiles_api.go +++ /dev/null @@ -1,12 +0,0 @@ -package server - -import ( - "code.smartsheep.studio/hydrogen/passport/pkg/models" - "github.com/gofiber/fiber/v2" -) - -func aboutMe(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) - - return c.JSON(user) -} diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 559dd91..22a61de 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -21,7 +21,7 @@ func NewServer() { api := A.Group("/api").Name("API") { - api.Get("/users/me", auth, aboutMe) + api.Get("/users/me", auth, getPrincipal) api.Post("/users", doRegister) api.Put("/auth", startChallenge) diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index aefe952..d79b85f 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -1,10 +1,11 @@ package services import ( - "fmt" - "code.smartsheep.studio/hydrogen/passport/pkg/database" "code.smartsheep.studio/hydrogen/passport/pkg/models" + "code.smartsheep.studio/hydrogen/passport/pkg/security" + "fmt" + "gorm.io/datatypes" ) func GetAccount(id uint) (models.Account, error) { @@ -36,3 +37,35 @@ func LookupAccount(id string) (models.Account, error) { return account, fmt.Errorf("account was not found") } + +func CreateAccount(name, nick, email, password string) (models.Account, error) { + user := models.Account{ + Name: name, + Nick: nick, + State: models.PendingAccountState, + Profile: models.AccountProfile{ + Experience: 100, + }, + Factors: []models.AuthFactor{ + { + Type: models.PasswordAuthFactor, + Secret: security.HashPassword(password), + }, + }, + Contacts: []models.AccountContact{ + { + Type: models.EmailAccountContact, + Content: email, + VerifiedAt: nil, + }, + }, + Permissions: datatypes.NewJSONType(make([]string, 0)), + ConfirmedAt: nil, + } + + if err := database.C.Create(&user).Error; err != nil { + return user, err + } else { + return user, nil + } +}