本文整理汇总了Golang中github.com/silvasur/mailremind/model.User.SetPWHash方法的典型用法代码示例。如果您正苦于以下问题:Golang User.SetPWHash方法的具体用法?Golang User.SetPWHash怎么用?Golang User.SetPWHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/silvasur/mailremind/model.User
的用法示例。
在下文中一共展示了User.SetPWHash方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: settings
func settings(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) {
if user == nil {
return &settingsTpldata{Error: "You need to be logged in to do that.", Fatal: true}, nil
}
outdata := &settingsTpldata{Timezones: make(map[string]bool)}
tznow := user.Location().String()
for _, tz := range timeLocs {
outdata.Timezones[tz] = (tz == tznow)
}
if req.Method != "POST" {
return outdata, user
}
if err := req.ParseForm(); err != nil {
outdata.Error = "Could not parse form"
return outdata, user
}
switch req.FormValue("M") {
case "setpasswd":
if req.FormValue("Password") == "" {
outdata.Error = "Password must not be empty."
return outdata, user
}
if req.FormValue("Password") != req.FormValue("RepeatPassword") {
outdata.Error = "Passwords must be equal."
return outdata, user
}
hash, err := bcrypt.GenerateFromPassword([]byte(req.FormValue("Password")), bcrypt.DefaultCost)
if err != nil {
log.Printf("Error hashing password: %s", err)
outdata.Error = "Error while saving password."
return outdata.Error, user
}
if err := user.SetPWHash(hash); err != nil {
log.Printf("Error setting pwhash: %s", err)
outdata.Error = "Could not save new password."
} else {
outdata.Success = "Password changed"
}
case "settimezone":
loc, err := time.LoadLocation(req.FormValue("Timezone"))
if err != nil {
outdata.Error = "Unknown Timezone"
return outdata, user
}
if err := user.SetLocation(loc); err != nil {
log.Printf("Error setting location: %s", err)
outdata.Error = "Could not save new timezone."
} else {
outdata.Success = "New timezone saved."
}
}
return outdata, user
}
示例2: pwreset
func pwreset(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) {
if err := req.ParseForm(); err != nil {
return &pwresetTpldata{Error: "Form data corrupted."}, user
}
code := req.FormValue("Code")
_uid := req.FormValue("U")
pw1 := req.FormValue("Password")
pw2 := req.FormValue("PasswordAgain")
if code == "" {
return &pwresetTpldata{Error: "Wrong password reset code"}, user
}
uid, err := db.ParseDBID(_uid)
if err != nil {
return &pwresetTpldata{Error: "Invalid user ID"}, user
}
if user, err = dbcon.UserByID(uid); err != nil {
return &pwresetTpldata{Error: "User not found"}, user
}
if user.ActivationCode() != code {
return &pwresetTpldata{Error: "Wrong activation code"}, user
}
outdata := &pwresetTpldata{UID: _uid, Code: code}
if req.Method != "POST" {
return outdata, user
}
if pw1 == "" {
outdata.Error = "Password must not be empty."
return outdata, user
}
if pw1 != pw2 {
outdata.Error = "Passwords are not identical."
return outdata, user
}
hash, err := bcrypt.GenerateFromPassword([]byte(pw1), bcrypt.DefaultCost)
if err != nil {
log.Printf("Could not has password: %s", err)
outdata.Error = "Could not save password."
return outdata, user
}
if err := user.SetPWHash(hash); err != nil {
log.Printf("Error while hashing password: %s", err)
outdata.Error = "Could not save password."
return outdata, user
}
if err := user.SetActivationCode(""); err != nil {
log.Printf("Error resetting acCode: %s", err)
}
outdata.Success = "Password was changed"
return outdata, user
}