Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 21:26: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) 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 = "" user.Email = ""
err = UpdateUserCols(ctx, user, "email") return UpdateUserCols(ctx, user, "email")
if err != nil {
return err
}
return committer.Commit()
} }
// GetEmailAddresses returns all email addresses belongs to given user. // GetEmailAddresses returns all email addresses belongs to given user.

View file

@ -47,11 +47,11 @@ type CreateOrgOption struct {
// EditOrgOption options for editing an organization // EditOrgOption options for editing an organization
type EditOrgOption struct { type EditOrgOption struct {
FullName string `json:"full_name" binding:"MaxSize(100)"` 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)"` Description string `json:"description" binding:"MaxSize(255)"`
Website string `json:"website" binding:"ValidUrl;MaxSize(255)"` Website string `json:"website" binding:"ValidUrl;MaxSize(255)"`
Location string `json:"location" binding:"MaxSize(50)"` Location string `json:"location" binding:"MaxSize(50)"`
// possible values are `public`, `limited` or `private` // possible values are `public`, `limited` or `private`
// enum: ["public", "limited", "private"] // enum: ["public", "limited", "private"]
Visibility string `json:"visibility" binding:"In(,public,limited,private)"` Visibility string `json:"visibility" binding:"In(,public,limited,private)"`

View file

@ -345,21 +345,23 @@ func Edit(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.EditOrgOption) form := web.GetForm(ctx).(*api.EditOrgOption)
if form.Email == "" { if form.Email != nil {
err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID) if *form.Email == "" {
if err != nil { err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID)
ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err) if err != nil {
return ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err)
} return
ctx.Org.Organization.Email = "" }
} else { ctx.Org.Organization.Email = ""
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil { } else {
if user_model.IsErrEmailInvalid(err) || user_model.IsErrEmailCharIsNotSupported(err) { if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), *form.Email); err != nil {
ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err) if user_model.IsErrEmailInvalid(err) || user_model.IsErrEmailCharIsNotSupported(err) {
} else { ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err)
ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err) } else {
ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err)
}
return
} }
return
} }
} }

View file

@ -234,7 +234,8 @@ func TestAPIOrgChangeEmail(t *testing.T) {
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
t.Run("Invalid", func(t *testing.T) { 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) 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) { 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) 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) { 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) resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)