// set a new password to user func (this *UserService) SaveNewPassword(u *user.User, password string) error { salt := GetUserSalt() u.Password = fmt.Sprintf("%s$%s", salt, utils.EncodePassword(password, salt)) u.PasswordSalt = salt _, err := orm.NewOrm().Update(u, "Password", "PasswordSalt", "Updated") return err }
// Active implemented check Email actice code. func (this *RegisterController) Active() { this.TplName = "auth/active.html" // no need active if this.CheckActiveRedirect(false) { return } code := this.GetString(":code") var user user.User if this.UserService.VerifyUserActiveCode(&user, code) { user.Active = true user.PasswordSalt = userServ.GetUserSalt() if err := this.UserService.Update(&user, "Active", "PasswordSalt", "Updated"); err != nil { beego.Error("Active: user Update ", err) } if this.IsLogin { this.User = user } this.Redirect("/active/success", 302) } else { this.Data["Success"] = false } }
// register a user func (this *UserService) RegisterUser(u *user.User, username, email, password string, userType *user.UserType, role *user.Role) error { u.UserType = userType // use random salt encode password salt := GetUserSalt() pwd := utils.EncodePassword(password, salt) u.Username = strings.ToLower(username) u.Email = strings.ToLower(email) // save salt and encode password, use $ as split char u.Password = fmt.Sprintf("%s$%s", salt, pwd) u.PasswordSalt = salt var err error tr := orm.NewOrm() tr.Begin() if err = this.InsertWithScope(tr, u); err == nil { roleService := RoleService{} err = roleService.InsertUsersWithScope(tr, role, u) } if err == nil { tr.Commit() } else { tr.Rollback() } return err }
// Reset implemented user password reset. func (this *ForgotController) ResetPost() { this.TplName = "auth/reset.html" code := this.GetString(":code") this.Data["Code"] = code var user user.User if this.UserService.VerifyUserResetPwdCode(&user, code) { this.Data["Success"] = true form := ResetPwdModel{} if this.ValidFormSets(&form) == false { return } user.Active = true user.PasswordSalt = userServ.GetUserSalt() if err := this.UserService.SaveNewPassword(&user, form.Password); err != nil { beego.Error("ResetPost Save New Password: "******"/login", 302, "ResetSuccess") } else { this.Data["Success"] = false } }
func (this UserService) InsertWithScope(tr orm.Ormer, u *user.User) error { u.PasswordSalt = GetUserSalt() if !utils.IsEmail(u.Email) { return errors.New("invalid email") } beego.Info(setting.SystemAdminEmails) if strings.Index(setting.SystemAdminEmails, u.Email) >= 0 { u.IsSystemAccount = true beego.Info(setting.SystemAdminEmails) } if _, err := tr.Insert(u); err != nil { return err } return nil }