Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-26 11:46:09 +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

@ -48,7 +48,7 @@ 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)"`

View file

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

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)