From 2bdc146bcf6818f34a9aead2255e5e15f0d1e417 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 7 Oct 2018 03:47:44 +0000 Subject: [PATCH] treat register like sign in, even oauth --- modules/auth/user_form.go | 1 + routers/user/auth.go | 107 ++++++++++++-------------- templates/user/auth/signup_inner.tmpl | 7 ++ 3 files changed, 59 insertions(+), 56 deletions(-) diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go index 43ddb29c7..df89c42d4 100644 --- a/modules/auth/user_form.go +++ b/modules/auth/user_form.go @@ -76,6 +76,7 @@ type RegisterForm struct { Email string `binding:"Required;Email;MaxSize(254)"` Password string `binding:"Required;MaxSize(255)"` Retype string + Remember bool GRecaptchaResponse string `form:"g-recaptcha-response"` } diff --git a/routers/user/auth.go b/routers/user/auth.go index a4a0ee3e6..a8184d536 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -497,6 +497,37 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR return setting.AppSubURL + "/" } +func handleRegister(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) { + // Auto-set admin for the only user. + if models.CountUsers() == 1 { + u.IsAdmin = true + u.IsActive = true + u.SetLastLogin() + if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { + ctx.ServerError("UpdateUser", err) + return + } + } + + // Send confirmation email + if setting.Service.RegisterEmailConfirm && u.ID > 1 { + models.SendActivateAccountMail(ctx.Context, u) + ctx.Data["IsSendRegisterMail"] = true + ctx.Data["Email"] = u.Email + ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) + ctx.HTML(200, TplActivate) + + if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + log.Error(4, "Set cache(MailResendLimit) fail: %v", err) + } + return + } + + ctx.Flash.Success(ctx.Tr("auth.sign_up_successful")) + // Complete the signin without logging in again + handleSignInFull(ctx, u, remember, true) +} + // SignInOAuth handles the OAuth2 login buttons func SignInOAuth(ctx *context.Context) { provider := ctx.Params(":provider") @@ -800,14 +831,20 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au ctx.ServerError("CreateUser", err) } + // TODO LoginName should come from form.UserName... shouldn't it? u := &models.User{ - Name: form.UserName, - Email: form.Email, - Passwd: form.Password, - IsActive: !setting.Service.RegisterEmailConfirm, - LoginType: models.LoginOAuth2, - LoginSource: loginSource.ID, - LoginName: gothUser.(goth.User).UserID, + Name: form.UserName, + Email: form.Email, + Passwd: form.Password, + IsActive: !setting.Service.RegisterEmailConfirm, + } + + // This will link the account in such a way that it cannot be removed + // TODO why is this different from normal linking? + if setting.Service.AllowOnlyExternalRegistration { + u.LoginType = models.LoginOAuth2 + u.LoginSource = loginSource.ID + u.LoginName = gothUser.(goth.User).UserID } if err := models.CreateUser(u); err != nil { @@ -831,32 +868,16 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au } log.Trace("Account created: %s", u.Name) - // Auto-set admin for the only user. - if models.CountUsers() == 1 { - u.IsAdmin = true - u.IsActive = true - u.SetLastLogin() - if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { - ctx.ServerError("UpdateUser", err) + // This will link the account in such a way that it can be removed + if !setting.Service.AllowOnlyExternalRegistration { + err = models.LinkAccountToUser(u, gothUser.(goth.User)) + if err != nil { + ctx.ServerError("UserLinkAccount", err) return } } - // Send confirmation email - if setting.Service.RegisterEmailConfirm && u.ID > 1 { - models.SendActivateAccountMail(ctx.Context, u) - ctx.Data["IsSendRegisterMail"] = true - ctx.Data["Email"] = u.Email - ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) - ctx.HTML(200, TplActivate) - - if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { - log.Error(4, "Set cache(MailResendLimit) fail: %v", err) - } - return - } - - ctx.Redirect(setting.AppSubURL + "/user/login") + handleRegister(ctx, u, form.Remember, true) } // SignOut sign out from login status @@ -964,33 +985,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo } log.Trace("Account created: %s", u.Name) - // Auto-set admin for the only user. - if models.CountUsers() == 1 { - u.IsAdmin = true - u.IsActive = true - u.SetLastLogin() - if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { - ctx.ServerError("UpdateUser", err) - return - } - } - - // Send confirmation email, no need for social account. - if setting.Service.RegisterEmailConfirm && u.ID > 1 { - models.SendActivateAccountMail(ctx.Context, u) - ctx.Data["IsSendRegisterMail"] = true - ctx.Data["Email"] = u.Email - ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) - ctx.HTML(200, TplActivate) - - if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { - log.Error(4, "Set cache(MailResendLimit) fail: %v", err) - } - return - } - - ctx.Flash.Success(ctx.Tr("auth.sign_up_successful")) - handleSignInFull(ctx, u, false, true) + handleRegister(ctx, u, form.Remember, true) } // Activate render activate user page diff --git a/templates/user/auth/signup_inner.tmpl b/templates/user/auth/signup_inner.tmpl index cd969276b..a89d8c2be 100644 --- a/templates/user/auth/signup_inner.tmpl +++ b/templates/user/auth/signup_inner.tmpl @@ -45,6 +45,13 @@ {{end}} +
+ +
+ + +
+