mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 10:45:47 +08:00
Add user to VerificationRecord.
This commit is contained in:
parent
dbf11d61a7
commit
290c5aac9e
@ -22,6 +22,17 @@ import (
|
|||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c *ApiController) getCurrentUser() *object.User {
|
||||||
|
var user *object.User
|
||||||
|
userId := c.GetSessionUser()
|
||||||
|
if userId == "" {
|
||||||
|
user = nil
|
||||||
|
} else {
|
||||||
|
user = object.GetUser(userId)
|
||||||
|
}
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ApiController) SendVerificationCode() {
|
func (c *ApiController) SendVerificationCode() {
|
||||||
destType := c.Ctx.Request.Form.Get("type")
|
destType := c.Ctx.Request.Form.Get("type")
|
||||||
dest := c.Ctx.Request.Form.Get("dest")
|
dest := c.Ctx.Request.Form.Get("dest")
|
||||||
@ -48,6 +59,7 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user := c.getCurrentUser()
|
||||||
organization := object.GetOrganization(orgId)
|
organization := object.GetOrganization(orgId)
|
||||||
application := object.GetApplicationByOrganizationName(organization.Name)
|
application := object.GetApplicationByOrganizationName(organization.Name)
|
||||||
|
|
||||||
@ -60,7 +72,7 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
provider := application.GetEmailProvider()
|
provider := application.GetEmailProvider()
|
||||||
msg = object.SendVerificationCodeToEmail(provider, remoteAddr, dest)
|
msg = object.SendVerificationCodeToEmail(user, provider, remoteAddr, dest)
|
||||||
case "phone":
|
case "phone":
|
||||||
if !util.IsPhoneCnValid(dest) {
|
if !util.IsPhoneCnValid(dest) {
|
||||||
c.ResponseError("Invalid phone number")
|
c.ResponseError("Invalid phone number")
|
||||||
@ -74,7 +86,7 @@ func (c *ApiController) SendVerificationCode() {
|
|||||||
|
|
||||||
dest = fmt.Sprintf("+%s%s", org.PhonePrefix, dest)
|
dest = fmt.Sprintf("+%s%s", org.PhonePrefix, dest)
|
||||||
provider := application.GetSmsProvider()
|
provider := application.GetSmsProvider()
|
||||||
msg = object.SendVerificationCodeToPhone(provider, remoteAddr, dest)
|
msg = object.SendVerificationCodeToPhone(user, provider, remoteAddr, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
status := "ok"
|
status := "ok"
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
type VerificationRecord struct {
|
type VerificationRecord struct {
|
||||||
RemoteAddr string `xorm:"varchar(100) notnull pk"`
|
RemoteAddr string `xorm:"varchar(100) notnull pk"`
|
||||||
Type string `xorm:"varchar(10) notnull pk"`
|
Type string `xorm:"varchar(10) notnull pk"`
|
||||||
|
User string `xorm:"varchar(100) notnull"`
|
||||||
Provider string `xorm:"varchar(100) notnull"`
|
Provider string `xorm:"varchar(100) notnull"`
|
||||||
Receiver string `xorm:"varchar(100) notnull"`
|
Receiver string `xorm:"varchar(100) notnull"`
|
||||||
Code string `xorm:"varchar(10) notnull"`
|
Code string `xorm:"varchar(10) notnull"`
|
||||||
@ -33,7 +34,7 @@ type VerificationRecord struct {
|
|||||||
IsUsed bool
|
IsUsed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendVerificationCodeToEmail(provider *Provider, remoteAddr string, dest string) string {
|
func SendVerificationCodeToEmail(user *User, provider *Provider, remoteAddr string, dest string) string {
|
||||||
if provider == nil {
|
if provider == nil {
|
||||||
return "Please set an Email provider first"
|
return "Please set an Email provider first"
|
||||||
}
|
}
|
||||||
@ -43,7 +44,7 @@ func SendVerificationCodeToEmail(provider *Provider, remoteAddr string, dest str
|
|||||||
code := getRandomCode(5)
|
code := getRandomCode(5)
|
||||||
content := fmt.Sprintf("You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes.", code)
|
content := fmt.Sprintf("You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes.", code)
|
||||||
|
|
||||||
if result := AddToVerificationRecord(provider.Name, remoteAddr, "Email", dest, code); len(result) != 0 {
|
if result := AddToVerificationRecord(user, provider, remoteAddr, "Email", dest, code); len(result) != 0 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,24 +59,27 @@ func SendVerificationCodeToEmail(provider *Provider, remoteAddr string, dest str
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendVerificationCodeToPhone(provider *Provider, remoteAddr string, dest string) string {
|
func SendVerificationCodeToPhone(user *User, provider *Provider, remoteAddr string, dest string) string {
|
||||||
if provider == nil {
|
if provider == nil {
|
||||||
return "Please set a SMS provider first"
|
return "Please set a SMS provider first"
|
||||||
}
|
}
|
||||||
|
|
||||||
code := getRandomCode(5)
|
code := getRandomCode(5)
|
||||||
if result := AddToVerificationRecord(provider.Name, remoteAddr, "SMS", dest, code); len(result) != 0 {
|
if result := AddToVerificationRecord(user, provider, remoteAddr, "SMS", dest, code); len(result) != 0 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
return SendCodeToPhone(provider, dest, code)
|
return SendCodeToPhone(provider, dest, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddToVerificationRecord(providerName, remoteAddr, recordType, dest, code string) string {
|
func AddToVerificationRecord(user *User, provider *Provider, remoteAddr, recordType, dest, code string) string {
|
||||||
var record VerificationRecord
|
var record VerificationRecord
|
||||||
record.RemoteAddr = remoteAddr
|
record.RemoteAddr = remoteAddr
|
||||||
record.Type = recordType
|
record.Type = recordType
|
||||||
record.Provider = providerName
|
if user != nil {
|
||||||
|
record.User = user.GetId()
|
||||||
|
}
|
||||||
|
record.Provider = provider.Name
|
||||||
has, err := adapter.Engine.Get(&record)
|
has, err := adapter.Engine.Get(&record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user