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 *ResetRouter) 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", "Reset:", 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", "Reset:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Get the email code
	var code string
	code_sess := this.GetSession("Authen")
	if code_sess != nil {
		this.DelSession("Authen")
		code = code_sess.(string)
	}
	// Get the username
	var username string
	user_sess := this.GetSession("Username")
	if user_sess != nil {
		this.DelSession("Username")
		username = user_sess.(string)
	}

	// Get user inputs
	authen := strings.TrimSpace(inputs.Get("authen"))
	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"))

	// Validate user inputs
	if models.ValidString(password) && models.ValidString(re_password) && models.ValidString(fundpass) && models.ValidString(re_fundpass) && password == re_password && fundpass == re_fundpass && authen == code {
		// Check if code matches input
		if code != authen {
			models.Log(models.Log_Struct{"info", "Reset:", errors.New("Code not matches.")})
			this.SetSession("Error", ERROR_CODENOTMATCH)
			return
		}

		// Update DB
		if !models.UpdateUserPass(username, models.EncodePass(password), models.EncodePass(fundpass)) {
			models.Log(models.Log_Struct{"info", "Reset:", errors.New("Cant update password of user.")})
			this.SetSession("Error", ERROR_CANTUPDATE)
			return
		}

		this.succ()
		return
	}

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