コード例 #1
0
ファイル: captcha.go プロジェクト: r0lodex/dots-master-server
func captchaGenHandler(response http.ResponseWriter, request *http.Request) {
	context := appengine.NewContext(request)
	garbageOldCaptchas(context)
	captcha := model.Captcha{
		Value:       strconv.Itoa(rand.Intn(999999)),
		GeneratedAt: time.Now(),
		Viewed:      false,
	}
	key, err := datastore.Put(context, datastore.NewIncompleteKey(context, model.DB_KIND_CAPTCHA, nil), &captcha)
	if err != nil {
		log.Fatal(err)
	}
	utils.WriteJsonResponse(response, fmt.Sprintf("{\"id\":\"%v\"}", key.Encode()))
}
コード例 #2
0
func registerHandler(response http.ResponseWriter, request *http.Request) {
	if !(utils.CheckValue(request, KEY_LOGIN, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_PASSWORD, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_ALIAS, utils.Rule{Min: 3, Max: 100}) &&
		utils.CheckValue(request, KEY_EMAIL, utils.Rule{Min: 3, Max: 500, Regex: "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$"}) &&
		utils.CheckValue(request, KEY_CAPTCHA_ID, utils.Rule{Min: 1, Max: 100}) &&
		utils.CheckValue(request, KEY_CAPTCHA_VAL, utils.Rule{Min: 1, Max: 100})) {
		http.Error(response, utils.GetLastValidationError().Error(), http.StatusBadRequest)
		return
	}

	context := appengine.NewContext(request)

	query := datastore.NewQuery(model.DB_KIND_USER).
		Filter("Login ="******"Email =", request.FormValue(KEY_EMAIL)).KeysOnly()
	count, err = query.Count(context)
	if count != 0 {
		http.Error(response, ERR_EMAIL_CONFLICT, http.StatusConflict)
		return
	}

	passed, err := CheckCaptcha(context, request.FormValue(KEY_CAPTCHA_ID), request.FormValue(KEY_CAPTCHA_VAL))
	if !passed {
		if err != nil {
			log.Println(err)
		}
		http.Error(response, ERR_CAPTCHA_NOT_PASS, http.StatusNotAcceptable)
		return
	}

	user := model.User{
		Login:      request.FormValue(KEY_LOGIN),
		Password:   request.FormValue(KEY_PASSWORD),
		Alias:      request.FormValue(KEY_ALIAS),
		Email:      request.FormValue(KEY_EMAIL),
		Verified:   false,
		Registered: time.Now(),
	}
	key, err := datastore.Put(context, datastore.NewIncompleteKey(context, model.DB_KIND_USER, nil), &user)
	if err != nil {
		log.Fatal(err)
		return
	}

	url := "http://" + appengine.DefaultVersionHostname(context) + HANDLER_PATH_CONFIRM + "?" + KEY_USER + "=" + key.Encode()
	msg := &mail.Message{
		Sender:   base.PHLOX_CONNECT_EMAIL,
		To:       []string{request.FormValue(KEY_EMAIL)},
		Subject:  CONFIRM_REGISTRATION,
		HTMLBody: fmt.Sprintf(CONFIRM_MAIL_BODY, url, url),
	}
	err = mail.Send(context, msg)
	if err != nil {
		log.Println(err)
		http.Error(response, fmt.Sprintf(ERR_CANT_SEND_EMAIL, err), http.StatusInternalServerError)
		return
	}

	utils.WriteJsonResponse(response, utils.JSON_RESULT_SUCCESS)
}
コード例 #3
0
ファイル: user.go プロジェクト: r0lodex/dots-master-server
func loginHandler(response http.ResponseWriter, request *http.Request) {
	utils.WriteJsonResponse(response, "{\"test\":\"pass\"}")
}