Exemple #1
0
func validateNewEmail(ctx *app.Context, email string) (string, error) {
	addr, err := mail.Validate(email, true)
	if err != nil {
		return "", i18n.Errorf("this does not look like a valid email address")
	}
	userType := getUserType(ctx)
	found, err := ctx.Orm().Exists(ctx.Orm().TypeTable(userType), ByEmail(addr))
	if err != nil {
		return "", err
	}
	if found {
		return "", i18n.Errorf("email %q is already in use", addr)
	}
	return addr, nil
}
Exemple #2
0
func (f *PasswordForm) ValidateConfirmPassword() error {
	if f.ConfirmPassword != f.Password {
		return i18n.Errorf("passwords don't match")
	}
	if f.User.IsValid() {
		setUserValue(f.User, "Password", f.Password)
	}
	return nil
}
Exemple #3
0
func validateNewUsername(ctx *app.Context, username string) error {
	userType := getUserType(ctx)
	found, err := ctx.Orm().Exists(ctx.Orm().TypeTable(userType), ByUsername(username))
	if err != nil {
		return err
	}
	if found {
		return i18n.Errorf("username %q is already in use", username)
	}
	return nil
}
Exemple #4
0
func (f *AcceptForm) ValidateAccept() error {
	if !f.Accept {
		return i18n.Errorf("please, accept the Terms of Service and the Privacy Policy")
	}
	return nil
}
Exemple #5
0
func forgotHandler(ctx *app.Context) {
	d := data(ctx)
	if !d.allowDirectSignIn() {
		ctx.NotFound("")
		return
	}
	var user User
	var isEmail bool
	var sent bool
	var fields struct {
		Username         string `form:",singleline,label=Username or Email"`
		ValidateUsername func(*app.Context) error
	}
	fields.ValidateUsername = func(c *app.Context) error {
		username := Normalize(fields.Username)
		isEmail = strings.Contains(username, "@")
		var field string
		if isEmail {
			field = "User.NormalizedEmail"
		} else {
			field = "User.NormalizedUsername"
		}
		userVal, userIface := newEmptyUser(ctx)
		ok := c.Orm().MustOne(orm.Eq(field, username), userIface)
		if !ok {
			if isEmail {
				return i18n.Errorf("address %q does not belong to any registered user", username)
			}
			return i18n.Errorf("username %q does not belong to any registered user", username)
		}
		user = getUserValue(userVal, "User").(User)
		if user.Email == "" {
			return i18n.Errorf("username %q does not have any registered emails", username)
		}
		return nil
	}
	f := form.New(ctx, &fields)
	if f.Submitted() && f.IsValid() {
		se, err := ctx.App().EncryptSigner(Salt)
		if err != nil {
			panic(err)
		}
		values := make(url.Values)
		values.Add("u", strconv.FormatInt(user.Id(), 36))
		values.Add("t", strconv.FormatInt(time.Now().Unix(), 36))
		values.Add("n", stringutil.Random(64))
		payload := values.Encode()
		p, err := se.EncryptSign([]byte(payload))
		if err != nil {
			panic(err)
		}
		abs := ctx.URL()
		reset := fmt.Sprintf("%s://%s%s?p=%s", abs.Scheme, abs.Host, ctx.MustReverse(ResetHandlerName), p)
		data := map[string]interface{}{
			"URL": reset,
		}
		from := mail.DefaultFrom()
		if from == "" {
			from = fmt.Sprintf("no-reply@%s", abs.Host)
		}
		msg := &mail.Message{
			To:      user.Email,
			From:    from,
			Subject: fmt.Sprintf(ctx.T("Reset your %s password"), d.opts.SiteName),
		}
		ctx.MustSendMail("reset_password.txt", data, msg)
		sent = true
	}
	data := map[string]interface{}{
		"ForgotForm": f,
		"IsEmail":    isEmail,
		"Sent":       sent,
		"User":       user,
	}
	ctx.MustExecute(ForgotTemplateName, data)
}
Exemple #6
0
func parse(val string, v reflect.Value) error {
	var err error
	p := v
	// Get Pointer methods
	if p.IsValid() && p.Kind() != reflect.Ptr && p.CanAddr() {
		p = p.Addr()
	}
	if p.Type().Implements(parserInterface) {
		err = p.Interface().(Parser).Parse(val)
		if err == nil {
			return nil
		}
		// Continue with the function, just in case we can
		// still parse the value (e.g. an enum type which defines
		// a Parse() function for accepting raw strings but val
		// is actually a numeric value).
	}
	// If val is empty, set the value to zero
	if val == "" {
		v.Set(reflect.Zero(v.Type()))
		return nil
	}
	switch v.Type().Kind() {
	case reflect.Bool:
		res := false
		switch strings.ToLower(val) {
		case "", "f", "false", "0", "off":
		case "t", "true", "1", "on":
			res = true
		default:
			return i18n.Errorf("invalid boolean value %q", val)
		}
		v.SetBool(res)
		return nil
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		res, err := strconv.ParseInt(val, 0, 64)
		if err != nil {
			return err
		}
		if v.OverflowInt(res) {
			if res > 0 {
				res = int64(math.Pow(2, float64(8*v.Type().Size()-1)) - 1)
			} else {
				res = -int64(math.Pow(2, float64(8*v.Type().Size()-1)))
			}
		}
		v.SetInt(res)
		return nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		res, err := strconv.ParseUint(val, 0, 64)
		if err != nil {
			return err
		}
		if v.OverflowUint(res) {
			res = uint64(math.Pow(2, float64(8*v.Type().Size())) - 1)
		}
		v.SetUint(res)
		return nil
	case reflect.Float32, reflect.Float64:
		res, err := strconv.ParseFloat(val, 64)
		if err != nil {
			return err
		}
		v.SetFloat(res)
		return nil
	case reflect.String:
		v.SetString(val)
		return nil
	default:
		if err == nil {
			err = fmt.Errorf("Invalid argument type passed to Parse(): %s. Please, see the documentation for a list of the supported types.",
				v.Type())
		}
	}
	return err
}