func (form *UserAdminForm) SetToUser(user *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 }
// register create user func RegisterUser(user *User, form RegisterForm) error { // use random salt encode password salt := GetUserSalt() pwd := utils.EncodePassword(form.Password, salt) user.UserName = strings.ToLower(form.UserName) user.Email = strings.ToLower(form.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(form.Email) // Use username as default nickname. user.NickName = user.UserName return user.Insert() }
func (form *ProfileForm) SaveUserProfile(user *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 }