From 030cdd6ae291d8db0132cb8e30f9fd24d3a2e285 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 5 Feb 2024 16:49:19 +0100 Subject: [PATCH] [GITEA] Allow changing the email address before activation (squash) See https://codeberg.org/forgejo/forgejo/pulls/2300 --- models/user/email_address.go | 31 ++----------------------------- models/user/email_address_test.go | 22 ---------------------- routers/web/auth/auth.go | 2 +- services/user/email.go | 27 +++++++++++++++++++++++++++ services/user/email_test.go | 22 ++++++++++++++++++++++ 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/models/user/email_address.go b/models/user/email_address.go index 7cb574de54..cc62620e0f 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -332,7 +332,7 @@ func updateActivation(ctx context.Context, email *EmailAddress, activate bool) e return UpdateUserCols(ctx, user, "rands") } -func makeEmailPrimary(ctx context.Context, user *User, email *EmailAddress) error { +func MakeEmailPrimaryWithUser(ctx context.Context, user *User, email *EmailAddress) error { ctx, committer, err := db.TxContext(ctx) if err != nil { return err @@ -362,33 +362,6 @@ func makeEmailPrimary(ctx context.Context, user *User, email *EmailAddress) erro return committer.Commit() } -// ReplaceInactivePrimaryEmail replaces the primary email of a given user, even if the primary is not yet activated. -func ReplaceInactivePrimaryEmail(ctx context.Context, oldEmail string, email *EmailAddress) error { - user := &User{} - has, err := db.GetEngine(ctx).ID(email.UID).Get(user) - if err != nil { - return err - } else if !has { - return ErrUserNotExist{ - UID: email.UID, - Name: "", - KeyID: 0, - } - } - - err = AddEmailAddress(ctx, email) - if err != nil { - return err - } - - err = makeEmailPrimary(ctx, user, email) - if err != nil { - return err - } - - return DeleteEmailAddress(ctx, &EmailAddress{UID: email.UID, Email: oldEmail}) -} - // MakeEmailPrimary sets primary email address of given user. func MakeEmailPrimary(ctx context.Context, email *EmailAddress) error { has, err := db.GetEngine(ctx).Get(email) @@ -410,7 +383,7 @@ func MakeEmailPrimary(ctx context.Context, email *EmailAddress) error { return ErrUserNotExist{UID: email.UID} } - return makeEmailPrimary(ctx, user, email) + return MakeEmailPrimaryWithUser(ctx, user, email) } // VerifyActiveEmailCode verifies active email code when active account diff --git a/models/user/email_address_test.go b/models/user/email_address_test.go index 0105aec0aa..be1ccea544 100644 --- a/models/user/email_address_test.go +++ b/models/user/email_address_test.go @@ -77,28 +77,6 @@ func TestMakeEmailPrimary(t *testing.T) { assert.Equal(t, "user101@example.com", user.Email) } -func TestReplaceInactivePrimaryEmail(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - email := &user_model.EmailAddress{ - Email: "user9999999@example.com", - UID: 9999999, - } - err := user_model.ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email) - assert.Error(t, err) - assert.True(t, user_model.IsErrUserNotExist(err)) - - email = &user_model.EmailAddress{ - Email: "user201@example.com", - UID: 10, - } - err = user_model.ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email) - assert.NoError(t, err) - - user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10}) - assert.Equal(t, "user201@example.com", user.Email) -} - func TestActivate(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index 04f38a714a..91f631acac 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -715,7 +715,7 @@ func ActivatePost(ctx *context.Context) { ctx.Data["ResendLimited"] = true } else { ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale) - err := user_model.ReplaceInactivePrimaryEmail(ctx, ctx.Doer.Email, &user_model.EmailAddress{ + err := user_service.ReplaceInactivePrimaryEmail(ctx, ctx.Doer.Email, &user_model.EmailAddress{ UID: ctx.Doer.ID, Email: email, }) diff --git a/services/user/email.go b/services/user/email.go index 07e19bc688..12ac1f6ca4 100644 --- a/services/user/email.go +++ b/services/user/email.go @@ -145,6 +145,33 @@ func AddEmailAddresses(ctx context.Context, u *user_model.User, emails []string) return nil } +// ReplaceInactivePrimaryEmail replaces the primary email of a given user, even if the primary is not yet activated. +func ReplaceInactivePrimaryEmail(ctx context.Context, oldEmail string, email *user_model.EmailAddress) error { + user := &user_model.User{} + has, err := db.GetEngine(ctx).ID(email.UID).Get(user) + if err != nil { + return err + } else if !has { + return user_model.ErrUserNotExist{ + UID: email.UID, + Name: "", + KeyID: 0, + } + } + + err = AddEmailAddresses(ctx, user, []string{email.Email}) + if err != nil { + return err + } + + err = user_model.MakeEmailPrimaryWithUser(ctx, user, email) + if err != nil { + return err + } + + return DeleteEmailAddresses(ctx, user, []string{oldEmail}) +} + func DeleteEmailAddresses(ctx context.Context, u *user_model.User, emails []string) error { for _, emailStr := range emails { // Check if address exists diff --git a/services/user/email_test.go b/services/user/email_test.go index 8f419b69f9..66d4821346 100644 --- a/services/user/email_test.go +++ b/services/user/email_test.go @@ -107,6 +107,28 @@ func TestAddEmailAddresses(t *testing.T) { assert.True(t, user_model.IsErrEmailAlreadyUsed(err)) } +func TestReplaceInactivePrimaryEmail(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + email := &user_model.EmailAddress{ + Email: "user9999999@example.com", + UID: 9999999, + } + err := ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email) + assert.Error(t, err) + assert.True(t, user_model.IsErrUserNotExist(err)) + + email = &user_model.EmailAddress{ + Email: "user201@example.com", + UID: 10, + } + err = ReplaceInactivePrimaryEmail(db.DefaultContext, "user10@example.com", email) + assert.NoError(t, err) + + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10}) + assert.Equal(t, "user201@example.com", user.Email) +} + func TestDeleteEmailAddresses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase())