Esempio n. 1
0
func (m *User) SetPassword(password string) error {
	m.Salt = utils.GetRandomString(6)
	m.Password = utils.EncodeMd5(utils.EncodeMd5(password) + m.Salt)
	return nil
}
Esempio n. 2
0
func (this *SocialAuthController) Connect() {
	this.Data["PageTitle"] = fmt.Sprintf("社交帐号登录 | %s", setting.AppName)
	this.Layout = "layout.html"
	this.TplNames = "social-login.html"
	if this.IsLogin {
		this.Redirect("/", 302)
	}
	//检查社交帐号登录是否正常
	var socialType social.SocialType
	if !this.canConnect(&socialType) {
		beego.Error(this.GetString("error_description"))
		this.Abort("500")
		this.Redirect(SocialAuth.LoginURL, 302)
		return
	}
	p, _ := social.GetProviderByType(socialType)
	if p == nil {
		beego.Error("unknown provider")
	}
	var socialUserLogin, socialUserEmail, socialUserAvatarUrl string
	var ok bool
	if socialUserLogin, ok = this.GetSession("social_user_login").(string); !ok {
		beego.Error("error while reading session ")
		this.Abort("500")
	}
	if socialUserEmail, ok = this.GetSession("social_user_email").(string); !ok {
		beego.Error("error while reading session ")
		this.Abort("500")
	}
	if socialUserAvatarUrl, ok = this.GetSession("social_user_avatar_url").(string); !ok {
		beego.Error("error while reading session ")
		this.Abort("500")
	}
	this.Data["SocialType"] = p.GetName()
	this.Data["SocialUserLogin"] = socialUserLogin
	this.Data["SocialUserEmail"] = socialUserEmail
	this.Data["SocialUserAvatarUrl"] = socialUserAvatarUrl
	//准备注册表格初始数据
	registerForm := SocialAuthRegisterForm{}
	var user models.User
	if this.Ctx.Input.IsGet() {
		user = models.User{Username: socialUserLogin}
		if user.Read("Username") == nil {
			registerForm.Username = socialUserLogin + utils.GetRandomString(3)
			this.Data["UsernameTakenMsg"] = fmt.Sprintf("%s已经被使用,如果你不喜欢我们帮你选的%s,请修改", socialUserLogin, registerForm.Username)
		} else {
			registerForm.Username = socialUserLogin
		}
		if socialUserEmail != "" {
			user = models.User{Email: socialUserEmail}
			if user.Read("Email") == nil {
				registerForm.Email = ""
			} else {
				registerForm.Email = socialUserEmail
			}
		}
		this.Data["RegisterForm"] = registerForm
	}

	if this.Ctx.Input.IsPost() {
		this.CheckRequestFrequency(3, 15, 30)
		action := this.GetString("action")
		switch action {
		case "Register":
			this.processRegisterForm(socialType, registerForm, socialUserAvatarUrl)
		case "Login":
			this.processLoginForm(socialType)
		}
	}

}