summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorEarl Warren <contact@earl-warren.org>2024-02-05 16:49:19 +0100
committerEarl Warren <contact@earl-warren.org>2024-02-05 16:57:58 +0100
commit030cdd6ae291d8db0132cb8e30f9fd24d3a2e285 (patch)
tree94094644a2739ac9a4536d01747876e1cf4bb952 /services
parent[GITEA] always load outdated comments (diff)
downloadforgejo-030cdd6ae291d8db0132cb8e30f9fd24d3a2e285.tar.xz
forgejo-030cdd6ae291d8db0132cb8e30f9fd24d3a2e285.zip
[GITEA] Allow changing the email address before activation (squash)
See https://codeberg.org/forgejo/forgejo/pulls/2300
Diffstat (limited to 'services')
-rw-r--r--services/user/email.go27
-rw-r--r--services/user/email_test.go22
2 files changed, 49 insertions, 0 deletions
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())