email service modify

This commit is contained in:
geek 2017-06-26 16:11:57 +09:00
parent d7bf02db9f
commit c85aa7c70d
2 changed files with 35 additions and 42 deletions

View File

@ -13,6 +13,7 @@ import (
"net/mail" "net/mail"
"net/smtp" "net/smtp"
"strings" "strings"
"git.loafle.net/overflow/overflow_service/proxy/utils"
) )
const ( const (
@ -24,18 +25,15 @@ const (
) )
type Email struct { type Email struct {
Id json.Number `json:"id,Number,omitempty"` Id json.Number `json:"id,Number,omitempty"`
Member member.Member `json:"member"` Member member.Member `json:"member"`
From string `json:"_"` From string `json:"_"`
Subj string `json:"_"` Subj string `json:"_"`
AuthToken string `json:"authToken"` EmailAuthKey string `json:"EmailAuthKey"`
IsInvalid bool `json:"isInvalid"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
IsConfirm bool `json:"isConfirm"` authConfirmDate timestamp.Timestamp `json:"authConfirmDate,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"` SmtpServer string `json:"_"`
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"` BodyMsg string `json:"_"`
ConfirmDate timestamp.Timestamp `json:"confirmDate,omitempty"`
SmtpServer string `json:"_"`
BodyMsg string `json:"_"`
} }
func NewEmail(member member.Member, subject string) *Email { func NewEmail(member member.Member, subject string) *Email {
@ -111,9 +109,7 @@ func (es *EmailService) SendEmailForAuth(e *Email) error {
} }
//Todo auth token generation //Todo auth token generation
e.AuthToken = es.generationAuthToken(e) e.EmailAuthKey = es.generationAuthToken(e)
e.IsInvalid = false
e.IsConfirm = false
conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig) conn, err := tls.Dial("tcp", SERVER_NAME, tlsconfig)
es.checkError(err) es.checkError(err)
@ -150,25 +146,15 @@ func (es *EmailService) SendEmailForAuth(e *Email) error {
return err return err
} }
func (es *EmailService) getEmailMap(e *Email) map[string]string {
emMap := make(map[string]string)
str, err := json.Marshal(e)
if err != nil {
log.Fatal("Json Marshal Error: ", err)
}
emMap["com.loafle.overflow.email.model.EmailAuth"] = string(str)
return emMap
}
func (es *EmailService) saveEmail(e *Email) { func (es *EmailService) saveEmail(e *Email) {
memMap := es.getEmailMap(e) rr, err := utils.InvokeDBByModel("emailAuth","save", e, utils.MODEL_EMAIL_AUTH)
proxy.InvokeDB("emailAuth", "create", memMap)
if err != nil {
log.Fatal(err)
}
log.Println(rr)
} }
@ -176,23 +162,24 @@ func (es *EmailService) CheckAuthURL(e *Email) bool {
//Query from the database with an authentication token. //Query from the database with an authentication token.
emMap := es.getEmailMap(e) re, err := utils.InvokeDBByModel("emailAuth","findByEmailAuthKey", e.EmailAuthKey, utils.MODEL_STRING)
if err != nil {
log.Fatal(err)
}
re := proxy.InvokeDB("emailAuth", "findByAuthToken", emMap)
tempEmail := &Email{} tempEmail := &Email{}
json.Unmarshal([]byte(re), tempEmail) json.Unmarshal([]byte(re), tempEmail)
r := strings.Compare(e.AuthToken, tempEmail.AuthToken) r := strings.Compare(e.EmailAuthKey, tempEmail.EmailAuthKey)
//Todo Check for valid //Todo Check for valid
log.Println(tempEmail) log.Println(tempEmail)
if r == 0 { if r == 0 {
// Todo isConfirm change true and db update // Todo isConfirm change true and db update
tempEmail.IsConfirm = true tempEmail.authConfirmDate = timestamp.Now()
tempEmail.UpdateDate = timestamp.Now()
tempEmail.ConfirmDate = timestamp.Now()
es.modifyEmailAuth(tempEmail) es.modifyEmailAuth(tempEmail)
return true return true
@ -201,7 +188,13 @@ func (es *EmailService) CheckAuthURL(e *Email) bool {
} }
func (es *EmailService) modifyEmailAuth(e *Email) { func (es *EmailService) modifyEmailAuth(e *Email) {
emMap := es.getEmailMap(e)
proxy.InvokeDB("emailAuth", "update", emMap) proxy.InvokeDB("emailAuth", "update", emMap)
re, err := utils.InvokeDBByModel("emailAuth","save", e, utils.MODEL_EMAIL_AUTH)
if err != nil {
log.Fatal(err)
}
log.Println(re)
} }

View File

@ -12,8 +12,8 @@ func getEmailObj() (*Email,*EmailService) {
m := member.Member{ m := member.Member{
Email:"geek@loafle.com", Email:"geek@loafle.com",
Name:"geek", Name:"geek",
Company:"loafle", CompanyName:"loafle",
Id:json.Number("4"), Id:json.Number("2"),
} }
e := NewEmail(m, "Hello Oveflow") e := NewEmail(m, "Hello Oveflow")
@ -29,7 +29,7 @@ func TestEmailService_CheckAuthURL(t *testing.T) {
e, es := getEmailObj() e, es := getEmailObj()
//e.Id = json.Number("2") //e.Id = json.Number("2")
e.AuthToken = "3C03F8AB-1D4D-4C8A-8C36-EE2D644988B5" e.EmailAuthKey = "3C03F8AB-1D4D-4C8A-8C36-EE2D644988B5"
rr := es.CheckAuthURL(e) rr := es.CheckAuthURL(e)