@@ -827,8 +827,9 @@ func validateUser(u *User) error {
827
827
return ValidateEmail (u .Email )
828
828
}
829
829
830
- func updateUser (ctx context.Context , u * User , changePrimaryEmail bool ) error {
831
- if err := validateUser (u ); err != nil {
830
+ func updateUser (ctx context.Context , u * User , changePrimaryEmail bool , cols ... string ) error {
831
+ err := validateUser (u )
832
+ if err != nil {
832
833
return err
833
834
}
834
835
@@ -860,15 +861,35 @@ func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error {
860
861
}); err != nil {
861
862
return err
862
863
}
864
+ } else { // check if primary email in email_address table
865
+ primaryEmailExist , err := e .Where ("uid=? AND is_primary=?" , u .ID , true ).Exist (& EmailAddress {})
866
+ if err != nil {
867
+ return err
868
+ }
869
+
870
+ if ! primaryEmailExist {
871
+ if _ , err = e .Insert (& EmailAddress {
872
+ Email : u .Email ,
873
+ UID : u .ID ,
874
+ IsActivated : true ,
875
+ IsPrimary : true ,
876
+ }); err != nil {
877
+ return err
878
+ }
879
+ }
863
880
}
864
881
865
- _ , err := e .ID (u .ID ).AllCols ().Update (u )
882
+ if len (cols ) == 0 {
883
+ _ , err = e .ID (u .ID ).AllCols ().Update (u )
884
+ } else {
885
+ _ , err = e .ID (u .ID ).Cols (cols ... ).Update (u )
886
+ }
866
887
return err
867
888
}
868
889
869
890
// UpdateUser updates user's information.
870
- func UpdateUser (u * User , emailChanged bool ) error {
871
- return updateUser (db .DefaultContext , u , emailChanged )
891
+ func UpdateUser (u * User , emailChanged bool , cols ... string ) error {
892
+ return updateUser (db .DefaultContext , u , emailChanged , cols ... )
872
893
}
873
894
874
895
// UpdateUserCols update user according special columns
0 commit comments