@@ -509,6 +509,37 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
509
509
return setting .AppSubURL + "/"
510
510
}
511
511
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
+
512
543
// SignInOAuth handles the OAuth2 login buttons
513
544
func SignInOAuth (ctx * context.Context ) {
514
545
provider := ctx .Params (":provider" )
@@ -834,14 +865,20 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
834
865
ctx .ServerError ("CreateUser" , err )
835
866
}
836
867
868
+ // TODO LoginName should come from form.UserName... shouldn't it?
837
869
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
845
882
}
846
883
847
884
if err := models .CreateUser (u ); err != nil {
@@ -865,32 +902,16 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
865
902
}
866
903
log .Trace ("Account created: %s" , u .Name )
867
904
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 )
875
910
return
876
911
}
877
912
}
878
913
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 )
894
915
}
895
916
896
917
// SignOut sign out from login status
@@ -1003,33 +1024,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
1003
1024
}
1004
1025
log .Trace ("Account created: %s" , u .Name )
1005
1026
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 )
1033
1028
}
1034
1029
1035
1030
// Activate render activate user page
0 commit comments