Skip to content

Commit 8b64cc3

Browse files
yp05327GiteaBot
authored andcommitted
Fix 500 error if there is a name conflict when edit authentication source (go-gitea#23832)
1 parent 503af4b commit 8b64cc3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Diff for: models/auth/source.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,14 @@ func UpdateSource(source *Source) error {
317317
}
318318
}
319319

320-
_, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
320+
has, err := db.GetEngine(db.DefaultContext).Where("name=? AND id!=?", source.Name, source.ID).Exist(new(Source))
321+
if err != nil {
322+
return err
323+
} else if has {
324+
return ErrSourceAlreadyExist{source.Name}
325+
}
326+
327+
_, err = db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
321328
if err != nil {
322329
return err
323330
}

Diff for: routers/web/admin/auths.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,11 @@ func EditAuthSourcePost(ctx *context.Context) {
426426
source.IsActive = form.IsActive
427427
source.IsSyncEnabled = form.IsSyncEnabled
428428
source.Cfg = config
429-
// FIXME: if the name conflicts, it will result in 500: Error 1062: Duplicate entry 'aa' for key 'login_source.UQE_login_source_name'
430429
if err := auth.UpdateSource(source); err != nil {
431-
if oauth2.IsErrOpenIDConnectInitialize(err) {
430+
if auth.IsErrSourceAlreadyExist(err) {
431+
ctx.Data["Err_Name"] = true
432+
ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(auth.ErrSourceAlreadyExist).Name), tplAuthEdit, form)
433+
} else if oauth2.IsErrOpenIDConnectInitialize(err) {
432434
ctx.Flash.Error(err.Error(), true)
433435
ctx.Data["Err_DiscoveryURL"] = true
434436
ctx.HTML(http.StatusOK, tplAuthEdit)

0 commit comments

Comments
 (0)