Exemplo n.º 1
0
func (this *AuthController) DoRegister() {
	this.CheckRequestFrequency(3, 15, 30)
	this.Layout = "layout.html"
	this.TplNames = "register.html"
	valid := validation.Validation{}
	form := RegisterForm{}
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	b, err := valid.Valid(form)
	if err != nil {
		beego.Error(err)
	}
	if !b {
		this.registerPageWithErrors(form, valid.Errors)
		return
	}
	//验证用户名
	user := models.User{Username: form.Username}
	if err := user.ValidUsername(); err != nil {
		valid.SetError("username", err.Error())
		this.registerPageWithErrors(form, valid.Errors)
		return
	} else {
		if user.Read("Username") == nil {
			valid.SetError("username", fmt.Sprintf("%s已被使用,请使用其他用户名!", form.Username))
			this.registerPageWithErrors(form, valid.Errors)
			return
		}
	}
	//验证email未被注册
	user.Email = form.Email
	if user.Read("Email") == nil {
		valid.SetError("email", "已被使用,请直接使用此电邮登录")
		this.registerPageWithErrors(form, valid.Errors)
		return
	}
	//通过所有验证
	actCode, _ := user.GenerateActivateCode()
	user.SetPassword(form.Password)
	if err := user.Insert(); err != nil {
		beego.Error(err)
		this.Abort("500")
		return
	}
	sub := sendcloud.NewSubstitution()
	sub.AddTo(user.Email)
	sub.AddSub("%appname%", setting.AppName)
	sub.AddSub("%name%", user.Username)
	sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.Activate", ":code", actCode))
	if err := setting.Sendcloud.SendTemplate("gotalk_register", setting.AppName+"欢迎你", setting.From, setting.FromName, sub); err != nil {
		beego.Error(err)
	}
	this.FlashWrite("notice", fmt.Sprintf("注册成功!欢迎你, %s。建议你再花点时间上传头像、验证电子邮件!", user.Username))
	this.LogUserIn(&user, false)
	userEditUrl := beego.UrlFor("UserController.Edit", ":username", user.Username)
	this.Redirect(userEditUrl, 302)
	return
}
Exemplo n.º 2
0
func (this *UserController) resendValidation(user *models.User) {
	//发验证邮件
	sub := sendcloud.NewSubstitution()
	sub.AddTo(user.Email)
	sub.AddSub("%name%", user.Username)
	sub.AddSub("%appname%", setting.AppName)
	code, err := user.GenerateActivateCode()
	if err != nil {
		beego.Trace(err)
		this.Abort("500")
	}
	sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.Activate", ":code", code))
	if err := setting.Sendcloud.SendTemplate("gotalk_revalidate", setting.AppName+"邮件验证", setting.From, setting.FromName, sub); err != nil {
		beego.Error(err)
	}
}
Exemplo n.º 3
0
//输入email,发送重设密码邮件
func (this *AuthController) ForgetPassword() {
	this.Data["PageTitle"] = fmt.Sprintf("忘记密码 | %s", setting.AppName)
	this.Layout = "layout.html"
	this.TplNames = "forget-password.html"
	valid := validation.Validation{}
	form := ForgetPasswordForm{}
	if this.Ctx.Request.Method == "POST" {
		if err := this.ParseForm(&form); err != nil {
			beego.Error(err)
		}
		_, err := valid.Valid(form)
		if err != nil {
			beego.Error(err)
		}
		user := models.User{Email: form.Email}
		if err := user.Read("Email"); err != nil {
			beego.Trace(user)
			beego.Trace(form)
			valid.SetError("Email", "此电子邮件并未注册")
		}
		beego.Trace(valid.Errors)
		if len(valid.Errors) == 0 {
			//发送忘记密码邮件
			code, err := user.GenerateActivateCode()
			if err != nil {
				this.Abort("500")
			}
			sub := sendcloud.NewSubstitution()
			sub.AddTo(user.Email)
			sub.AddSub("%appname%", setting.AppName)
			sub.AddSub("%name%", user.Username)
			sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.ResetPassword", ":code", code))
			if err := setting.Sendcloud.SendTemplate("gotalk_password", setting.AppName+"忘记密码", setting.From, setting.FromName, sub); err != nil {
				beego.Error(err)
			}
			this.FlashWrite("notice", fmt.Sprintf("重设密码的方法已经发到%s。请查收!", user.Email))
			this.Redirect("/", 302)
		} else {
			this.Data["HasError"] = true
			this.Data["errors"] = valid.Errors
		}
	}
}
Exemplo n.º 4
0
func (this *SocialAuthController) processRegisterForm(socialType social.SocialType, form SocialAuthRegisterForm, socialUserAvatarUrl string) {
	valid := validation.Validation{}
	var user models.User
	var actCode string
	var sub *sendcloud.Substitution
	var resp *http.Response
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	if err := this.ParseForm(&form); err != nil {
		beego.Error(err)
	}
	b, err := valid.Valid(form)
	if err != nil {
		beego.Error(err)
	}
	if !b {
		goto showRegisterErrors
	}
	//验证用户名
	user = models.User{Username: form.Username}
	if err := user.ValidUsername(); err != nil {
		valid.SetError("Username", err.Error())
		goto showRegisterErrors
	} else {
		if user.Read("Username") == nil {
			valid.SetError("Username", fmt.Sprintf("%s已被使用,请使用其他用户名!", form.Username))
			goto showRegisterErrors
		}
	}
	//验证email未被注册
	user.Email = form.Email
	if user.Read("Email") == nil {
		valid.SetError("Email", "已被使用,请直接使用此电邮登录")
		goto showRegisterErrors
	}
	//通过所有验证
	actCode, _ = user.GenerateActivateCode()
	user.SetPassword(form.Password)
	if err := user.Insert(); err != nil {
		beego.Error(err)
		this.Abort("500")
		return
	}
	sub = sendcloud.NewSubstitution()
	sub.AddTo(user.Email)
	sub.AddSub("%appname%", setting.AppName)
	sub.AddSub("%name%", user.Username)
	sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.Activate", ":code", actCode))
	if err := setting.Sendcloud.SendTemplate("gotalk_register", setting.AppName+"欢迎你", setting.From, setting.FromName, sub); err != nil {
		beego.Error(err)
	}
	//复制头像
	if resp, err = http.Get(socialUserAvatarUrl); err != nil {
		beego.Error(fmt.Sprintf("Error opening url:%s", socialUserAvatarUrl))
		this.Abort("500")
		return
	}
	defer resp.Body.Close()
	if err := user.ValidateAndSetAvatar(resp.Body, ""); err != nil {
		beego.Error("error copying avatar ", socialUserAvatarUrl, "error: ", err.Error())
	}
	if err := user.Update(); err != nil {
		beego.Error("error update avatar for user ", user.Username)
	}

	this.FlashWrite("notice", fmt.Sprintf("注册成功!欢迎你, %s。建议你再花点时间验证电子邮件!", user.Username))
	if loginRedirect, _, err := SocialAuth.ConnectAndLogin(this.Ctx, socialType, user.Id); err != nil {
		beego.Error("ConnectAndLogin:"******"Let's redirect ", loginRedirect)
		this.Redirect(loginRedirect, 302)
		return
	}
showRegisterErrors:
	this.Data["RegisterForm"] = form
	this.Data["RegisterormErrors"] = valid.Errors
	return
}