feat(init):完成创建最初始用户功能。

This commit is contained in:
徐涛
2022-08-12 21:30:43 +08:00
parent a9f281d4df
commit 18297feba1
2 changed files with 88 additions and 4 deletions

View File

@@ -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
}