From 18297feba104d0a1ff2a97cf26d45c5d9215b5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 12 Aug 2022 21:30:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(init):=E5=AE=8C=E6=88=90=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=9C=80=E5=88=9D=E5=A7=8B=E7=94=A8=E6=88=B7=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 40 +++++++++++++++++++++++++++++++++++++ service/user.go | 52 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index efe269f..ccb9645 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "electricity_bill_calc/global" "electricity_bill_calc/model" "electricity_bill_calc/router" + "electricity_bill_calc/service" "electricity_bill_calc/utils" "encoding/csv" "fmt" @@ -15,6 +16,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/shopspring/decimal" ) func init() { @@ -58,6 +60,12 @@ func init() { log.Fatalf("Regions initialize failed: %v", err) } log.Println("Regions synchronized.") + + err = intializeSingularity() + if err != nil { + log.Fatalf("Singularity account intialize failed: %v", err) + } + log.Println("Singularity account intialized.") } func initializeRegions() error { @@ -102,6 +110,38 @@ func initializeRegions() error { return nil } +func intializeSingularity() error { + singularityExists, err := service.UserService.IsUserExists("000") + if err != nil { + return fmt.Errorf("singularity detect failed: %w", err) + } + if singularityExists { + return nil + } + singularity := &model.User{ + Id: "000", + Username: "singularity", + Type: 2, + Enabled: true, + } + singularityName := "Singularity" + singularityExpires, err := time.Parse("2006-01-02 15:04:05", "2099-12-31 23:59:59") + if err != nil { + return fmt.Errorf("singularity expires time parse failed: %w", err) + } + singularityDetail := &model.UserDetail{ + Name: &singularityName, + UnitServiceFee: decimal.Zero, + ServiceExpiration: singularityExpires, + } + verifyCode, err := service.UserService.CreateUser(singularity, singularityDetail) + if err != nil { + return fmt.Errorf("singularity account failed to create: %w", err) + } + log.Printf("Singularity account created, use %s as verify code to reset password.", verifyCode) + return nil +} + func DBConnectionKeepLive() { for range time.Tick(30 * time.Second) { err := global.DBConn.Ping() diff --git a/service/user.go b/service/user.go index 7be26e8..37ac2c8 100644 --- a/service/user.go +++ b/service/user.go @@ -10,6 +10,7 @@ import ( "electricity_bill_calc/repository" "electricity_bill_calc/utils" "fmt" + "log" "time" "github.com/google/uuid" @@ -45,6 +46,10 @@ func (_UserService) ProcessEnterpriseUserLogin(username, password string) (*mode authErr.NeedReset = true return nil, authErr } + userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id) + if userDetial.ServiceExpiration.Before(time.Now()) { + return nil, exceptions.NewAuthenticationError(401, "用户服务期限已过。") + } session := &model.Session{ Token: uuid.New().String(), Uid: user.Id, @@ -52,7 +57,6 @@ func (_UserService) ProcessEnterpriseUserLogin(username, password string) (*mode Name: user.Username, ExpiresAt: time.Now().Add(config.ServiceSettings.MaxSessionLife), } - userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id) if userDetial != nil { session.Name = *userDetial.Name } @@ -109,7 +113,7 @@ func (_UserService) InvalidUserPassword(uid string) (string, error) { verifyCode := utils.RandStr(10) hash := sha512.New512_256() hash.Write([]byte(verifyCode)) - user.Password = string(hash.Sum(nil)) + user.Password = fmt.Sprintf("%x", hash.Sum(nil)) user.ResetNeeded = true affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user) if err != nil { @@ -132,7 +136,7 @@ func (_UserService) VerifyUserPassword(username, verifyCode string) (bool, error } hash := sha512.New512_256() hash.Write([]byte(verifyCode)) - hashedVerifyCode := string(hash.Sum(nil)) + hashedVerifyCode := fmt.Sprintf("%x", hash.Sum(nil)) if hashedVerifyCode != user.Password { return false, nil } else { @@ -147,7 +151,7 @@ func (_UserService) ResetUserPassword(username, password string) (bool, error) { } hash := sha512.New512_256() hash.Write([]byte(password)) - user.Password = string(hash.Sum(nil)) + user.Password = fmt.Sprintf("%x", hash.Sum(nil)) user.ResetNeeded = false affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user) if err != nil { @@ -161,3 +165,43 @@ func (_UserService) ResetUserPassword(username, password string) (bool, error) { return false, nil } } + +func (_UserService) IsUserExists(uid string) (bool, error) { + return global.DBConn.ID(uid).Exist(&model.User{}) +} + +func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) { + if len(user.Id) == 0 { + user.Id = uuid.New().String() + } + detail.Id = user.Id + + verifyCode := utils.RandStr(10) + hash := sha512.New512_256() + hash.Write([]byte(verifyCode)) + user.Password = fmt.Sprintf("%x", hash.Sum(nil)) + user.ResetNeeded = true + + tx := global.DBConn.NewSession() + defer tx.Close() + if err := tx.Begin(); err != nil { + return "", err + } + log.Printf("[debug]user: %v", user) + _, err := tx.Insert(user) + if err != nil { + tx.Rollback() + return "", fmt.Errorf("user create failed: %w", err) + } + _, err = tx.Insert(detail) + if err != nil { + tx.Rollback() + return "", fmt.Errorf("user Detail create failed: %w", err) + } + err = tx.Commit() + if err != nil { + tx.Rollback() + return "", fmt.Errorf("transaction commit unsuccessful: %w", err) + } + return verifyCode, nil +}