Skip to content

Commit 7de114a

Browse files
lunnywxiaoguang
andauthored
Rework delete org and rename org UI (#34762)
# What's the problem of the original implementation Renaming organization will mix with organization's information change make the operation difficult to keep consistent. This PR created a danger zone like what's repository setting. It also moved organization's `rename` and `delete` operations to this zone. The original updating repository will not change the name any more. This is also a step to extract the `updaterepository` function completely. Before: ![image](https://github.com/user-attachments/assets/d097dfdf-07be-4d79-8fcf-e78822515575) ![image](https://github.com/user-attachments/assets/42ee832c-cb44-41ec-9fe3-92a1c94747d2) After: ![image](https://github.com/user-attachments/assets/f7700ed7-f104-4302-a924-09e118f24be3) ![image](https://github.com/user-attachments/assets/4c49952a-578e-4d14-bd01-4a68c9e02412) ![image](https://github.com/user-attachments/assets/814829d3-00fe-4e87-ae05-625c129170d2) ![image](https://github.com/user-attachments/assets/b067b263-c909-4b48-b23c-73481c32d350) --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 4fc626d commit 7de114a

File tree

17 files changed

+310
-207
lines changed

17 files changed

+310
-207
lines changed

options/locale/locale_en-US.ini

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,7 @@ team_permission_desc = Permission
28112811
team_unit_desc = Allow Access to Repository Sections
28122812
team_unit_disabled = (Disabled)
28132813

2814+
form.name_been_taken = The organisation name "%s" has already been taken.
28142815
form.name_reserved = The organization name "%s" is reserved.
28152816
form.name_pattern_not_allowed = The pattern "%s" is not allowed in an organization name.
28162817
form.create_org_not_allowed = You are not allowed to create an organization.
@@ -2832,15 +2833,28 @@ settings.visibility.private_shortname = Private
28322833

28332834
settings.update_settings = Update Settings
28342835
settings.update_setting_success = Organization settings have been updated.
2835-
settings.change_orgname_prompt = Note: Changing the organization name will also change your organization's URL and free the old name.
2836-
settings.change_orgname_redirect_prompt = The old name will redirect until it is claimed.
2836+
2837+
settings.rename = Rename Organization
2838+
settings.rename_desc = Changing the organization name will also change your organization's URL and free the old name.
2839+
settings.rename_success = Organization %[1]s have been renamed to %[2]s successfully.
2840+
settings.rename_no_change = Organization name is no change.
2841+
settings.rename_new_org_name = New Organization Name
2842+
settings.rename_failed = Rename Organization failed because of internal error
2843+
settings.rename_notices_1 = This operation <strong>CANNOT</strong> be undone.
2844+
settings.rename_notices_2 = The old name will redirect until it is claimed.
2845+
28372846
settings.update_avatar_success = The organization's avatar has been updated.
28382847
settings.delete = Delete Organization
28392848
settings.delete_account = Delete This Organization
28402849
settings.delete_prompt = The organization will be permanently removed. This <strong>CANNOT</strong> be undone!
2850+
settings.name_confirm = Enter the organization name as confirmation:
2851+
settings.delete_notices_1 = This operation <strong>CANNOT</strong> be undone.
2852+
settings.delete_notices_2 = This operation will permanently delete all the <strong>repositories</strong> of <strong>%s</strong> including code, issues, comments, wiki data and collaborator settings.
2853+
settings.delete_notices_3 = This operation will permanently delete all the <strong>packages</strong> of <strong>%s</strong>.
2854+
settings.delete_notices_4 = This operation will permanently delete all the <strong>projects</strong> of <strong>%s</strong>.
28412855
settings.confirm_delete_account = Confirm Deletion
2842-
settings.delete_org_title = Delete Organization
2843-
settings.delete_org_desc = This organization will be deleted permanently. Continue?
2856+
settings.delete_failed = Delete Organization failed because of internal error
2857+
settings.delete_successful = Organization <b>%s</b> has been deleted successfully.
28442858
settings.hooks_desc = Add webhooks which will be triggered for <strong>all repositories</strong> under this organization.
28452859

28462860
settings.labels_desc = Add labels which can be used on issues for <strong>all repositories</strong> under this organization.

routers/web/org/setting.go

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
repo_module "code.gitea.io/gitea/modules/repository"
1919
"code.gitea.io/gitea/modules/setting"
2020
"code.gitea.io/gitea/modules/templates"
21+
"code.gitea.io/gitea/modules/util"
2122
"code.gitea.io/gitea/modules/web"
2223
shared_user "code.gitea.io/gitea/routers/web/shared/user"
2324
user_setting "code.gitea.io/gitea/routers/web/user/setting"
@@ -31,8 +32,6 @@ import (
3132
const (
3233
// tplSettingsOptions template path for render settings
3334
tplSettingsOptions templates.TplName = "org/settings/options"
34-
// tplSettingsDelete template path for render delete repository
35-
tplSettingsDelete templates.TplName = "org/settings/delete"
3635
// tplSettingsHooks template path for render hook settings
3736
tplSettingsHooks templates.TplName = "org/settings/hooks"
3837
// tplSettingsLabels template path for render labels settings
@@ -71,26 +70,6 @@ func SettingsPost(ctx *context.Context) {
7170

7271
org := ctx.Org.Organization
7372

74-
if org.Name != form.Name {
75-
if err := user_service.RenameUser(ctx, org.AsUser(), form.Name); err != nil {
76-
if user_model.IsErrUserAlreadyExist(err) {
77-
ctx.Data["Err_Name"] = true
78-
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form)
79-
} else if db.IsErrNameReserved(err) {
80-
ctx.Data["Err_Name"] = true
81-
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplSettingsOptions, &form)
82-
} else if db.IsErrNamePatternNotAllowed(err) {
83-
ctx.Data["Err_Name"] = true
84-
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
85-
} else {
86-
ctx.ServerError("RenameUser", err)
87-
}
88-
return
89-
}
90-
91-
ctx.Org.OrgLink = setting.AppSubURL + "/org/" + url.PathEscape(org.Name)
92-
}
93-
9473
if form.Email != "" {
9574
if err := user_service.ReplacePrimaryEmailAddress(ctx, org.AsUser(), form.Email); err != nil {
9675
ctx.Data["Err_Email"] = true
@@ -163,42 +142,27 @@ func SettingsDeleteAvatar(ctx *context.Context) {
163142
ctx.JSONRedirect(ctx.Org.OrgLink + "/settings")
164143
}
165144

166-
// SettingsDelete response for deleting an organization
167-
func SettingsDelete(ctx *context.Context) {
168-
ctx.Data["Title"] = ctx.Tr("org.settings")
169-
ctx.Data["PageIsOrgSettings"] = true
170-
ctx.Data["PageIsSettingsDelete"] = true
171-
172-
if ctx.Req.Method == http.MethodPost {
173-
if ctx.Org.Organization.Name != ctx.FormString("org_name") {
174-
ctx.Data["Err_OrgName"] = true
175-
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_org_name"), tplSettingsDelete, nil)
176-
return
177-
}
178-
179-
if err := org_service.DeleteOrganization(ctx, ctx.Org.Organization, false); err != nil {
180-
if repo_model.IsErrUserOwnRepos(err) {
181-
ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
182-
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
183-
} else if packages_model.IsErrUserOwnPackages(err) {
184-
ctx.Flash.Error(ctx.Tr("form.org_still_own_packages"))
185-
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
186-
} else {
187-
ctx.ServerError("DeleteOrganization", err)
188-
}
189-
} else {
190-
log.Trace("Organization deleted: %s", ctx.Org.Organization.Name)
191-
ctx.Redirect(setting.AppSubURL + "/")
192-
}
145+
// SettingsDeleteOrgPost response for deleting an organization
146+
func SettingsDeleteOrgPost(ctx *context.Context) {
147+
if ctx.Org.Organization.Name != ctx.FormString("org_name") {
148+
ctx.JSONError(ctx.Tr("form.enterred_invalid_org_name"))
193149
return
194150
}
195151

196-
if _, err := shared_user.RenderUserOrgHeader(ctx); err != nil {
197-
ctx.ServerError("RenderUserOrgHeader", err)
152+
if err := org_service.DeleteOrganization(ctx, ctx.Org.Organization, false /* no purge */); err != nil {
153+
if repo_model.IsErrUserOwnRepos(err) {
154+
ctx.JSONError(ctx.Tr("form.org_still_own_repo"))
155+
} else if packages_model.IsErrUserOwnPackages(err) {
156+
ctx.JSONError(ctx.Tr("form.org_still_own_packages"))
157+
} else {
158+
log.Error("DeleteOrganization: %v", err)
159+
ctx.JSONError(util.Iif(ctx.Doer.IsAdmin, err.Error(), string(ctx.Tr("org.settings.delete_failed"))))
160+
}
198161
return
199162
}
200163

201-
ctx.HTML(http.StatusOK, tplSettingsDelete)
164+
ctx.Flash.Success(ctx.Tr("org.settings.delete_successful", ctx.Org.Organization.Name))
165+
ctx.JSONRedirect(setting.AppSubURL + "/")
202166
}
203167

204168
// Webhooks render webhook list page
@@ -250,3 +214,40 @@ func Labels(ctx *context.Context) {
250214

251215
ctx.HTML(http.StatusOK, tplSettingsLabels)
252216
}
217+
218+
// SettingsRenamePost response for renaming organization
219+
func SettingsRenamePost(ctx *context.Context) {
220+
form := web.GetForm(ctx).(*forms.RenameOrgForm)
221+
if ctx.HasError() {
222+
ctx.JSONError(ctx.GetErrMsg())
223+
return
224+
}
225+
226+
oldOrgName, newOrgName := ctx.Org.Organization.Name, form.NewOrgName
227+
228+
if form.OrgName != oldOrgName {
229+
ctx.JSONError(ctx.Tr("form.enterred_invalid_org_name"))
230+
return
231+
}
232+
if newOrgName == oldOrgName {
233+
ctx.JSONError(ctx.Tr("org.settings.rename_no_change"))
234+
return
235+
}
236+
237+
if err := user_service.RenameUser(ctx, ctx.Org.Organization.AsUser(), newOrgName); err != nil {
238+
if user_model.IsErrUserAlreadyExist(err) {
239+
ctx.JSONError(ctx.Tr("org.form.name_been_taken", newOrgName))
240+
} else if db.IsErrNameReserved(err) {
241+
ctx.JSONError(ctx.Tr("org.form.name_reserved", newOrgName))
242+
} else if db.IsErrNamePatternNotAllowed(err) {
243+
ctx.JSONError(ctx.Tr("org.form.name_pattern_not_allowed", newOrgName))
244+
} else {
245+
log.Error("RenameOrganization: %v", err)
246+
ctx.JSONError(util.Iif(ctx.Doer.IsAdmin, err.Error(), string(ctx.Tr("org.settings.rename_failed"))))
247+
}
248+
return
249+
}
250+
251+
ctx.Flash.Success(ctx.Tr("org.settings.rename_success", oldOrgName, newOrgName))
252+
ctx.JSONRedirect(setting.AppSubURL + "/org/" + url.PathEscape(newOrgName) + "/settings")
253+
}

routers/web/web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,8 @@ func registerWebRoutes(m *web.Router) {
964964
addSettingsVariablesRoutes()
965965
}, actions.MustEnableActions)
966966

967-
m.Methods("GET,POST", "/delete", org.SettingsDelete)
967+
m.Post("/rename", web.Bind(forms.RenameOrgForm{}), org.SettingsRenamePost)
968+
m.Post("/delete", org.SettingsDeleteOrgPost)
968969

969970
m.Group("/packages", func() {
970971
m.Get("", org.Packages)

services/forms/org.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding
3636

3737
// UpdateOrgSettingForm form for updating organization settings
3838
type UpdateOrgSettingForm struct {
39-
Name string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
4039
FullName string `binding:"MaxSize(100)"`
4140
Email string `binding:"MaxSize(255)"`
4241
Description string `binding:"MaxSize(255)"`
@@ -53,6 +52,11 @@ func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors)
5352
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
5453
}
5554

55+
type RenameOrgForm struct {
56+
OrgName string `binding:"Required"`
57+
NewOrgName string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
58+
}
59+
5660
// ___________
5761
// \__ ___/___ _____ _____
5862
// | |_/ __ \\__ \ / \

templates/org/settings/delete.tmpl

Lines changed: 0 additions & 35 deletions
This file was deleted.

templates/org/settings/navbar.tmpl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,5 @@
4141
</div>
4242
</details>
4343
{{end}}
44-
<a class="{{if .PageIsSettingsDelete}}active {{end}}item" href="{{.OrgLink}}/settings/delete">
45-
{{ctx.Locale.Tr "org.settings.delete"}}
46-
</a>
4744
</div>
4845
</div>

0 commit comments

Comments
 (0)