mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
Use signup table in Signup API.
This commit is contained in:
@ -18,6 +18,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/casdoor/casdoor/object"
|
||||
@ -77,9 +78,7 @@ func (c *ApiController) Signup() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() != "" {
|
||||
resp = Response{Status: "error", Msg: "Please log out first before signing up", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
c.ResponseErrorWithData("Please sign out first before signing up", c.GetSessionUser())
|
||||
return
|
||||
}
|
||||
|
||||
@ -89,61 +88,78 @@ func (c *ApiController) Signup() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
checkResult := object.CheckVerificationCode(form.Email, form.EmailCode)
|
||||
if len(checkResult) != 0 {
|
||||
responseText := fmt.Sprintf("Email%s", checkResult)
|
||||
c.ResponseError(responseText)
|
||||
return
|
||||
}
|
||||
|
||||
checkPhone := fmt.Sprintf("+%s%s", form.PhonePrefix, form.Phone)
|
||||
checkResult = object.CheckVerificationCode(checkPhone, form.PhoneCode)
|
||||
if len(checkResult) != 0 {
|
||||
responseText := fmt.Sprintf("Phone%s", checkResult)
|
||||
c.ResponseError(responseText)
|
||||
return
|
||||
}
|
||||
|
||||
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
|
||||
if !application.EnableSignUp {
|
||||
resp = Response{Status: "error", Msg: "The application does not allow to sign up new account", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
c.ResponseError("The application does not allow to sign up new account")
|
||||
return
|
||||
}
|
||||
|
||||
if application.IsSignupItemEnabled("Email") {
|
||||
checkResult := object.CheckVerificationCode(form.Email, form.EmailCode)
|
||||
if len(checkResult) != 0 {
|
||||
c.ResponseError(fmt.Sprintf("Email%s", checkResult))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var checkPhone string
|
||||
if application.IsSignupItemEnabled("Phone") {
|
||||
checkPhone = fmt.Sprintf("+%s%s", form.PhonePrefix, form.Phone)
|
||||
checkResult := object.CheckVerificationCode(checkPhone, form.PhoneCode)
|
||||
if len(checkResult) != 0 {
|
||||
c.ResponseError(fmt.Sprintf("Phone%s", checkResult))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
userId := fmt.Sprintf("%s/%s", form.Organization, form.Username)
|
||||
msg := object.CheckUserSignup(form.Organization, form.Username, form.Password, form.Name, form.Email, form.Phone, form.Affiliation)
|
||||
|
||||
organization := object.GetOrganization(fmt.Sprintf("%s/%s", "admin", form.Organization))
|
||||
msg := object.CheckUserSignup(application, organization, form.Username, form.Password, form.Name, form.Email, form.Phone, form.Affiliation)
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg, Data: ""}
|
||||
} else {
|
||||
user := &object.User{
|
||||
Owner: form.Organization,
|
||||
Name: form.Username,
|
||||
CreatedTime: util.GetCurrentTime(),
|
||||
Id: util.GenerateId(),
|
||||
Type: "normal-user",
|
||||
Password: form.Password,
|
||||
DisplayName: form.Name,
|
||||
Avatar: "https://casbin.org/img/casbin.svg",
|
||||
Email: form.Email,
|
||||
Phone: form.Phone,
|
||||
Affiliation: form.Affiliation,
|
||||
IsAdmin: false,
|
||||
IsGlobalAdmin: false,
|
||||
IsForbidden: false,
|
||||
Properties: map[string]string{},
|
||||
}
|
||||
object.AddUser(user)
|
||||
|
||||
//c.SetSessionUser(user)
|
||||
|
||||
object.DisableVerificationCode(form.Email)
|
||||
object.DisableVerificationCode(checkPhone)
|
||||
util.LogInfo(c.Ctx, "API: [%s] is signed up as new user", userId)
|
||||
resp = Response{Status: "ok", Msg: "", Data: userId}
|
||||
c.ResponseError(msg)
|
||||
return
|
||||
}
|
||||
|
||||
id := util.GenerateId()
|
||||
if application.GetSignupItemRule("ID") == "Incremental" {
|
||||
lastUser := object.GetLastUser(form.Organization)
|
||||
lastIdInt := util.ParseInt(lastUser.Id)
|
||||
id = strconv.Itoa(lastIdInt + 1)
|
||||
}
|
||||
|
||||
username := form.Username
|
||||
if !application.IsSignupItemVisible("Username") {
|
||||
username = id
|
||||
}
|
||||
|
||||
user := &object.User{
|
||||
Owner: form.Organization,
|
||||
Name: username,
|
||||
CreatedTime: util.GetCurrentTime(),
|
||||
Id: id,
|
||||
Type: "normal-user",
|
||||
Password: form.Password,
|
||||
DisplayName: form.Name,
|
||||
Avatar: "https://casbin.org/img/casbin.svg",
|
||||
Email: form.Email,
|
||||
Phone: form.Phone,
|
||||
Affiliation: form.Affiliation,
|
||||
IsAdmin: false,
|
||||
IsGlobalAdmin: false,
|
||||
IsForbidden: false,
|
||||
Properties: map[string]string{},
|
||||
}
|
||||
object.AddUser(user)
|
||||
|
||||
//c.SetSessionUser(user)
|
||||
|
||||
object.DisableVerificationCode(form.Email)
|
||||
object.DisableVerificationCode(checkPhone)
|
||||
|
||||
util.LogInfo(c.Ctx, "API: [%s] is signed up as new user", userId)
|
||||
|
||||
resp = Response{Status: "ok", Msg: "", Data: userId}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
@ -58,6 +58,11 @@ func (c *ApiController) ResponseError(error string) {
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) ResponseErrorWithData(error string, data interface{}) {
|
||||
c.Data["json"] = Response{Status: "error", Msg: error, Data: data}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) RequireSignedIn() (string, bool) {
|
||||
userId := c.GetSessionUser()
|
||||
if userId == "" {
|
||||
|
Reference in New Issue
Block a user