@@ -827,8 +827,9 @@ func validateUser(u *User) error {
827827 return ValidateEmail (u .Email )
828828}
829829
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 {
832833 return err
833834 }
834835
@@ -860,15 +861,34 @@ func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error {
860861 }); err != nil {
861862 return err
862863 }
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+ _ , err = e .Insert (& EmailAddress {
872+ Email : u .Email ,
873+ UID : u .ID ,
874+ IsActivated : true ,
875+ IsPrimary : true ,
876+ })
877+ return err
878+ }
863879 }
864880
865- _ , err := e .ID (u .ID ).AllCols ().Update (u )
881+ if len (cols ) == 0 {
882+ _ , err = e .ID (u .ID ).AllCols ().Update (u )
883+ } else {
884+ _ , err = e .ID (u .ID ).Cols (cols ... ).Update (u )
885+ }
866886 return err
867887}
868888
869889// UpdateUser updates user's information.
870- func UpdateUser (u * User , emailChanged bool ) error {
871- return updateUser (db .DefaultContext , u , emailChanged )
890+ func UpdateUser (u * User , emailChanged bool , cols ... string ) error {
891+ return updateUser (db .DefaultContext , u , emailChanged , cols ... )
872892}
873893
874894// UpdateUserCols update user according special columns
0 commit comments