Skip to content

Commit c003070

Browse files
AJ ONealryanburnette
AJ ONeal
authored andcommitted
treat register like sign in, even oauth
1 parent c168095 commit c003070

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

modules/auth/user_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type RegisterForm struct {
8080
Email string `binding:"Required;Email;MaxSize(254)"`
8181
Password string `binding:"Required;MaxSize(255)"`
8282
Retype string
83+
Remember bool
8384
GRecaptchaResponse string `form:"g-recaptcha-response"`
8485
}
8586

routers/user/auth.go

+51-56
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,37 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
509509
return setting.AppSubURL + "/"
510510
}
511511

512+
func handleRegister(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) {
513+
// Auto-set admin for the only user.
514+
if models.CountUsers() == 1 {
515+
u.IsAdmin = true
516+
u.IsActive = true
517+
u.SetLastLogin()
518+
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
519+
ctx.ServerError("UpdateUser", err)
520+
return
521+
}
522+
}
523+
524+
// Send confirmation email
525+
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
526+
models.SendActivateAccountMail(ctx.Context, u)
527+
ctx.Data["IsSendRegisterMail"] = true
528+
ctx.Data["Email"] = u.Email
529+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
530+
ctx.HTML(200, TplActivate)
531+
532+
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
533+
log.Error("Set cache(MailResendLimit) fail: %v", err)
534+
}
535+
return
536+
}
537+
538+
ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
539+
// Complete the signin without logging in again
540+
handleSignInFull(ctx, u, remember, true)
541+
}
542+
512543
// SignInOAuth handles the OAuth2 login buttons
513544
func SignInOAuth(ctx *context.Context) {
514545
provider := ctx.Params(":provider")
@@ -834,14 +865,20 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
834865
ctx.ServerError("CreateUser", err)
835866
}
836867

868+
// TODO LoginName should come from form.UserName... shouldn't it?
837869
u := &models.User{
838-
Name: form.UserName,
839-
Email: form.Email,
840-
Passwd: form.Password,
841-
IsActive: !setting.Service.RegisterEmailConfirm,
842-
LoginType: models.LoginOAuth2,
843-
LoginSource: loginSource.ID,
844-
LoginName: gothUser.(goth.User).UserID,
870+
Name: form.UserName,
871+
Email: form.Email,
872+
Passwd: form.Password,
873+
IsActive: !setting.Service.RegisterEmailConfirm,
874+
}
875+
876+
// This will link the account in such a way that it cannot be removed
877+
// TODO why is this different from normal linking?
878+
if setting.Service.AllowOnlyExternalRegistration {
879+
u.LoginType = models.LoginOAuth2
880+
u.LoginSource = loginSource.ID
881+
u.LoginName = gothUser.(goth.User).UserID
845882
}
846883

847884
if err := models.CreateUser(u); err != nil {
@@ -865,32 +902,16 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
865902
}
866903
log.Trace("Account created: %s", u.Name)
867904

868-
// Auto-set admin for the only user.
869-
if models.CountUsers() == 1 {
870-
u.IsAdmin = true
871-
u.IsActive = true
872-
u.SetLastLogin()
873-
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
874-
ctx.ServerError("UpdateUser", err)
905+
// This will link the account in such a way that it can be removed
906+
if !setting.Service.AllowOnlyExternalRegistration {
907+
err = models.LinkAccountToUser(u, gothUser.(goth.User))
908+
if err != nil {
909+
ctx.ServerError("UserLinkAccount", err)
875910
return
876911
}
877912
}
878913

879-
// Send confirmation email
880-
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
881-
models.SendActivateAccountMail(ctx.Context, u)
882-
ctx.Data["IsSendRegisterMail"] = true
883-
ctx.Data["Email"] = u.Email
884-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
885-
ctx.HTML(200, TplActivate)
886-
887-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
888-
log.Error("Set cache(MailResendLimit) fail: %v", err)
889-
}
890-
return
891-
}
892-
893-
ctx.Redirect(setting.AppSubURL + "/user/login")
914+
handleRegister(ctx, u, form.Remember, true)
894915
}
895916

896917
// SignOut sign out from login status
@@ -1003,33 +1024,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
10031024
}
10041025
log.Trace("Account created: %s", u.Name)
10051026

1006-
// Auto-set admin for the only user.
1007-
if models.CountUsers() == 1 {
1008-
u.IsAdmin = true
1009-
u.IsActive = true
1010-
u.SetLastLogin()
1011-
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
1012-
ctx.ServerError("UpdateUser", err)
1013-
return
1014-
}
1015-
}
1016-
1017-
// Send confirmation email, no need for social account.
1018-
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
1019-
models.SendActivateAccountMail(ctx.Context, u)
1020-
ctx.Data["IsSendRegisterMail"] = true
1021-
ctx.Data["Email"] = u.Email
1022-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
1023-
ctx.HTML(200, TplActivate)
1024-
1025-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
1026-
log.Error("Set cache(MailResendLimit) fail: %v", err)
1027-
}
1028-
return
1029-
}
1030-
1031-
ctx.Flash.Success(ctx.Tr("auth.sign_up_successful"))
1032-
handleSignInFull(ctx, u, false, true)
1027+
handleRegister(ctx, u, form.Remember, true)
10331028
}
10341029

10351030
// Activate render activate user page

templates/user/auth/signup_inner.tmpl

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
</div>
5050
{{end}}
5151

52+
<div class="inline field">
53+
<label></label>
54+
<div class="ui checkbox">
55+
<label>{{.i18n.Tr "auth.remember_me"}}</label>
56+
<input name="remember" type="checkbox">
57+
</div>
58+
</div>
59+
5260
<div class="inline field">
5361
<label></label>
5462
<button class="ui green button">

0 commit comments

Comments
 (0)