Exemplo n.º 1
0
func checkAccount(ctx *context.Context) bool {
	ck, err := ctx.Request.Cookie("uname")
	if err != nil {
		return false
	}

	uname := ck.Value

	ck, err = ctx.Request.Cookie("pwd")
	if err != nil {
		return false
	}

	pwd := ck.Value

	// 验证用户名及密码
	if uname == beego.AppConfig.String("adminName") && pwd == beego.AppConfig.String("adminPass") {
		return true
	} else {

		var user *(models.User)
		var err error
		switch beego.AppConfig.String("database") {
		case "redis":
			user, err = models.GetUserRedis(uname)
		default:
			user, err = models.GetUser(uname)
		}
		if err == nil && pwd == user.Password {
			return true
		}
	}
	return false
}
Exemplo n.º 2
0
func (this *UserController) LoadView() {
	uname := this.Input().Get("uname")
	var user *(models.User)
	var err error
	switch beego.AppConfig.String("database") {
	case "redis":
		user, err = models.GetUserRedis(uname)
	default:
		user, err = models.GetUser(uname)
	}

	if err != nil {
		beego.Error(err)
		this.Redirect("/", 302)
		return
	}

	data := &struct {
		User *models.User
	}{
		User: user,
	}

	this.Data["json"] = data
	this.ServeJson()
}
Exemplo n.º 3
0
func (this *LoginController) Post() {

	// 获取表单信息
	uname := this.Input().Get("uname")
	pwd := this.Input().Get("pwd")
	autoLogin := this.Input().Get("autoLogin") == "on"

	// 验证用户名及密码
	if uname == beego.AppConfig.String("adminName") && pwd == beego.AppConfig.String("adminPass") {
		maxAge := 0
		if autoLogin {
			maxAge = 1<<31 - 1
		}

		this.Ctx.SetCookie("uname", uname, maxAge, "/")
		this.Ctx.SetCookie("pwd", pwd, maxAge, "/")
	} else {

		var user *(models.User)
		var err error
		switch beego.AppConfig.String("database") {
		case "redis":
			user, err = models.GetUserRedis(uname)
		default:
			user, err = models.GetUser(uname)
		}

		if err == nil && pwd == user.Password {
			maxAge := 0
			if autoLogin {
				maxAge = 1<<31 - 1
			}

			this.Ctx.SetCookie("uname", uname, maxAge, "/")
			this.Ctx.SetCookie("pwd", pwd, maxAge, "/")
		}
	}

	this.Redirect("/", 302)
}