Example #1
0
func (this *RegisterRouter) Post() {
	// Get user inputs
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Register:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Register:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate user inputs
	username := strings.TrimSpace(inputs.Get("username"))
	password := strings.TrimSpace(inputs.Get("password"))
	re_password := strings.TrimSpace(inputs.Get("re-password"))
	fundpass := strings.TrimSpace(inputs.Get("fundpassword"))
	re_fundpass := strings.TrimSpace(inputs.Get("re-fundpassword"))
	email := strings.TrimSpace(inputs.Get("email"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))

	// Check referral
	refer := strings.TrimSpace(inputs.Get("refer"))
	if refer != "" && models.ValidString(refer) {
		if !models.UserExist(refer) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Referral user not exist.")})
			this.SetSession("Error", ERROR_REFERNOTEXIST)
			this.fail()
			return
		}
	}

	// Validate user inputs, set sessions and redirect
	if models.ValidString(username) && models.ValidString(password) && models.ValidString(re_password) && models.ValidString(fundpass) && models.ValidString(re_fundpass) && password == re_password && fundpass == re_fundpass && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("User already exist.")})
			this.SetSession("Error", ERROR_USEREXIST)
			this.fail()
			return
		}

		// Check if email exist
		if models.EmailExist(email) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Email already exist.")})
			this.SetSession("Error", ERROR_EMAILEXIST)
			this.fail()
			return
		}

		// Generate new address for new user
		address, err := models.NewAddress(username)
		if err != nil {
			models.Log(models.Log_Struct{"info", "Register:", err})
			this.fail()
			this.SetSession("Error", ERROR_ADDRESS)
			return
		}

		// Insert new user to DB
		ok := models.NewUser(models.User{Username: username, Password: models.EncodePass(password), Fundpassword: models.EncodePass(fundpass), Email: email, Btcaddress: address, Birth: dateofbirth, Referral: refer})
		if !ok {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Unable to insert user, need to delete from wallet.")})
			err = models.Archive(address)
			if err != nil {
				models.Log(models.Log_Struct{"warn", "Register:", errors.New("Unable to archive.")})
			} else {
				models.Log(models.Log_Struct{"info", "Register:", errors.New("Succeed archiving address.")})
			}
			this.fail()
			this.SetSession("Error", ERROR_DB)
			return
		}

		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Register:", errors.New("Failed, invalid data")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Example #2
0
func (this *ForgetRouter) Post() {
	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Forget:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Forget:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate inputs
	username := strings.TrimSpace(inputs.Get("username"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))
	email := strings.TrimSpace(inputs.Get("email"))

	if models.ValidString(username) && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if !models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("User not exist.")})
			this.SetSession("Error", ERROR_USERNOTEXIST)
			this.fail()
			return
		}
		// Check if birth matches
		if !models.BirthMatch(username, dateofbirth) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Birth not match.")})
			this.SetSession("Error", ERROR_BIRTHNOTMATCH)
			this.fail()
			return
		}
		// Check if email mathces
		if !models.EmailMatch(username, email) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Email not match.")})
			this.SetSession("Error", ERROR_EMAILNOTMATCH)
			this.fail()
			return
		}

		// Send Email to authenticate
		authen := models.RandString(8)
		if !models.SendEmail(email, "重设密码", username+":  请复制验证码,以完成重设密码操作---->", authen) {
			models.Log(models.Log_Struct{"warn", "Forget:", errors.New("Cant send email to authen password reset.")})
			this.SetSession("Error", ERROR_EMAILNOTSENT)
			this.fail()
			return
		}

		this.SetSession("Username", username)
		this.SetSession("Authen", authen)
		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Forget:", errors.New("Failed, invalid data.")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}