Skip to content

Commit

Permalink
Fix 500 error if there is a name conflict when edit authentication so…
Browse files Browse the repository at this point in the history
…urce (#23832) (#23852)

Backport #23832 by @yp05327

Co-authored-by: yp05327 <576951401@qq.com>
  • Loading branch information
GiteaBot and yp05327 committed Mar 31, 2023
1 parent 503af4b commit 16bfe98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 8 additions & 1 deletion models/auth/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,14 @@ func UpdateSource(source *Source) error {
}
}

_, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
has, err := db.GetEngine(db.DefaultContext).Where("name=? AND id!=?", source.Name, source.ID).Exist(new(Source))
if err != nil {
return err
} else if has {
return ErrSourceAlreadyExist{source.Name}
}

_, err = db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,11 @@ func EditAuthSourcePost(ctx *context.Context) {
source.IsActive = form.IsActive
source.IsSyncEnabled = form.IsSyncEnabled
source.Cfg = config
// FIXME: if the name conflicts, it will result in 500: Error 1062: Duplicate entry 'aa' for key 'login_source.UQE_login_source_name'
if err := auth.UpdateSource(source); err != nil {
if oauth2.IsErrOpenIDConnectInitialize(err) {
if auth.IsErrSourceAlreadyExist(err) {
ctx.Data["Err_Name"] = true
ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(auth.ErrSourceAlreadyExist).Name), tplAuthEdit, form)
} else if oauth2.IsErrOpenIDConnectInitialize(err) {
ctx.Flash.Error(err.Error(), true)
ctx.Data["Err_DiscoveryURL"] = true
ctx.HTML(http.StatusOK, tplAuthEdit)
Expand Down

0 comments on commit 16bfe98

Please sign in to comment.