Skip to content
4 changes: 2 additions & 2 deletions modules/structs/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ type CreateOrgOption struct {
type EditOrgOption struct {
// The full display name of the organization
FullName string `json:"full_name" binding:"MaxSize(100)"`
// The email address of the organization
Email string `json:"email" binding:"MaxSize(255)"`
// The email address of the organization; set to "" to clear
Email *string `json:"email" binding:"MaxSize(255)"`
// The description of the organization
Description string `json:"description" binding:"MaxSize(255)"`
// The website URL of the organization
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ func Edit(ctx *context.APIContext) {

form := web.GetForm(ctx).(*api.EditOrgOption)

if form.Email != "" {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil {
if form.Email != nil {
if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), *form.Email); err != nil {
Comment thread
bircni marked this conversation as resolved.
Outdated
ctx.APIErrorInternal(err)
return
}
Expand Down
10 changes: 4 additions & 6 deletions routers/web/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ func SettingsPost(ctx *context.Context) {

org := ctx.Org.Organization

if form.Email != "" {
if err := user_service.ReplacePrimaryEmailAddress(ctx, org.AsUser(), form.Email); err != nil {
ctx.Data["Err_Email"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("form.email_invalid"), tplSettingsOptions, &form)
return
}
if err := user_service.ReplacePrimaryEmailAddress(ctx, org.AsUser(), form.Email); err != nil {
ctx.Data["Err_Email"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("form.email_invalid"), tplSettingsOptions, &form)
return
}
Comment thread
wxiaoguang marked this conversation as resolved.
Outdated

opts := &user_service.UpdateOptions{
Expand Down
6 changes: 4 additions & 2 deletions services/user/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ func ReplacePrimaryEmailAddress(ctx context.Context, u *user_model.User, emailSt
return nil
}

if err := user_model.ValidateEmail(emailStr); err != nil {
return err
if emailStr != "" {
if err := user_model.ValidateEmail(emailStr); err != nil {
return err
}
Comment thread
wxiaoguang marked this conversation as resolved.
Outdated
}

return db.WithTx(ctx, func(ctx context.Context) error {
Expand Down
4 changes: 4 additions & 0 deletions services/user/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func TestReplacePrimaryEmailAddress(t *testing.T) {
assert.NoError(t, ReplacePrimaryEmailAddress(t.Context(), org.AsUser(), "primary-org@example.com"))

assert.Equal(t, "primary-org@example.com", org.Email)

assert.NoError(t, ReplacePrimaryEmailAddress(t.Context(), org.AsUser(), ""))

assert.Empty(t, org.Email)
})
}

Expand Down
23 changes: 23 additions & 0 deletions tests/integration/api_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ func TestAPIOrgGeneral(t *testing.T) {
assert.Equal(t, org.Visibility, apiOrg.Visibility)
})

t.Run("OrgEditClearEmail", func(t *testing.T) {
// first set an email
setEmail := "contact@org3.example.com"
req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &api.EditOrgOption{
Email: &setEmail,
Visibility: "public",
}).AddTokenAuth(user1Token)
resp := MakeRequest(t, req, http.StatusOK)
var apiOrg api.Organization
DecodeJSON(t, resp, &apiOrg)
assert.Equal(t, setEmail, apiOrg.Email)

// clear the email: nil = "don't touch", "" = "clear"
clearEmail := ""
req = NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &api.EditOrgOption{
Email: &clearEmail,
Visibility: "public",
}).AddTokenAuth(user1Token)
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiOrg)
assert.Empty(t, apiOrg.Email)
})

t.Run("OrgEditBadVisibility", func(t *testing.T) {
org := api.EditOrgOption{
FullName: "Org3 organization new full name",
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,25 @@ func TestTeamSearch(t *testing.T) {
assert.Len(t, teams, 1) // team permission is "write", so can write "code"
})
}

func TestOrgSettingsClearEmail(t *testing.T) {
defer tests.PrepareTestEnv(t)()

session := loginUser(t, "user2")

req := NewRequestWithValues(t, "POST", "/org/org3/settings", map[string]string{
"name": "org3",
"email": "contact@org3.example.com",
})
session.MakeRequest(t, req, http.StatusSeeOther)
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
assert.Equal(t, "contact@org3.example.com", org.Email)

req = NewRequestWithValues(t, "POST", "/org/org3/settings", map[string]string{
"name": "org3",
"email": "",
})
Comment thread
wxiaoguang marked this conversation as resolved.
session.MakeRequest(t, req, http.StatusSeeOther)
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
assert.Empty(t, org.Email)
}
Loading