// register create user func RegisterUser(user *models.User, username, email, password string, locale i18n.Locale) error { // use random salt encode password salt := models.GetUserSalt() pwd := utils.EncodePassword(password, salt) user.UserName = strings.ToLower(username) user.Email = strings.ToLower(email) // save salt and encode password, use $ as split char user.Password = fmt.Sprintf("%s$%s", salt, pwd) // save md5 email value for gravatar user.GrEmail = utils.EncodeMd5(user.Email) // Use username as default nickname. user.NickName = user.UserName //set default language if locale.Lang == "en-US" { user.Lang = setting.LangEnUS } else { user.Lang = setting.LangZhCN } //set default avatar user.AvatarType = setting.AvatarTypeGravatar return user.Insert() }
func LoginUserFromRememberCookie(user *models.User, ctx *context.Context) (success bool) { userName := ctx.GetCookie(setting.CookieUserName) if len(userName) == 0 { return false } defer func() { if !success { DeleteRememberCookie(ctx) } }() user.UserName = userName if err := user.Read("UserName"); err != nil { return false } secret := utils.EncodeMd5(user.Rands + user.Password) value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName) if value != userName { return false } LoginUser(user, ctx, true) return true }
func (form *UserAdminForm) SetToUser(user *models.User) { // set md5 value if the value is an email if strings.IndexRune(form.GrEmail, '@') != -1 { form.GrEmail = utils.EncodeMd5(form.GrEmail) } utils.SetFormValues(form, user) }
// compare raw password and encoded password func VerifyPassword(rawPwd, encodedPwd string) bool { // for discuz accounts if len(encodedPwd) == 39 { salt := encodedPwd[:6] encoded := encodedPwd[7:] return encoded == utils.EncodeMd5(utils.EncodeMd5(rawPwd)+salt) } // split var salt, encoded string if len(encodedPwd) > 11 { salt = encodedPwd[:10] encoded = encodedPwd[11:] } return utils.EncodePassword(rawPwd, salt) == encoded }
func (form *ProfileForm) SaveUserProfile(user *models.User) error { // set md5 value if the value is an email if strings.IndexRune(form.GrEmail, '@') != -1 { form.GrEmail = utils.EncodeMd5(form.GrEmail) } changes := utils.FormChanges(user, form) if len(changes) > 0 { // if email changed then need re-active if user.Email != form.Email { user.IsActive = false changes = append(changes, "IsActive") } utils.SetFormValues(form, user) return user.Update(changes...) } return nil }
func WriteRememberCookie(user *models.User, ctx *context.Context) { secret := utils.EncodeMd5(user.Rands + user.Password) days := 86400 * setting.LoginRememberDays ctx.SetCookie(setting.CookieUserName, user.UserName, days) ctx.SetSecureCookie(secret, setting.CookieRememberName, user.UserName, days) }