func (this *UserController) ChangePwd() { old_password := strings.TrimSpace(this.GetString("old_password", "")) new_password := strings.TrimSpace(this.GetString("new_password", "")) repeat_password := strings.TrimSpace(this.GetString("repeat_password", "")) if new_password == "" { this.ServeErrJson("new password can not be empty") return } if new_password != repeat_password { this.ServeErrJson("The password and its confirm are not the same") return } cur := this.Ctx.Input.GetData("CurrentUser").(*User) if cur.Password != utils.Md5Encode(old_password) { this.ServeErrJson("old password is invalid") return } cur.Password = utils.Md5Encode(new_password) err := cur.Update() if err != nil { this.ServeErrJson("password changed fail") return } this.ServeOKJson() }
func (this *AuthController) LoginPost() { username := this.GetString("name", "") password := this.GetString("password", "") if username == "" || password == "" { this.ServeErrJson("name or password is empty!!!") return } u := ReadUserByName(username) if u == nil { this.ServeErrJson("no such user!!!!") return } if u.Password != utils.Md5Encode(password) { this.ServeErrJson("password error") return } this.CreateSession(u.Id, 3600) this.ServeOKJson() }
func (this *AuthController) RegisterPost() { username := strings.TrimSpace(this.GetString("name", "")) password := strings.TrimSpace(this.GetString("password", "")) repeatPassword := strings.TrimSpace(this.GetString("repeat_password", "")) email := strings.TrimSpace(this.GetString("email")) wechat := strings.TrimSpace(this.GetString("wechat")) if username == "" || password == "" { this.ServeErrJson("name or password is empty") return } if password != repeatPassword { this.ServeErrJson("The password and its confirm are not the same") return } if !utils.IsUsernameValid(username) { this.ServeErrJson("The username is invalid") return } if email != "" && !utils.IsEmailVaild(email) { this.ServeErrJson("The email is invalid") return } u := ReadUserByName(username) if u != nil { this.ServeErrJson("name is already existen") return } u = &User{ Username: username, Password: utils.Md5Encode(password), Email: email, Wechat: wechat, } if username == "root" { u.Role = 1 } lastId, err := u.InsertUser() CheckErr(err) this.CreateSession(lastId, 3600*48) this.ServeOKJson() }
func (this *UserController) UpdateUser() { username := strings.TrimSpace(this.GetString("username", "")) email := strings.TrimSpace(this.GetString("email", "")) weChat := strings.TrimSpace(this.GetString("wechat", "")) phone := strings.TrimSpace(this.GetString("phone", "")) pwd := strings.TrimSpace(this.GetString("password", "")) rPwd := strings.TrimSpace(this.GetString("repeat_password", "")) user, _ := GetUserByUsernmae(username) if pwd != "" || rPwd != "" { if pwd != rPwd { this.ServeErrJson("new password and its confirm not same") return } else { user.Password = utils.Md5Encode(pwd) } } if email != "" { if !utils.IsEmailVaild(email) { this.ServeErrJson("email address is invalid") return } } if phone != "" { if !utils.IsPhoneVaild(phone) { this.ServeErrJson("phone number is invalid") return } } user.Email = email user.Phone = phone user.Wechat = weChat err := user.Update() if err != nil { this.ServeErrJson("update user failed") return } this.ServeOKJson() }