@@ -7,6 +7,7 @@ package auth
7
7
import (
8
8
"errors"
9
9
"fmt"
10
+ "html/template"
10
11
"net/http"
11
12
"strings"
12
13
@@ -37,12 +38,10 @@ import (
37
38
)
38
39
39
40
const (
40
- // tplSignIn template for sign in page
41
- tplSignIn base.TplName = "user/auth/signin"
42
- // tplSignUp template path for sign up page
43
- tplSignUp base.TplName = "user/auth/signup"
44
- // TplActivate template path for activate user
45
- TplActivate base.TplName = "user/auth/activate"
41
+ tplSignIn base.TplName = "user/auth/signin" // for sign in page
42
+ tplSignUp base.TplName = "user/auth/signup" // for sign up page
43
+ TplActivate base.TplName = "user/auth/activate" // for activate user
44
+ TplActivatePrompt base.TplName = "user/auth/activate_prompt" // for showing a message for user activation
46
45
)
47
46
48
47
// autoSignIn reads cookie and try to auto-login.
@@ -613,71 +612,77 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
613
612
}
614
613
}
615
614
616
- // Send confirmation email
617
- if ! u .IsActive && u .ID > 1 {
618
- if setting .Service .RegisterManualConfirm {
619
- ctx .Data ["ManualActivationOnly" ] = true
620
- ctx .HTML (http .StatusOK , TplActivate )
621
- return false
622
- }
615
+ // for active user or the first (admin) user, we don't need to send confirmation email
616
+ if u .IsActive || u .ID == 1 {
617
+ return true
618
+ }
623
619
624
- mailer .SendActivateAccountMail (ctx .Locale , u )
620
+ if setting .Service .RegisterManualConfirm {
621
+ renderActivationPromptMessage (ctx , ctx .Locale .Tr ("auth.manual_activation_only" ))
622
+ return false
623
+ }
625
624
626
- ctx .Data ["IsSendRegisterMail" ] = true
627
- ctx .Data ["Email" ] = u .Email
628
- ctx .Data ["ActiveCodeLives" ] = timeutil .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale )
629
- ctx .HTML (http .StatusOK , TplActivate )
625
+ sendActivateEmail (ctx , u )
626
+ return false
627
+ }
630
628
631
- if err := ctx .Cache .Put ("MailResendLimit_" + u .LowerName , u .LowerName , 180 ); err != nil {
632
- log .Error ("Set cache(MailResendLimit) fail: %v" , err )
633
- }
634
- return false
629
+ func renderActivationPromptMessage (ctx * context.Context , msg template.HTML ) {
630
+ ctx .Data ["ActivationPromptMessage" ] = msg
631
+ ctx .HTML (http .StatusOK , TplActivatePrompt )
632
+ }
633
+
634
+ func sendActivateEmail (ctx * context.Context , u * user_model.User ) {
635
+ if ctx .Cache .IsExist ("MailResendLimit_" + u .LowerName ) {
636
+ renderActivationPromptMessage (ctx , ctx .Locale .Tr ("auth.resent_limit_prompt" ))
637
+ return
635
638
}
636
639
637
- return true
640
+ if err := ctx .Cache .Put ("MailResendLimit_" + u .LowerName , u .LowerName , 180 ); err != nil {
641
+ log .Error ("Set cache(MailResendLimit) fail: %v" , err )
642
+ renderActivationPromptMessage (ctx , ctx .Locale .Tr ("auth.resent_limit_prompt" ))
643
+ return
644
+ }
645
+
646
+ mailer .SendActivateAccountMail (ctx .Locale , u )
647
+
648
+ activeCodeLives := timeutil .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale )
649
+ msgHTML := ctx .Locale .Tr ("auth.confirmation_mail_sent_prompt" , u .Email , activeCodeLives )
650
+ renderActivationPromptMessage (ctx , msgHTML )
638
651
}
639
652
640
653
// Activate render activate user page
641
654
func Activate (ctx * context.Context ) {
642
655
code := ctx .FormString ("code" )
643
656
644
- if len (code ) == 0 {
645
- ctx .Data ["IsActivatePage" ] = true
646
- if ctx .Doer == nil || ctx .Doer .IsActive {
647
- ctx .NotFound ("invalid user" , nil )
657
+ if code == "" {
658
+ if ctx .Doer == nil {
659
+ ctx .Redirect (setting .AppSubURL + "/user/login" )
660
+ return
661
+ } else if ctx .Doer .IsActive {
662
+ ctx .Redirect (setting .AppSubURL + "/" )
648
663
return
649
664
}
650
- // Resend confirmation email.
651
- if setting .Service .RegisterEmailConfirm {
652
- if ctx .Cache .IsExist ("MailResendLimit_" + ctx .Doer .LowerName ) {
653
- ctx .Data ["ResendLimited" ] = true
654
- } else {
655
- ctx .Data ["ActiveCodeLives" ] = timeutil .MinutesToFriendly (setting .Service .ActiveCodeLives , ctx .Locale )
656
- mailer .SendActivateAccountMail (ctx .Locale , ctx .Doer )
657
665
658
- if err := ctx .Cache .Put ("MailResendLimit_" + ctx .Doer .LowerName , ctx .Doer .LowerName , 180 ); err != nil {
659
- log .Error ("Set cache(MailResendLimit) fail: %v" , err )
660
- }
661
- }
662
- } else {
663
- ctx .Data ["ServiceNotEnabled" ] = true
666
+ if setting .MailService == nil || ! setting .Service .RegisterEmailConfirm {
667
+ renderActivationPromptMessage (ctx , ctx .Tr ("auth.disable_register_mail" ))
668
+ return
664
669
}
665
- ctx .HTML (http .StatusOK , TplActivate )
670
+
671
+ // Resend confirmation email.
672
+ sendActivateEmail (ctx , ctx .Doer )
666
673
return
667
674
}
668
675
669
676
user := user_model .VerifyUserActiveCode (ctx , code )
670
- // if code is wrong
671
- if user == nil {
672
- ctx .Data ["IsCodeInvalid" ] = true
673
- ctx .HTML (http .StatusOK , TplActivate )
677
+ if user == nil { // if code is wrong
678
+ renderActivationPromptMessage (ctx , ctx .Locale .Tr ("auth.invalid_code" ))
674
679
return
675
680
}
676
681
677
682
// if account is local account, verify password
678
683
if user .LoginSource == 0 {
679
- ctx .Data ["Code " ] = code
680
- ctx .Data ["NeedsPassword " ] = true
684
+ ctx .Data ["ActivationCode " ] = code
685
+ ctx .Data ["NeedVerifyLocalPassword " ] = true
681
686
ctx .HTML (http .StatusOK , TplActivate )
682
687
return
683
688
}
@@ -688,30 +693,30 @@ func Activate(ctx *context.Context) {
688
693
// ActivatePost handles account activation with password check
689
694
func ActivatePost (ctx * context.Context ) {
690
695
code := ctx .FormString ("code" )
691
- if len ( code ) == 0 {
696
+ if code == "" || ctx . Doer == nil || ctx . Doer . IsActive {
692
697
ctx .Redirect (setting .AppSubURL + "/user/activate" )
693
698
return
694
699
}
695
700
696
701
user := user_model .VerifyUserActiveCode (ctx , code )
697
- // if code is wrong
698
- if user == nil {
699
- ctx .Data ["IsCodeInvalid" ] = true
700
- ctx .HTML (http .StatusOK , TplActivate )
702
+ if user == nil { // if code is wrong
703
+ renderActivationPromptMessage (ctx , ctx .Locale .Tr ("auth.invalid_code" ))
701
704
return
702
705
}
703
706
704
707
// if account is local account, verify password
705
708
if user .LoginSource == 0 {
706
709
password := ctx .FormString ("password" )
707
- if len ( password ) == 0 {
708
- ctx .Data ["Code " ] = code
709
- ctx .Data ["NeedsPassword " ] = true
710
+ if password == "" {
711
+ ctx .Data ["ActivationCode " ] = code
712
+ ctx .Data ["NeedVerifyLocalPassword " ] = true
710
713
ctx .HTML (http .StatusOK , TplActivate )
711
714
return
712
715
}
713
716
if ! user .ValidatePassword (password ) {
714
- ctx .Data ["IsPasswordInvalid" ] = true
717
+ ctx .Flash .Error (ctx .Locale .Tr ("auth.invalid_password" ), true )
718
+ ctx .Data ["ActivationCode" ] = code
719
+ ctx .Data ["NeedVerifyLocalPassword" ] = true
715
720
ctx .HTML (http .StatusOK , TplActivate )
716
721
return
717
722
}
0 commit comments