Ejemplo n.º 1
0
func (this *OAuthController) Post() {
	this.Forbbiden("is", "guest")

	password := this.GetString("password")
	token := this.GetString("token")
	username := this.Data["username"].(string)

	user := models.User{}
	oa := models.OAuth{}

	result, _ := user.Login(username, password)

	if result {
		u, err := user.GetUser(0, username, "", "")
		if err != nil {
			log.Warnln("get uid failed.")
			this.Data["json"] = map[string]interface{}{
				"result": false,
				"msg":    "get uid failed.",
				"refer":  nil,
			}
		}

		if oa.GithubBind(token, "github", u.Id) {
			this.Data["json"] = map[string]interface{}{
				"result": true,
				"msg":    "binding success",
				"refer":  nil,
			}
		} else {
			this.Data["json"] = map[string]interface{}{
				"result": false,
				"msg":    "binding failed",
				"refer":  nil,
			}
		}

	} else {
		this.Data["json"] = map[string]interface{}{
			"result": false,
			"msg":    "wrong password.",
			"refer":  nil,
		}
	}

	this.ServeJson()

}
Ejemplo n.º 2
0
// run before get
func (this *BaseController) Prepare() {
	// get user level
	var lev string

	stn := time.Now()
	st := stn.UnixNano()
	this.Data["start"] = st

	log.Blueln(this.Ctx.Request.UserAgent())

	user := this.GetSession("username")
	if user == nil {
		lev = "guest" // guest, not login
	} else {
		level := this.GetSession("level")

		if level == nil {
			lev = "user"
		} else {
			if tmplev, ok := level.(string); !ok {
				lev = "user"
			} else {
				lev = tmplev
			}
		}

		username := user.(string)
		usr := models.User{}
		u, err := usr.GetUser(0, username, "", "")
		if err != nil {
			this.Data["nickname"] = ""
			this.Data["email_md5"] = ""
		} else {
			this.Data["username"] = username
			this.Data["nickname"] = u.Nickname
			this.Data["email_md5"] = com.Md5(u.Email)
		}

	}

	this.Data["userIs"] = lev

	// log.Pinkln(lev)
}
Ejemplo n.º 3
0
func (this *LoginController) Post() {
	this.Forbbiden("not", "guest")

	user := models.User{}

	username := this.GetString("username")
	password := this.GetString("password")

	if result, lev := user.Login(username, password); result {
		user, err := user.GetUser(0, username, "", "")
		if err != nil {
			this.Data["json"] = map[string]interface{}{
				"result": false,
				"msg":    "login failed, get user info failed",
				"refer":  nil,
			}
		} else {
			this.SetSession("username", username)
			this.SetSession("userid", fmt.Sprintf("%d", user.Id))
			this.SetSession("level", lev)

			this.Data["json"] = map[string]interface{}{
				"result": true,
				"msg":    "login success",
				"refer":  nil,
			}
		}

	} else {
		this.Data["json"] = map[string]interface{}{
			"result": false,
			"msg":    "login failed",
			"refer":  nil,
		}
	}

	this.ServeJson()
}