func (this *SendEmailToGetBackPasswordController) Get() { username := this.GetString("username") if "" == username { this.Data["json"] = map[string]interface{}{"result": false, "msg": "username could not be empty", "refer": "/"} this.ServeJson() return } time := time.Now() code := com.Md5(com.RandString(20) + time.String()) err := AddVerify(username, code, time) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "create varify failed", "refer": "/"} this.ServeJson() } else { host := beego.AppConfig.String("host") subject := "blog system get your password back" body := `click the following link to get your password back <font color="red"><a href="` + host + `/password/reset/` + code + `">` + host + `/password/reset/` + code + `</a></font>` currentUser, _ := FindUser(username) email := currentUser.Email err := utils.SendMail(email, subject, body) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "send mail failed", "refer": "/"} this.ServeJson() } else { this.Data["json"] = map[string]interface{}{"result": true, "msg": "create varify success", "refer": "/"} this.ServeJson() } } }
// 添加用户 func AddUser(username string, password string) (int64, error) { o := orm.NewOrm() o.Using("default") user := new(Users) user.Username = username user.Salt = com.RandString(10) user.Password = com.Md5(password + user.Salt) return o.Insert(user) }
// user registor // return: int64 id // error if failed func (this *User) Register(userName string, password string, email string, nickName string) (int64, error) { if len(userName) <= 0 || len(password) < 5 || len(email) <= 0 { return 0, errors.New("check you form, please.") } if len(nickName) <= 0 { nickName = userName + com.RandString(5) // gen the default nickname } o := orm.NewOrm() var user User user.Username = userName user.Salt = com.RandString(7) user.Password = com.Md5(password + user.Salt) user.Email = email user.Level = "user" user.Nickname = nickName return o.Insert(&user) }
// 设置密码 func SetPassword(username string, password string) error { o := orm.NewOrm() o.Using("default") salt := com.RandString(10) num, err := o.QueryTable("users").Filter("username", username).Update(orm.Params{ "salt": salt, "password": com.Md5(password + salt), }) if 0 == num { return errors.New("item not exist") } return err }
// 修改密码 func ChangePassword(username string, oldPassword string, newPassword string) error { o := orm.NewOrm() o.Using("default") salt := com.RandString(10) user := Users{Username: username} err := o.Read(&user, "username") if nil != err { return err } else { if user.Password == com.Md5(oldPassword+user.Salt) { _, err := o.QueryTable("users").Filter("username", username).Update(orm.Params{ "salt": salt, "password": com.Md5(newPassword + salt), }) return err } else { return errors.New("verification failed") } } }