Refactor out GenerateIdForNewUser()

This commit is contained in:
Yang Luo 2023-09-20 22:45:00 +08:00
parent 3b2820cbe3
commit f40d44fa1c
2 changed files with 25 additions and 16 deletions

View File

@ -18,7 +18,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"strings" "strings"
"github.com/casdoor/casdoor/form" "github.com/casdoor/casdoor/form"
@ -119,20 +118,10 @@ func (c *ApiController) Signup() {
} }
} }
id := util.GenerateId() id, err := object.GenerateIdForNewUser(application)
if application.GetSignupItemRule("ID") == "Incremental" { if err != nil {
lastUser, err := object.GetLastUser(authForm.Organization) c.ResponseError(err.Error())
if err != nil { return
c.ResponseError(err.Error())
return
}
lastIdInt := -1
if lastUser != nil {
lastIdInt = util.ParseInt(lastUser.Id)
}
id = strconv.Itoa(lastIdInt + 1)
} }
username := authForm.Username username := authForm.Username

View File

@ -16,6 +16,7 @@ package object
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/casdoor/casdoor/conf" "github.com/casdoor/casdoor/conf"
@ -483,7 +484,7 @@ func GetMaskedUsers(users []*User, errs ...error) ([]*User, error) {
return users, nil return users, nil
} }
func GetLastUser(owner string) (*User, error) { func getLastUser(owner string) (*User, error) {
user := User{Owner: owner} user := User{Owner: owner}
existed, err := ormer.Engine.Desc("created_time", "id").Get(&user) existed, err := ormer.Engine.Desc("created_time", "id").Get(&user)
if err != nil { if err != nil {
@ -900,3 +901,22 @@ func (user *User) IsGlobalAdmin() bool {
return user.Owner == "built-in" return user.Owner == "built-in"
} }
func GenerateIdForNewUser(application *Application) (string, error) {
if application.GetSignupItemRule("ID") != "Incremental" {
return util.GenerateId(), nil
}
lastUser, err := getLastUser(application.Organization)
if err != nil {
return "", err
}
lastUserId := -1
if lastUser != nil {
lastUserId = util.ParseInt(lastUser.Id)
}
res := strconv.Itoa(lastUserId + 1)
return res, nil
}