Ejemplo n.º 1
0
// Post implemented login action
func (this *LoginController) Login() {
	var (
		user  models.User
		form  models.LoginForm
		valid validation.Validation
	)

	if err := this.ParseForm(&form); err != nil {
		fmt.Println(err)
	} else {
		if ok, valid_err := valid.Valid(form); ok && valid_err == nil {
			user.Name = form.UserName
			//user.Password = helpers.EncryptPassword(form.Password, nil)
			has, gerr := models.Engine.Get(&user)
			if has && gerr == nil && helpers.ValidatePassword(user.Password, form.Password) {
				this.SetSession("username", user.Name)
				this.SetSession("userid", int(user.Id))
				this.SetSession("userrole", int(user.IRole.Id))
				this.SetSession("useremail", user.Email)

				this.Redirect("/user/view/"+fmt.Sprintf("%d", user.Id), 302)
			}
		} else {
			for _, e := range valid.Errors {
				this.FlashError(e.Key + " : " + e.Message)
			}
		}
	}

	this.SaveFlash()
	this.Data["Form"] = form
	this.Data["Title"] = "Login"
	this.TplNames = "auth/login.html"
	// this.Redirect("/login", 302)
}
Ejemplo n.º 2
0
func (this *UserController) Block() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	user := models.User{}
	if _, err := models.Engine.Id(id).Get(&user); err == nil {
		user.Status = models.BLOCKED
		if _, e := models.Engine.Id(id).Update(&user); e == nil {
			// do something
		} else {
			// do something
		}
	}
}
Ejemplo n.º 3
0
// Post
func (this *RegisterController) Register() {
	var (
		user  models.User
		form  models.RegisterForm
		valid validation.Validation
		err   error
	)

	if err = this.ParseForm(&form); err != nil {
		fmt.Println(err)
	} else {
		if ok, e := valid.Valid(form); ok && e == nil {
			user.Name = form.UserName
			user.Email = form.Email
			user.Password = helpers.EncryptPassword(form.Password, nil)
			user.Status = models.ACTIVE
			user.IRole = models.Role{Id: models.ROLE_AUTHENTICATED}
			user.CreateTime = time.Now()

			if _, err = models.Engine.Insert(&user); err == nil {
				this.FlashNotice(user.Name + " registered successfully. Please login now!")
				this.SaveFlash()
				this.Redirect("/login", 302)
				return
			}
		} else {
			for _, e := range valid.Errors {
				this.FlashError(e.Key + " : " + e.Message)
			}
		}
	}

	this.SaveFlash()
	this.Data["Form"] = form
	this.Data["Title"] = "Register"
	this.TplNames = "auth/register.html"
}