Files
leanote/app/service/PwdService.go

63 lines
1.3 KiB
Go
Raw Normal View History

2014-05-07 13:06:24 +08:00
package service
import (
"github.com/leanote/leanote/app/db"
"github.com/leanote/leanote/app/info"
2015-09-07 00:29:02 +08:00
. "github.com/leanote/leanote/app/lea"
2015-11-13 17:58:41 +08:00
"gopkg.in/mgo.v2/bson"
2014-05-07 13:06:24 +08:00
)
// 找回密码
// 修改密码
var overHours = 2.0 // 小时后过期
type PwdService struct {
}
2015-11-13 17:58:41 +08:00
// 1. 找回密码, 通过email找用户,
2014-05-07 13:06:24 +08:00
// 用户存在, 生成code
func (this *PwdService) FindPwd(email string) (ok bool, msg string) {
ok = false
userId := userService.GetUserId(email)
if userId == "" {
msg = "用户不存在"
return
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
token := tokenService.NewToken(userId, email, info.TokenPwd)
if token == "" {
return false, "db error"
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 发送邮件
2014-10-22 16:20:45 +08:00
ok, msg = emailService.FindPwdSendEmail(token, email)
2014-05-07 13:06:24 +08:00
return
}
2015-11-28 14:28:51 +08:00
// 重置密码时
2014-05-07 13:06:24 +08:00
// 修改密码
// 先验证
func (this *PwdService) UpdatePwd(token, pwd string) (bool, string) {
var tokenInfo info.Token
var ok bool
var msg string
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 先验证
if ok, msg, tokenInfo = tokenService.VerifyToken(token, info.TokenPwd); !ok {
return ok, msg
}
2015-11-28 14:28:51 +08:00
passwd := GenPwd(pwd)
if passwd == "" {
2015-11-13 17:58:41 +08:00
return false, "GenerateHash error"
2015-09-06 23:16:56 +08:00
}
2015-11-28 14:28:51 +08:00
2014-05-07 13:06:24 +08:00
// 修改密码之
2015-09-06 23:16:56 +08:00
ok = db.UpdateByQField(db.Users, bson.M{"_id": tokenInfo.UserId}, "Pwd", passwd)
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 删除token
tokenService.DeleteToken(tokenInfo.UserId.Hex(), info.TokenPwd)
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
return ok, ""
2015-11-13 17:58:41 +08:00
}