Skip to content

Commit 3c914ab

Browse files
zeripathjeffliu27
authored andcommitted
Install page - Handle invalid administrator username better (go-gitea#7060)
* Install page - detect invalid admin username before installing * Also fix go-gitea#6954
1 parent d48e0d4 commit 3c914ab

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

options/locale/locale_en-US.ini

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ sqlite_helper = File path for the SQLite3 database.<br>Enter an absolute path if
9494
err_empty_db_path = The SQLite3 database path cannot be empty.
9595
no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account.
9696
err_empty_admin_password = The administrator password cannot be empty.
97+
err_empty_admin_email = The administrator email cannot be empty.
98+
err_admin_name_is_reserved = Administrator Username is invalid, username is reserved
99+
err_admin_name_pattern_not_allowed = Administrator Username is invalid, username is pattern is not allowed
100+
err_admin_name_is_invalid = Administrator Username is invalid
97101

98102
general_title = General Settings
99103
app_name = Site Title

routers/install.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,42 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
215215
return
216216
}
217217

218-
// Check admin password.
219-
if len(form.AdminName) > 0 && len(form.AdminPasswd) == 0 {
220-
ctx.Data["Err_Admin"] = true
221-
ctx.Data["Err_AdminPasswd"] = true
222-
ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_password"), tplInstall, form)
223-
return
224-
}
225-
if form.AdminPasswd != form.AdminConfirmPasswd {
226-
ctx.Data["Err_Admin"] = true
227-
ctx.Data["Err_AdminPasswd"] = true
228-
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplInstall, form)
229-
return
218+
// Check admin user creation
219+
if len(form.AdminName) > 0 {
220+
// Ensure AdminName is valid
221+
if err := models.IsUsableUsername(form.AdminName); err != nil {
222+
ctx.Data["Err_Admin"] = true
223+
ctx.Data["Err_AdminName"] = true
224+
if models.IsErrNameReserved(err) {
225+
ctx.RenderWithErr(ctx.Tr("install.err_admin_name_is_reserved"), tplInstall, form)
226+
return
227+
} else if models.IsErrNamePatternNotAllowed(err) {
228+
ctx.RenderWithErr(ctx.Tr("install.err_admin_name_pattern_not_allowed"), tplInstall, form)
229+
return
230+
}
231+
ctx.RenderWithErr(ctx.Tr("install.err_admin_name_is_invalid"), tplInstall, form)
232+
return
233+
}
234+
// Check Admin email
235+
if len(form.AdminEmail) == 0 {
236+
ctx.Data["Err_Admin"] = true
237+
ctx.Data["Err_AdminEmail"] = true
238+
ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_email"), tplInstall, form)
239+
return
240+
}
241+
// Check admin password.
242+
if len(form.AdminPasswd) == 0 {
243+
ctx.Data["Err_Admin"] = true
244+
ctx.Data["Err_AdminPasswd"] = true
245+
ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_password"), tplInstall, form)
246+
return
247+
}
248+
if form.AdminPasswd != form.AdminConfirmPasswd {
249+
ctx.Data["Err_Admin"] = true
250+
ctx.Data["Err_AdminPasswd"] = true
251+
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplInstall, form)
252+
return
253+
}
230254
}
231255

232256
if form.AppURL[len(form.AppURL)-1] != '/' {

0 commit comments

Comments
 (0)