Example #1
0
func (form *ResetPwdForm) Valid(v *validation.Validation) {
	// Check if passwords of two times are same.
	if form.Password != form.PasswordRe {
		v.SetError("PasswordRe", "Passwords of two times are not matched.")
		return
	}
}
Example #2
0
func (form *PostAdminForm) Valid(v *validation.Validation) {
	user := models.User{Id: form.User}
	if user.Read() != nil {
		v.SetError("User", "admin.not_found_by_id")
	}

	user.Id = form.LastReply
	if user.Read() != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	user.Id = form.LastAuthor
	if user.Read() != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	topic := models.Topic{Id: form.Topic}
	if topic.Read() != nil {
		v.SetError("Topic", "admin.not_found_by_id")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Not Found")
	}
}
Example #3
0
func (u *User) Check(v *validation.Validation) {
	if u.Name == "toukii" && u.Passwd == "1234" {
		v.Clear()
	} else {
		v.SetError("login", "user is abnormal.")
	}
}
Example #4
0
func (form *LoginForm) Valid(v *validation.Validation) {
	valid := validation.Validation{}
	valid.Email(form.Email, "email")
	if valid.HasErrors() {
		v.SetError("Email", form.Locale.Tr("valid.invalid_email"))
	}
}
Example #5
0
// Valid is the custom validation. It runs only when all the standard validations pass
func (o *Object) Valid(v *validation.Validation) {
	if o.Score == 0 {
		// We have no way to set error code here
		// TODO: fix it in Beego
		v.SetError("Score", "Can not be empty")
	}
}
Example #6
0
// Valid ...
func (r *RepTarget) Valid(v *validation.Validation) {
	if len(r.Name) == 0 {
		v.SetError("name", "can not be empty")
	}

	if len(r.Name) > 64 {
		v.SetError("name", "max length is 64")
	}

	if len(r.URL) == 0 {
		v.SetError("endpoint", "can not be empty")
	}

	r.URL = utils.FormatEndpoint(r.URL)

	if len(r.URL) > 64 {
		v.SetError("endpoint", "max length is 64")
	}

	// password is encoded using base64, the length of this field
	// in DB is 64, so the max length in request is 48
	if len(r.Password) > 48 {
		v.SetError("password", "max length is 48")
	}
}
Example #7
0
func (this *UserController) processUserPasswordForm(user *models.User) {
	valid := validation.Validation{}
	userPasswordForm := UserPasswordForm{}
	if err := this.ParseForm(&userPasswordForm); err != nil {
		beego.Error(err)
	}
	_, err := valid.Valid(userPasswordForm)
	if err != nil {
		beego.Error(err)
		this.Abort("400")
	}
	if !user.VerifyPassword(userPasswordForm.CurrentPassword) {
		valid.SetError("CurrentPassword", "当前密码错误")
	}
	if len(valid.Errors) > 0 {
		this.Data["UserPasswordFormValidErrors"] = valid.Errors
		beego.Trace(fmt.Sprint(valid.Errors))
	} else {
		user.SetPassword(userPasswordForm.Password)
		if err := user.Update(); err != nil {
			this.Abort("500")
		}
		this.FlashWrite("notice", "密码已更新!")
		this.Redirect(this.Ctx.Request.RequestURI, 302)
	}
}
Example #8
0
func (model *ResetPwdModel) Valid(v *validation.Validation) {
	// Check if passwords of two times are same.
	if model.Password != model.PasswordRe {
		v.SetError("PasswordRe", "auth.repassword_not_match")
		return
	}
}
Example #9
0
func (form *CommentAdminForm) Valid(v *validation.Validation) {
	var err error
	if _, err = models.GetUserById(int64(form.User)); err != nil {
		v.SetError("User", "admin.not_found_by_id")
	}

	if _, err = models.GetPostById(int64(form.Post)); err != nil {
		v.SetError("Post", "admin.not_found_by_id")
	}
}
Example #10
0
func (form *CategoryAdminForm) Valid(v *validation.Validation) {
	category := models.Category{Name: form.Name}
	if models.GetByExample(&category); category.Id != int64(form.Id) {
		v.SetError("Name", "admin.field_need_unique")
	}

	category = models.Category{Slug: form.Slug}
	if models.GetByExample(&category); category.Id != int64(form.Id) {
		v.SetError("Slug", "admin.field_need_unique")
	}
}
Example #11
0
func (form *TopicAdminForm) Valid(v *validation.Validation) {
	topic := models.Topic{Name: form.Name}
	if models.GetByExample(&topic); topic.Id != int64(form.Id) {
		v.SetError("Name", "admin.field_need_unique")
	}

	topic = models.Topic{Slug: form.Slug}
	if models.GetByExample(&topic); topic.Id != int64(form.Id) {
		v.SetError("Slug", "admin.field_need_unique")
	}
}
Example #12
0
func (form *CategoryAdminForm) Valid(v *validation.Validation) {
	qs := models.Categories()

	if models.CheckIsExist(qs, "Name", form.Name, form.Id) {
		v.SetError("Name", "admin.field_need_unique")
	}

	if models.CheckIsExist(qs, "Slug", form.Slug, form.Id) {
		v.SetError("Slug", "admin.field_need_unique")
	}
}
Example #13
0
func (form *CommentAdminForm) Valid(v *validation.Validation) {
	user := models.User{Id: form.User}
	if user.Read() != nil {
		v.SetError("User", "admin.not_found_by_id")
	}

	post := models.Post{Id: form.Post}
	if post.Read() != nil {
		v.SetError("Post", "admin.not_found_by_id")
	}
}
Example #14
0
func (form *PasswordForm) Valid(v *validation.Validation) {
	// Check if passwords of two times are same.
	if form.Password != form.PasswordRe {
		v.SetError("PasswordRe", "auth.repassword_not_match")
		return
	}

	if VerifyPassword(form.PasswordOld, form.User.Password) == false {
		v.SetError("PasswordOld", "auth.old_password_wrong")
	}
}
Example #15
0
func (form *RegisterForm) Valid(v *validation.Validation) {

	// Check if passwords of two times are same.
	if form.Password != form.PasswordRe {
		v.SetError("PasswordRe", "auth.repassword_not_match")
		return
	}

	e1, _ := CanRegistered(form.Email)

	if !e1 {
		v.SetError("Email", "auth.email_already_taken")
	}
}
Example #16
0
func (form *UserAdminForm) Valid(v *validation.Validation) {
	qs := models.Users()

	if models.CheckIsExist(qs, "UserName", form.UserName, form.Id) {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if models.CheckIsExist(qs, "Email", form.Email, form.Id) {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	if len(form.LangAdds) > 0 {
		adds := make(models.SliceStringField, 0, len(form.LangAdds))
		for _, l := range form.LangAdds {
			if d, err := utils.StrTo(l).Int(); err == nil {
				if form.Lang == d {
					continue
				}
				if len(i18n.GetLangByIndex(form.Lang)) == 0 {
					v.SetError("Lang", "Can not be empty")
					return
				}
				adds = append(adds, l)
			}
		}
		form.LangAdds = adds
	}
}
Example #17
0
func (form *PostForm) Valid(v *validation.Validation) {
	valid := false
	for _, topic := range form.Topics {
		if topic.Id == form.Topic {
			valid = true
		}
	}

	if !valid {
		v.SetError("Topic", "error")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "error")
	}
}
Example #18
0
//输入email,发送重设密码邮件
func (this *AuthController) ForgetPassword() {
	this.Data["PageTitle"] = fmt.Sprintf("忘记密码 | %s", setting.AppName)
	this.Layout = "layout.html"
	this.TplNames = "forget-password.html"
	valid := validation.Validation{}
	form := ForgetPasswordForm{}
	if this.Ctx.Request.Method == "POST" {
		if err := this.ParseForm(&form); err != nil {
			beego.Error(err)
		}
		_, err := valid.Valid(form)
		if err != nil {
			beego.Error(err)
		}
		user := models.User{Email: form.Email}
		if err := user.Read("Email"); err != nil {
			beego.Trace(user)
			beego.Trace(form)
			valid.SetError("Email", "此电子邮件并未注册")
		}
		beego.Trace(valid.Errors)
		if len(valid.Errors) == 0 {
			//发送忘记密码邮件
			code, err := user.GenerateActivateCode()
			if err != nil {
				this.Abort("500")
			}
			sub := sendcloud.NewSubstitution()
			sub.AddTo(user.Email)
			sub.AddSub("%appname%", setting.AppName)
			sub.AddSub("%name%", user.Username)
			sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.ResetPassword", ":code", code))
			if err := setting.Sendcloud.SendTemplate("gotalk_password", setting.AppName+"忘记密码", setting.From, setting.FromName, sub); err != nil {
				beego.Error(err)
			}
			this.FlashWrite("notice", fmt.Sprintf("重设密码的方法已经发到%s。请查收!", user.Email))
			this.Redirect("/", 302)
		} else {
			this.Data["HasError"] = true
			this.Data["errors"] = valid.Errors
		}
	}
}
Example #19
0
func (this *UserController) processUploadAvatar(user *models.User) {
	valid := validation.Validation{}
	avatarFile, header, err := this.GetFile("Avatar")
	if err != nil {
		this.Abort("400")
	}
	err = user.ValidateAndSetAvatar(avatarFile, header.Filename)
	defer avatarFile.Close()
	if err != nil {
		valid.SetError("Avatar", err.Error())
		this.Data["UserAvatarFormValidErrors"] = valid.Errors
	} else {
		if err := user.Update("Avatar"); err != nil {
			this.Abort("500")
		}
		this.FlashWrite("notice", "头像已更新!")
		this.Redirect(this.Ctx.Request.RequestURI, 302)
	}
}
Example #20
0
func (model *UserAdminModel) Valid(v *validation.Validation) {
	qs := userServ.UserService{}.Queryable()
	if services.CheckIsExist(qs, "user_name", model.Username, model.Id) {
		v.SetError("Username", "auth.username_already_taken")
	}

	if services.CheckIsExist(qs, "email", model.Email, model.Id) {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(model.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	//if len(model.LangAdds) > 0 {
	//	adds := make(models.SliceStringField, 0, len(model.LangAdds))
	//	for _, l := range model.LangAdds {
	//		if d, err := utils.StrTo(l).Int(); err == nil {
	//			if model.Lang == d {
	//				continue
	//			}
	//			if len(i18n.GetLangByIndex(model.Lang)) == 0 {
	//				v.SetError("Lang", "Can not be empty")
	//				return
	//			}
	//			adds = append(adds, l)
	//		}
	//	}
	//	model.LangAdds = adds
	//}
}
Example #21
0
func (form *ProfileForm) Valid(v *validation.Validation) {
	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	//if len(model.LangAdds) > 0 {
	//	adds := make(models.SliceStringField, 0, len(model.LangAdds))
	//	for _, l := range model.LangAdds {
	//		if d, err := utils.StrTo(l).Int(); err == nil {
	//			if model.Lang == d {
	//				continue
	//			}
	//			if len(i18n.GetLangByIndex(model.Lang)) == 0 {
	//				v.SetError("Lang", "Can not be empty")
	//				return
	//			}
	//			adds = append(adds, l)
	//		}
	//	}
	//	model.LangAdds = adds
	//}
}
Example #22
0
func (this *AuthController) DoRegister() {
	this.CheckRequestFrequency(3, 15, 30)
	this.Layout = "layout.html"
	this.TplNames = "register.html"
	valid := validation.Validation{}
	form := RegisterForm{}
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	b, err := valid.Valid(form)
	if err != nil {
		beego.Error(err)
	}
	if !b {
		this.registerPageWithErrors(form, valid.Errors)
		return
	}
	//验证用户名
	user := models.User{Username: form.Username}
	if err := user.ValidUsername(); err != nil {
		valid.SetError("username", err.Error())
		this.registerPageWithErrors(form, valid.Errors)
		return
	} else {
		if user.Read("Username") == nil {
			valid.SetError("username", fmt.Sprintf("%s已被使用,请使用其他用户名!", form.Username))
			this.registerPageWithErrors(form, valid.Errors)
			return
		}
	}
	//验证email未被注册
	user.Email = form.Email
	if user.Read("Email") == nil {
		valid.SetError("email", "已被使用,请直接使用此电邮登录")
		this.registerPageWithErrors(form, valid.Errors)
		return
	}
	//通过所有验证
	actCode, _ := user.GenerateActivateCode()
	user.SetPassword(form.Password)
	if err := user.Insert(); err != nil {
		beego.Error(err)
		this.Abort("500")
		return
	}
	sub := sendcloud.NewSubstitution()
	sub.AddTo(user.Email)
	sub.AddSub("%appname%", setting.AppName)
	sub.AddSub("%name%", user.Username)
	sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.Activate", ":code", actCode))
	if err := setting.Sendcloud.SendTemplate("gotalk_register", setting.AppName+"欢迎你", setting.From, setting.FromName, sub); err != nil {
		beego.Error(err)
	}
	this.FlashWrite("notice", fmt.Sprintf("注册成功!欢迎你, %s。建议你再花点时间上传头像、验证电子邮件!", user.Username))
	this.LogUserIn(&user, false)
	userEditUrl := beego.UrlFor("UserController.Edit", ":username", user.Username)
	this.Redirect(userEditUrl, 302)
	return
}
Example #23
0
func (form *ProfileForm) Valid(v *validation.Validation) {
	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}

	if len(form.LangAdds) > 0 {
		adds := make(models.SliceStringField, 0, len(form.LangAdds))
		for _, l := range form.LangAdds {
			if d, err := utils.StrTo(l).Int(); err == nil {
				if form.Lang == d {
					continue
				}
				if len(i18n.GetLangByIndex(form.Lang)) == 0 {
					v.SetError("Lang", "Can not be empty")
					return
				}
				adds = append(adds, l)
			}
		}
		form.LangAdds = adds
	}
}
Example #24
0
func (form *PostAdminForm) Valid(v *validation.Validation) {
	var err error
	if _, err = models.GetUserById(form.User); err != nil {
		v.SetError("User", "admin.not_found_by_id")
	}

	if _, err = models.GetUserById(form.LastReply); err != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	if _, err = models.GetUserById(form.LastAuthor); err != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	if _, err = models.GetTopicById(form.Topic); err != nil {
		v.SetError("Topic", "admin.not_found_by_id")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Not Found")
	}
}
Example #25
0
func (this *AuthController) DoLogin() {
	this.CheckRequestFrequency(3, 15, 30)
	if this.IsLogin {
		this.Redirect("/", 302)
	}
	valid := validation.Validation{}
	form := LoginForm{}
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	b, err := valid.Valid(form)
	if err != nil {
		beego.Error(err)
	}
	if !b {
		this.loginPageWithErrors(form, valid.Errors)
		return
	}
	//用户不存在?
	user := models.User{Username: form.Username, Email: form.Username}
	if err := user.Read("Username"); err != nil {
		if err2 := user.Read("Email"); err2 != nil {
			errMsg := fmt.Sprintf("用户 %s 不存在!", form.Username)
			beego.Trace(errMsg)
			valid.SetError("username", errMsg)
			this.loginPageWithErrors(form, valid.Errors)
			return
		}
	}
	//用户被禁止?
	if user.IsBanned {
		beego.Trace(fmt.Sprintf("用户%s被禁用,不能登录!", user.Username))
		valid.SetError("username", "抱歉,您被禁止登录!")
		this.loginPageWithErrors(form, valid.Errors)
		return
	}
	//检查密码
	if !user.VerifyPassword(form.Password) {
		beego.Trace(fmt.Sprintf("%s 登录失败!", form.Username))
		valid.SetError("password", "密码错误")
		this.loginPageWithErrors(form, valid.Errors)
		return
	}
	//验证全部通过
	var remember bool
	if form.Remember != "" {
		remember = true
	}
	this.LogUserIn(&user, remember)
	this.Redirect(GetLoginRedirectUrl(this.Ctx), 302)
	return
}
Example #26
0
File: form.go Project: zeuson/wego
func (form *UserAdminForm) Valid(v *validation.Validation) {
	if exist, _ := models.IsUserExistByName(form.UserName, int64(form.Id)); exist {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if exist, _ := models.IsUserExistByEmail(form.Email, int64(form.Id)); exist {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}
}
Example #27
0
func (this *SocialAuthController) processLoginForm(socialType social.SocialType) {
	valid := validation.Validation{}
	form := SocialAuthLoginForm{}
	var user models.User
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	b, err := valid.Valid(form)
	if err != nil {
		beego.Error(err)
	}
	if !b {
		goto showLoginErrors
	}
	//用户不存在?
	user = models.User{Username: form.Username, Email: form.Username}
	if err := user.Read("Username"); err != nil {
		if err2 := user.Read("Email"); err2 != nil {
			errMsg := fmt.Sprintf("用户 %s 不存在!", form.Username)
			valid.SetError("Username", errMsg)
			goto showLoginErrors
		}
	}
	//用户被禁止?
	if user.IsBanned {
		beego.Trace(fmt.Sprintf("用户%s被禁用,不能登录!", user.Username))
		valid.SetError("Username", "抱歉,您被禁止登录!")
		goto showLoginErrors
	}
	//检查密码
	if !user.VerifyPassword(form.Password) {
		beego.Trace(fmt.Sprintf("%s 登录失败!", form.Username))
		valid.SetError("Password", "密码错误")
		goto showLoginErrors
	}
	//验证全部通过
	if loginRedirect, _, err := SocialAuth.ConnectAndLogin(this.Ctx, socialType, user.Id); err != nil {
		beego.Error("ConnectAndLogin:"******"Let's redirect ", loginRedirect)
		this.Redirect(loginRedirect, 302)
		return
	}
showLoginErrors:
	this.Data["LoginForm"] = form
	this.Data["LoginFormErrors"] = valid.Errors
	return
}
Example #28
0
func (form *RegisterForm) Valid(v *validation.Validation) {

	// Check if passwords of two times are same.
	if form.Password != form.PasswordRe {
		v.SetError("PasswordRe", "auth.repassword_not_match")
		return
	}

	e1, e2, _ := CanRegistered(form.UserName, form.Email)

	if !e1 {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if !e2 {
		v.SetError("Email", "auth.email_already_taken")
	}

	if !setting.Captcha.Verify(form.CaptchaId, form.Captcha) {
		v.SetError("Captcha", "auth.captcha_wrong")
	}
}
Example #29
0
func (form *UserAdminForm) Valid(v *validation.Validation) {
	qs := models.Users()

	if models.CheckIsExist(qs, "UserName", form.UserName, form.Id) {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if models.CheckIsExist(qs, "Email", form.Email, form.Id) {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}
}
Example #30
0
func (form *TopicAdminForm) Valid(v *validation.Validation) {
	qs := Topics()

	if CheckIsExist(qs, "Name", form.Name, form.Id) {
		v.SetError("Name", "admin.field_need_unique")
	}

	if CheckIsExist(qs, "NameZhCn", form.NameZhCn, form.Id) {
		v.SetError("NameZhCn", "admin.field_need_unique")
	}

	if CheckIsExist(qs, "Slug", form.Slug, form.Id) {
		v.SetError("Slug", "admin.field_need_unique")
	}
}