Skip to content

Commit

Permalink
fix: error for the account activate
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Feb 6, 2021
1 parent 91d9ac9 commit ff1fcea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
5 changes: 4 additions & 1 deletion internal/app/model/user_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package model

import "time"

const UserStorageDefaultSize = 50 << 20
const (
UserStorageDefaultSize = 50 << 20
UserStorageActiveSize = 1024 << 20
)

type UserStorage struct {
Id int64 `json:"id"`
Expand Down
19 changes: 9 additions & 10 deletions internal/app/service/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const (

var (
mailLinks = map[string]string{
mailKindAccountActive: "%s/zplat/signin/%s",
mailKindPasswordReset: "%s/zplat/password-reset/%s",
mailKindAccountActive: "%s/u/signin/%s",
mailKindPasswordReset: "%s/u/password-reset/%s",
}
defaultTemplates = map[string]string{
mailKindAccountActive: "<h3>账户激活链接</h3>\n <p><a href=\"%s\">点击此处账户激活</a></p>\n\t\t<p>如果您没有进行账号注册请忽略!</p>",
Expand All @@ -35,8 +35,8 @@ type Mail struct {
dialer *gomail.Dialer

from string
enabled bool
templates map[string]string
enabled bool
}

func NewMail() *Mail {
Expand Down Expand Up @@ -76,27 +76,26 @@ func (m *Mail) Boot(opt model.Opts) error {
return nil
}

func (m *Mail) NotifyActive(email string, token string) error {
func (m *Mail) NotifyActive(siteAddr, email string, token string) error {
msg := gomail.NewMessage()
msg.SetHeader("From", m.from)
msg.SetHeader("To", email)
msg.SetHeader("Subject", "账户激活")
msg.SetBody("text/html", m.buildMailBody(mailKindAccountActive, email, token))
msg.SetBody("text/html", m.buildMailBody(mailKindAccountActive, siteAddr, email, token))
return m.dialer.DialAndSend(msg)
}

func (m *Mail) NotifyPasswordReset(email string, token string) error {
func (m *Mail) NotifyPasswordReset(siteAddr, email, token string) error {
msg := gomail.NewMessage()
msg.SetHeader("From", m.from)
msg.SetHeader("To", email)
msg.SetHeader("Subject", "密码重置申请")
msg.SetBody("text/html", m.buildMailBody(mailKindPasswordReset, email, token))
msg.SetBody("text/html", m.buildMailBody(mailKindPasswordReset, siteAddr, email, token))
return m.dialer.DialAndSend(msg)
}

func (m *Mail) buildMailBody(kind, email, token string) string {
origin := ""
link := fmt.Sprintf(mailLinks[kind], origin, encodeToKey(email, token))
func (m *Mail) buildMailBody(kind, siteAddr, email, token string) string {
link := fmt.Sprintf(mailLinks[kind], siteAddr, encodeToKey(email, token))
return fmt.Sprintf(m.templates[kind], link)
}

Expand Down
12 changes: 10 additions & 2 deletions internal/app/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (u *User) Signup(email, password string, opt model.UserCreateOption) (*mode
return nil, err
}

return mUser, u.sMail.NotifyActive(email, token)
return mUser, u.sMail.NotifyActive(opt.Origin, email, token)
}

return mUser, nil
Expand All @@ -67,6 +67,14 @@ func (u *User) Active(token string) error {
}

uid, _ := strconv.ParseInt(rc.Subject, 10, 64)
user, err := u.dUser.Find(uid)
if err != nil {
return err
} else if user.Status >= model.StatusActivated {
return fmt.Errorf("account already activated")
}

u.dUser.UpdateStorage(uid, model.UserStorageActiveSize) // 激活即送1G空间
return u.dUser.Activate(uid)
}

Expand Down Expand Up @@ -121,7 +129,7 @@ func (u *User) PasswordResetApply(origin, email string) error {
return err
}

return u.sMail.NotifyPasswordReset(email, token)
return u.sMail.NotifyPasswordReset(origin, email, token)
}

func (u *User) PasswordReset(token, password string) error {
Expand Down

0 comments on commit ff1fcea

Please sign in to comment.