Ejemplo n.º 1
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
	}
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 3
0
// setLang sets site language version.
func (this *baseRouter) setLang() bool {
	isNeedRedir := false
	hasCookie := false

	// get all lang names from i18n
	langs := utils.Langs

	// 1. Check URL arguments.
	lang := this.GetString("lang")

	// 2. Get language information from cookies.
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	} else {
		isNeedRedir = true
	}

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	// 3. check if isLogin then use user setting
	if len(lang) == 0 && this.isLogin {
		lang = i18n.GetLangByIndex(this.user.Lang)
	}

	// 4. Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Input.Header("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 4. DefaucurLang language is English.
	if len(lang) == 0 {
		lang = "en-US"
		isNeedRedir = false
	}

	// Save language information in cookies.
	if !hasCookie {
		this.setLangCookie(lang)
	}

	// Set language properties.
	this.Data["Lang"] = lang
	this.Data["Langs"] = langs

	this.Lang = lang

	return isNeedRedir
}
Ejemplo n.º 4
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")
	}

	valid = false
	for _, cat := range form.Categories {
		if cat.Id == form.Category {
			valid = true
		}
	}

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

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "error")
	}
}
Ejemplo n.º 5
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
	//}
}
Ejemplo n.º 6
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
	}
}
Ejemplo n.º 7
0
func (this *BaseRouter) LoginUser(user *models.User, remember bool) string {
	loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
	if utils.IsMatchHost(loginRedirect) == false {
		loginRedirect = "/"
	} else {
		this.Ctx.SetCookie("login_to", "", -1, "/")
	}

	// login user
	auth.LoginUser(user, this.Ctx, remember)

	this.setLangCookie(i18n.GetLangByIndex(user.Lang))

	return loginRedirect
}
Ejemplo n.º 8
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")
	}
}
Ejemplo n.º 9
0
func (this *BaseController) setLang() {

	// get all lang names from i18n
	langs := models.Langs

	// Get language information from cookies.
	lang := this.Ctx.GetCookie("lang")

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
	}

	// check if isLogin then use user setting
	if len(lang) == 0 && this.IsLogin {
		lang = i18n.GetLangByIndex(this.User.Lang)
	}

	// Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Input.Header("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// Default language is English.
	if len(lang) == 0 {
		lang = "en-US"
	}

	// Save language information in cookies.
	// this.Ctx.SetCookie("lang", lang, 60*60*24*365, "/", nil, nil, false)

	// Set language properties.
	this.Locale.Lang = lang
	this.Data["Lang"] = lang
	this.Data["Langs"] = langs
}
Ejemplo n.º 10
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
	//}
}
Ejemplo n.º 11
0
// Login implemented user login.
func (this *LoginRouter) Login() {
	this.Data["IsLoginPage"] = true
	this.TplNames = "auth/login.html"

	// no need login
	if this.CheckLoginRedirect(false) {
		return
	}

	var user models.User
	var key string
	ajaxErrMsg := "auth.login_error_ajax"

	form := models.LoginForm{}
	// valid form and put errors to template context
	if this.ValidFormSets(&form) == false {
		if this.IsAjax() {
			goto ajaxError
		}
		return
	}

	key = "auth.login." + form.UserName + this.Ctx.Input.IP()
	if times, ok := utils.TimesReachedTest(key, utils.LoginMaxRetries); ok {
		if this.IsAjax() {
			ajaxErrMsg = "auth.login_error_times_reached"
			goto ajaxError
		}
		this.Data["ErrorReached"] = true

	} else if models.VerifyUser(&user, form.UserName, form.Password) {
		loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
		if utils.IsMatchHost(loginRedirect) == false {
			loginRedirect = "/"
		} else {
			this.Ctx.SetCookie("login_to", "", -1, "/")
		}

		// login user
		models.LoginUser(&user, &this.Controller, form.Remember)

		this.setLangCookie(i18n.GetLangByIndex(user.Lang))

		if this.IsAjax() {
			this.Data["json"] = map[string]interface{}{
				"success":  true,
				"message":  this.Tr("auth.login_success_ajax"),
				"redirect": loginRedirect,
			}
			this.ServeJson()
			return
		}

		this.Redirect(loginRedirect, 302)
		return
	} else {
		utils.TimesReachedSet(key, times, utils.LoginFailedBlocks)
		if this.IsAjax() {
			goto ajaxError
		}
	}
	this.Data["Error"] = true
	return

ajaxError:
	this.Data["json"] = map[string]interface{}{
		"success": false,
		"message": this.Tr(ajaxErrMsg),
		"once":    this.Data["once_token"],
	}
	this.ServeJson()
}
Ejemplo n.º 12
0
func (m *Post) GetLang() string {
	return i18n.GetLangByIndex(m.Lang)
}
Ejemplo n.º 13
0
func (form *ProfileForm) Valid(v *validation.Validation) {
	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}
}
Ejemplo n.º 14
0
// Login implemented POST user login.
func (this *LoginRouter) Login() {
	this.Data["IsLoginPage"] = true
	this.TplNames = "auth/login.html"

	// no need login
	if this.CheckLoginRedirect(false) {
		return
	}

	var user models.User

	form := models.LoginForm{Locale: this.Locale}
	// valid form and put errors to template context
	if this.ValidFormSets(&form) == false {
		if this.IsAjax() {
			goto ajaxError
		}
		return
	}

	if models.VerifyUser(&user, form.Email, form.Password) {
		loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
		if utils.IsMatchHost(loginRedirect) == false {
			loginRedirect = "/home"
		} else {
			this.Ctx.SetCookie("login_to", "", -1, "/")
		}

		// login user
		models.LoginUser(&user, &this.Controller, form.Remember)

		this.setLangCookie(i18n.GetLangByIndex(user.Lang))

		if this.IsAjax() {
			this.Data["json"] = map[string]interface{}{
				"success":  true,
				"message":  this.Tr("auth.login_success_ajax"),
				"redirect": loginRedirect,
			}
			this.ServeJson()
			return
		}

		this.Redirect(loginRedirect, 302)
		return
	} else {
		if this.IsAjax() {
			goto ajaxError
		}
	}
	this.Data["Error"] = true
	return

ajaxError:
	this.Data["json"] = map[string]interface{}{
		"success": false,
		"message": this.Tr("auth.login_error_ajax"),
		"once":    this.Data["once_token"],
	}
	this.ServeJson()
}