示例#1
0
文件: public.go 项目: kizbitz/webapp
func CronNotifyEmailExpireGET(w http.ResponseWriter, r *http.Request) {
	result, err := model.EmailsWithVerificationIn30Days()

	if err != nil {
		log.Println(err)
		Error500(w, r)
	}

	c := view.ReadConfig()

	for _, v := range result {
		//log.Println(v.First_name, " expires on ", v.Expiring, v.Expired, v.Updated_at)

		user_id := int64(v.Id)
		email := v.Email
		first_name := v.First_name

		if v.Expiring {
			// Create the email verification string
			md := random.Generate(32)

			// Add the hash to the database
			err = model.UserEmailVerificationCreate(user_id, md)
			if err != nil {
				log.Println(err)
			}

			// Email the hash to the user
			err = emailer.SendEmail(email, "Email Verification Required for Verified.ninja", "Hi "+first_name+",\n\nTo keep your account active, please verify your email address by clicking on this link: "+c.BaseURI+"emailverification/"+md+"\n\nYour account will expire in 5 days if you don't verify your email.")
			if err != nil {
				log.Println(err)
			}
		} else if v.Expired {
			err = model.UserReverify(user_id)
			if err != nil {
				log.Println(err)
			}

			user_info, err := model.EmailVerificationTokenByUserId(uint64(user_id))
			if err != nil {
				log.Println(err)
			}

			md := user_info.Token

			// Email the hash to the user
			err = emailer.SendEmail(email, "Account Locked on Verified.ninja", "Hi "+first_name+",\n\nIt's been over 30 days since you verified your email. To unlock your account, please verify your email address by clicking on this link: "+c.BaseURI+"emailverification/"+md)
			if err != nil {
				log.Println(err)
			}
		}
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write([]byte(`{"Done": true}`))
}
示例#2
0
文件: user.go 项目: kizbitz/webapp
func UserEmailPOST(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	user_id := int64(sess.Values["id"].(uint32))
	if !isVerifiedEmail(r, user_id) {
		sess.AddFlash(view.Flash{"You can't change you email again until you verify your current email.", view.FlashError})
		sess.Save(r, w)
		http.Redirect(w, r, "/", http.StatusFound)
	}

	// Validate with required fields
	if validate, missingField := view.Validate(r, []string{"email"}); !validate {
		sess.AddFlash(view.Flash{"Field missing: " + missingField, view.FlashError})
		sess.Save(r, w)
		UserEmailGET(w, r)
		return
	}

	// Validate with Google reCAPTCHA
	if !recaptcha.Verified(r) {
		sess.AddFlash(view.Flash{"reCAPTCHA invalid!", view.FlashError})
		sess.Save(r, w)
		UserEmailGET(w, r)
		return
	}

	// Form values
	email := r.FormValue("email")
	emailOld := sess.Values["email"]

	if email == emailOld {
		sess.AddFlash(view.Flash{"New email cannot be the same as the old email.", view.FlashError})
		sess.Save(r, w)
		UserEmailGET(w, r)
		return
	}

	// Get database result
	err := model.UserEmailUpdate(user_id, email)

	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			sess.AddFlash(view.Flash{"That email already exists in the database. Please use a different one.", view.FlashError})
		} else {
			// Display error message
			log.Println(err)
			sess.AddFlash(view.Flash{"There was an error. Please try again later.", view.FlashError})
		}

		sess.Save(r, w)
		UserEmailGET(w, r)
		return
	}

	first_name := fmt.Sprintf("%v", sess.Values["first_name"])

	// Create the email verification string
	md := random.Generate(32)

	// Add the hash to the database
	err = model.UserEmailVerificationCreate(user_id, md)
	if err != nil {
		log.Println(err)
	}
	err = model.UserReverify(user_id)
	if err != nil {
		log.Println(err)
	}

	c := view.ReadConfig()

	// Email the hash to the user
	err = emailer.SendEmail(email, "Email Verification for Verified.ninja", "Hi "+first_name+",\n\nTo verify your email address ("+email+"), please click here: "+c.BaseURI+"emailverification/"+md)
	if err != nil {
		log.Println(err)
	}

	// Login successfully
	sess.AddFlash(view.Flash{"Email updated! You must verify your email before you can login again.", view.FlashSuccess})
	sess.Values["email"] = email
	sess.Save(r, w)
	http.Redirect(w, r, "/", http.StatusFound)
}