示例#1
0
func (this *UserController) EditUserInfo() {
	currentUser := this.Ctx.Input.GetData("CurrentUser").(*User)
	email := strings.TrimSpace(this.GetString("email", ""))
	weChat := strings.TrimSpace(this.GetString("wechat", ""))
	phone := strings.TrimSpace(this.GetString("phone", ""))
	if email != "" && !utils.IsEmailVaild(email) {
		this.ServeErrJson("email is invalid")
		return
	}
	if utils.HasDangerousCharacters(weChat) {
		this.ServeErrJson("wechat is invalid")
		return
	}
	if phone != "" && !utils.IsPhoneVaild(phone) {
		this.ServeErrJson("phone is invalid")
		return
	}
	currentUser.Email = email
	currentUser.Wechat = weChat
	currentUser.Phone = phone
	err := currentUser.Update()
	if err != nil {
		this.ServeErrJson("update failed")
		return
	}
	this.ServeOKJson()
}
示例#2
0
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()
}
示例#3
0
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()
}