Beispiel #1
0
// Register implemented Post method for RegisterRouter.
func (this *Register) Post() error {
	this.Data["IsRegisterPage"] = true

	// no need login
	if this.CheckLoginRedirect(false) {
		return nil
	}

	form := auth.RegisterForm{Locale: this.Locale}
	// valid form and put errors to template context
	if this.ValidFormSets(&form) == false {
		return this.Render("auth/register.html", this.Data)
	}

	// Create new user.
	user := new(models.User)

	if err := auth.RegisterUser(user, form.UserName, form.Email, form.Password, this.Locale); err != nil {
		return err
	}

	auth.SendRegisterMail(middlewares.Renders, this.Locale, user)

	loginRedirect := this.LoginUser(user, false)
	if loginRedirect == "/" {
		this.FlashRedirect("/settings/profile", 302, "RegSuccess")
	} else {
		this.Redirect(loginRedirect)
	}
	return nil
}
Beispiel #2
0
func (this *SocialAuthRouter) Post() {
	this.TplNames = "auth/connect.html"

	if this.CheckLoginRedirect(false) {
		return
	}

	var socialType social.SocialType
	if !this.canConnect(&socialType) {
		this.Redirect(setting.SocialAuth.LoginURL, 302)
		return
	}

	p, ok := social.GetProviderByType(socialType)
	if !ok {
		this.Redirect(setting.SocialAuth.LoginURL, 302)
		return
	}

	var form interface{}

	formL := auth.OAuthLoginForm{}
	this.SetFormSets(&formL)

	formR := auth.OAuthRegisterForm{Locale: this.Locale}
	this.SetFormSets(&formR)

	action := this.GetString("action")
	if action == "connect" {
		form = &formL
	} else {
		form = &formR
	}

	this.Data["Action"] = action
	this.Data["Social"] = socialType

	// valid form and put errors to template context
	if this.ValidFormSets(form) == false {
		return
	}

	var user models.User

	switch action {
	case "connect":
		key := "auth.login." + formL.UserName + utils.IP(this.Req())
		if times, ok := utils.TimesReachedTest(key, setting.LoginMaxRetries); ok {
			this.Data["ErrorReached"] = true
		} else if auth.VerifyUser(&user, formL.UserName, formL.Password) {
			goto connect
		} else {
			utils.TimesReachedSet(key, times, setting.LoginFailedBlocks)
		}

	default:
		if err := auth.RegisterUser(&user, formR.UserName, formR.Email, formR.Password, this.Locale); err == nil {

			auth.SendRegisterMail(middlewares.Renders, this.Locale, &user)

			goto connect

		} else {
			log.Error("Register: Failed ", err)
		}
	}

failed:
	this.Data["Error"] = true
	return

connect:
	if loginRedirect, _, err := setting.SocialAuth.ConnectAndLogin(this.Context, &this.Session, socialType, int(user.Id)); err != nil {
		log.Error("ConnectAndLogin:"******"connect":
		this.FlashRedirect("/settings/profile", 302, "ConnectSuccess", p.GetName())
	default:
		this.FlashRedirect("/settings/profile", 302, "RegSuccess")
	}
}