feature: add password changed notify (#647)

* feature: add password changed notify
This commit is contained in:
yubo 2021-04-09 11:21:09 +08:00 committed by GitHub
parent 72573e32cb
commit 71984c72b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 9 deletions

View File

@ -0,0 +1 @@
您好,您的密码已被重置

View File

@ -128,6 +128,7 @@ var (
"operation is blank": "operation 未设置",
"Reset Password": "重置密码",
"Login Code": "登录验证码",
"[Notify] Password Changed": "[通知] 账号密码修改",
},
}
)

View File

@ -30,6 +30,7 @@ import (
var (
loginCodeSmsTpl *template.Template
loginCodeEmailTpl *template.Template
passwordChangedEmailTpl *template.Template
errUnsupportCaptcha = errors.New("unsupported captcha")
// https://captcha.mojotv.cn
@ -74,6 +75,16 @@ func init() {
if err != nil {
log.Fatalf("open %s err: %s", filename, err)
}
filename, err = getConfigFile("password-changed-email", "tpl")
if err != nil {
log.Fatal(err)
}
passwordChangedEmailTpl, err = template.ParseFiles(filename)
if err != nil {
log.Fatalf("open %s err: %s", filename, err)
}
}
// login for UI
@ -525,6 +536,10 @@ func rstPassword(c *gin.Context) {
if err := auth.ChangePassword(user, in.Password); err != nil {
return err
}
if err := passwordChangedNotify(user); err != nil {
logger.Warningf("password changed notify error %s", err)
}
lc.Del()
return nil
}()
@ -756,3 +771,12 @@ func sessionDestory(c *gin.Context) (sid string, err error) {
return
}
func passwordChangedNotify(user *models.User) error {
var buf bytes.Buffer
if err := passwordChangedEmailTpl.Execute(&buf, nil); err != nil {
return err
}
return redisc.Write(&dataobj.Message{Tos: []string{user.Email}, Subject: _s("[Notify] Password Changed"), Content: buf.String()}, config.MAIL_QUEUE_NAME)
}

View File

@ -5,6 +5,7 @@ import (
"github.com/didi/nightingale/src/modules/rdb/auth"
"github.com/didi/nightingale/src/modules/rdb/config"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/logger"
)
func selfProfileGet(c *gin.Context) {
@ -58,7 +59,15 @@ func selfPasswordPut(c *gin.Context) {
return _e("Incorrect old password")
}
return auth.ChangePassword(user, f.NewPass)
if err := auth.ChangePassword(user, f.NewPass); err != nil {
return err
}
if err := passwordChangedNotify(user); err != nil {
logger.Warningf("password changed notify error %s", err)
}
return nil
}()
renderMessage(c, err)

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/str"
"github.com/didi/nightingale/src/models"
@ -200,6 +201,9 @@ func userPasswordPut(c *gin.Context) {
err := auth.ChangePassword(user, f.Password)
if err == nil {
if err := passwordChangedNotify(user); err != nil {
logger.Warningf("password changed notify error %s", err)
}
go models.OperationLogNew(root.Username, "user", user.Id, fmt.Sprintf("UserChangePassword %s", user.Username))
}
renderMessage(c, err)