From 8c946a7e5280f7e71a61f834c716e7bbe69f5258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 18 Jan 2023 15:06:22 +0100 Subject: [PATCH 1/2] Reliable selection of admin user When importing a repository via `gitea restore-repo`, external users will get remapped to an admin user. This admin user is obtained via `users.GetAdminUser()`, which unfortunately picks a more-or-less random admin to return. This makes it hard to predict which admin user will get assigned. This patch orders the admin by ascending ID before choosing the first one, i.e. it picks the admin with the lowest ID. Even though it would be nicer to have full control over which user is chosen, this at least gives us a predictable result. --- models/user/user.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/models/user/user.go b/models/user/user.go index 825223201b679..92595e899c567 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1233,7 +1233,10 @@ func GetUserByOpenID(uri string) (*User, error) { // GetAdminUser returns the first administrator func GetAdminUser() (*User, error) { var admin User - has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin) + has, err := db.GetEngine(db.DefaultContext). + Where("is_admin=?", true). + OrderBy("id asc"). // Reliably get the admin with the lowest ID. + Get(&admin) if err != nil { return nil, err } else if !has { From fd5e2284fdeb95356a8b4b0f4766674da7669ea7 Mon Sep 17 00:00:00 2001 From: Sybren <122987084+drsybren@users.noreply.github.com> Date: Wed, 18 Jan 2023 17:14:39 +0100 Subject: [PATCH 2/2] Change OrderBy("id asc") to Asc("id") Thanks @jolheiser for the review & the keen eyes ;-) Co-authored-by: John Olheiser --- models/user/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/user/user.go b/models/user/user.go index 92595e899c567..a2c54a4429fd9 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1235,7 +1235,7 @@ func GetAdminUser() (*User, error) { var admin User has, err := db.GetEngine(db.DefaultContext). Where("is_admin=?", true). - OrderBy("id asc"). // Reliably get the admin with the lowest ID. + Asc("id"). // Reliably get the admin with the lowest ID. Get(&admin) if err != nil { return nil, err