[GITEA] Allow changing the email address before activation (squash)
See https://codeberg.org/forgejo/forgejo/pulls/2300
This commit is contained in:
parent
ecfc3cb3f0
commit
030cdd6ae2
|
@ -332,7 +332,7 @@ func updateActivation(ctx context.Context, email *EmailAddress, activate bool) e
|
||||||
return UpdateUserCols(ctx, user, "rands")
|
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)
|
ctx, committer, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -362,33 +362,6 @@ func makeEmailPrimary(ctx context.Context, user *User, email *EmailAddress) erro
|
||||||
return committer.Commit()
|
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.
|
// MakeEmailPrimary sets primary email address of given user.
|
||||||
func MakeEmailPrimary(ctx context.Context, email *EmailAddress) error {
|
func MakeEmailPrimary(ctx context.Context, email *EmailAddress) error {
|
||||||
has, err := db.GetEngine(ctx).Get(email)
|
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 ErrUserNotExist{UID: email.UID}
|
||||||
}
|
}
|
||||||
|
|
||||||
return makeEmailPrimary(ctx, user, email)
|
return MakeEmailPrimaryWithUser(ctx, user, email)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyActiveEmailCode verifies active email code when active account
|
// VerifyActiveEmailCode verifies active email code when active account
|
||||||
|
|
|
@ -77,28 +77,6 @@ func TestMakeEmailPrimary(t *testing.T) {
|
||||||
assert.Equal(t, "user101@example.com", user.Email)
|
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) {
|
func TestActivate(t *testing.T) {
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
|
|
@ -715,7 +715,7 @@ func ActivatePost(ctx *context.Context) {
|
||||||
ctx.Data["ResendLimited"] = true
|
ctx.Data["ResendLimited"] = true
|
||||||
} else {
|
} else {
|
||||||
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale)
|
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,
|
UID: ctx.Doer.ID,
|
||||||
Email: email,
|
Email: email,
|
||||||
})
|
})
|
||||||
|
|
|
@ -145,6 +145,33 @@ func AddEmailAddresses(ctx context.Context, u *user_model.User, emails []string)
|
||||||
return nil
|
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 {
|
func DeleteEmailAddresses(ctx context.Context, u *user_model.User, emails []string) error {
|
||||||
for _, emailStr := range emails {
|
for _, emailStr := range emails {
|
||||||
// Check if address exists
|
// Check if address exists
|
||||||
|
|
|
@ -107,6 +107,28 @@ func TestAddEmailAddresses(t *testing.T) {
|
||||||
assert.True(t, user_model.IsErrEmailAlreadyUsed(err))
|
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) {
|
func TestDeleteEmailAddresses(t *testing.T) {
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue