Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 13:16:10 +01:00

Do requested changes

This commit is contained in:
JakobDev 2024-10-14 18:42:27 +02:00
parent db29a05b45
commit 351ed1ab8a
No known key found for this signature in database
GPG key ID: 39DEF62C3ED6DC4C
4 changed files with 39 additions and 39 deletions

View file

@ -214,24 +214,8 @@ func DeletePrimaryEmailAddressOfUser(ctx context.Context, uid int64) error {
return fmt.Errorf("%s is not a organization", user.Name)
}
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
defer committer.Close()
_, err = db.GetEngine(ctx).Exec("DELETE FROM email_address WHERE uid = ? AND is_primary = true", uid)
if err != nil {
return err
}
user.Email = ""
err = UpdateUserCols(ctx, user, "email")
if err != nil {
return err
}
return committer.Commit()
return UpdateUserCols(ctx, user, "email")
}
// GetEmailAddresses returns all email addresses belongs to given user.

View file

@ -48,7 +48,7 @@ type CreateOrgOption struct {
// EditOrgOption options for editing an organization
type EditOrgOption struct {
FullName string `json:"full_name" binding:"MaxSize(100)"`
Email string `json:"email" binding:"MaxSize(255)"`
Email *string `json:"email" binding:"MaxSize(255)"`
Description string `json:"description" binding:"MaxSize(255)"`
Website string `json:"website" binding:"ValidUrl;MaxSize(255)"`
Location string `json:"location" binding:"MaxSize(50)"`

View file

@ -345,7 +345,8 @@ func Edit(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.EditOrgOption)
if form.Email == "" {
if form.Email != nil {
if *form.Email == "" {
err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err)
@ -353,7 +354,7 @@ func Edit(ctx *context.APIContext) {
}
ctx.Org.Organization.Email = ""
} else {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), *form.Email); err != nil {
if user_model.IsErrEmailInvalid(err) || user_model.IsErrEmailCharIsNotSupported(err) {
ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err)
} else {
@ -362,6 +363,7 @@ func Edit(ctx *context.APIContext) {
return
}
}
}
opts := &user_service.UpdateOptions{
FullName: optional.Some(form.FullName),

View file

@ -234,7 +234,8 @@ func TestAPIOrgChangeEmail(t *testing.T) {
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
t.Run("Invalid", func(t *testing.T) {
settings := api.EditOrgOption{Email: "invalid"}
newMail := "invalid"
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusUnprocessableEntity)
@ -245,7 +246,19 @@ func TestAPIOrgChangeEmail(t *testing.T) {
})
t.Run("Valid", func(t *testing.T) {
settings := api.EditOrgOption{Email: "example@example.com"}
newMail := "example@example.com"
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)
var org *api.Organization
DecodeJSON(t, resp, &org)
assert.Equal(t, "example@example.com", org.Email)
})
t.Run("NoChange", func(t *testing.T) {
settings := api.EditOrgOption{}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)
@ -256,7 +269,8 @@ func TestAPIOrgChangeEmail(t *testing.T) {
})
t.Run("Empty", func(t *testing.T) {
settings := api.EditOrgOption{Email: ""}
newMail := ""
settings := api.EditOrgOption{Email: &newMail}
resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)