@@ -497,6 +497,37 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
497
497
return setting .AppSubURL + "/"
498
498
}
499
499
500
+ func handleRegister (ctx * context.Context , u * models.User , remember bool , obeyRedirect bool ) {
501
+ // Auto-set admin for the only user.
502
+ if models .CountUsers () == 1 {
503
+ u .IsAdmin = true
504
+ u .IsActive = true
505
+ u .SetLastLogin ()
506
+ if err := models .UpdateUserCols (u , "is_admin" , "is_active" , "last_login_unix" ); err != nil {
507
+ ctx .ServerError ("UpdateUser" , err )
508
+ return
509
+ }
510
+ }
511
+
512
+ // Send confirmation email
513
+ if setting .Service .RegisterEmailConfirm && u .ID > 1 {
514
+ models .SendActivateAccountMail (ctx .Context , u )
515
+ ctx .Data ["IsSendRegisterMail" ] = true
516
+ ctx .Data ["Email" ] = u .Email
517
+ ctx .Data ["ActiveCodeLives" ] = base .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale .Language ())
518
+ ctx .HTML (200 , TplActivate )
519
+
520
+ if err := ctx .Cache .Put ("MailResendLimit_" + u .LowerName , u .LowerName , 180 ); err != nil {
521
+ log .Error (4 , "Set cache(MailResendLimit) fail: %v" , err )
522
+ }
523
+ return
524
+ }
525
+
526
+ ctx .Flash .Success (ctx .Tr ("auth.sign_up_successful" ))
527
+ // Complete the signin without logging in again
528
+ handleSignInFull (ctx , u , remember , true )
529
+ }
530
+
500
531
// SignInOAuth handles the OAuth2 login buttons
501
532
func SignInOAuth (ctx * context.Context ) {
502
533
provider := ctx .Params (":provider" )
@@ -800,14 +831,20 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
800
831
ctx .ServerError ("CreateUser" , err )
801
832
}
802
833
834
+ // TODO LoginName should come from form.UserName... shouldn't it?
803
835
u := & models.User {
804
- Name : form .UserName ,
805
- Email : form .Email ,
806
- Passwd : form .Password ,
807
- IsActive : ! setting .Service .RegisterEmailConfirm ,
808
- LoginType : models .LoginOAuth2 ,
809
- LoginSource : loginSource .ID ,
810
- LoginName : gothUser .(goth.User ).UserID ,
836
+ Name : form .UserName ,
837
+ Email : form .Email ,
838
+ Passwd : form .Password ,
839
+ IsActive : ! setting .Service .RegisterEmailConfirm ,
840
+ }
841
+
842
+ // This will link the account in such a way that it cannot be removed
843
+ // TODO why is this different from normal linking?
844
+ if setting .Service .AllowOnlyExternalRegistration {
845
+ u .LoginType = models .LoginOAuth2
846
+ u .LoginSource = loginSource .ID
847
+ u .LoginName = gothUser .(goth.User ).UserID
811
848
}
812
849
813
850
if err := models .CreateUser (u ); err != nil {
@@ -831,32 +868,16 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
831
868
}
832
869
log .Trace ("Account created: %s" , u .Name )
833
870
834
- // Auto-set admin for the only user.
835
- if models .CountUsers () == 1 {
836
- u .IsAdmin = true
837
- u .IsActive = true
838
- u .SetLastLogin ()
839
- if err := models .UpdateUserCols (u , "is_admin" , "is_active" , "last_login_unix" ); err != nil {
840
- ctx .ServerError ("UpdateUser" , err )
871
+ // This will link the account in such a way that it can be removed
872
+ if ! setting .Service .AllowOnlyExternalRegistration {
873
+ err = models .LinkAccountToUser (u , gothUser .(goth.User ))
874
+ if err != nil {
875
+ ctx .ServerError ("UserLinkAccount" , err )
841
876
return
842
877
}
843
878
}
844
879
845
- // Send confirmation email
846
- if setting .Service .RegisterEmailConfirm && u .ID > 1 {
847
- models .SendActivateAccountMail (ctx .Context , u )
848
- ctx .Data ["IsSendRegisterMail" ] = true
849
- ctx .Data ["Email" ] = u .Email
850
- ctx .Data ["ActiveCodeLives" ] = base .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale .Language ())
851
- ctx .HTML (200 , TplActivate )
852
-
853
- if err := ctx .Cache .Put ("MailResendLimit_" + u .LowerName , u .LowerName , 180 ); err != nil {
854
- log .Error (4 , "Set cache(MailResendLimit) fail: %v" , err )
855
- }
856
- return
857
- }
858
-
859
- ctx .Redirect (setting .AppSubURL + "/user/login" )
880
+ handleRegister (ctx , u , form .Remember , true )
860
881
}
861
882
862
883
// SignOut sign out from login status
@@ -964,33 +985,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
964
985
}
965
986
log .Trace ("Account created: %s" , u .Name )
966
987
967
- // Auto-set admin for the only user.
968
- if models .CountUsers () == 1 {
969
- u .IsAdmin = true
970
- u .IsActive = true
971
- u .SetLastLogin ()
972
- if err := models .UpdateUserCols (u , "is_admin" , "is_active" , "last_login_unix" ); err != nil {
973
- ctx .ServerError ("UpdateUser" , err )
974
- return
975
- }
976
- }
977
-
978
- // Send confirmation email, no need for social account.
979
- if setting .Service .RegisterEmailConfirm && u .ID > 1 {
980
- models .SendActivateAccountMail (ctx .Context , u )
981
- ctx .Data ["IsSendRegisterMail" ] = true
982
- ctx .Data ["Email" ] = u .Email
983
- ctx .Data ["ActiveCodeLives" ] = base .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale .Language ())
984
- ctx .HTML (200 , TplActivate )
985
-
986
- if err := ctx .Cache .Put ("MailResendLimit_" + u .LowerName , u .LowerName , 180 ); err != nil {
987
- log .Error (4 , "Set cache(MailResendLimit) fail: %v" , err )
988
- }
989
- return
990
- }
991
-
992
- ctx .Flash .Success (ctx .Tr ("auth.sign_up_successful" ))
993
- handleSignInFull (ctx , u , false , true )
988
+ handleRegister (ctx , u , form .Remember , true )
994
989
}
995
990
996
991
// Activate render activate user page
0 commit comments