Skip to content

Commit 326d29d

Browse files
authored
Reliable selection of admin user (#22509)
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.
1 parent f59ce77 commit 326d29d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

models/user/user.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,10 @@ func GetUserByOpenID(uri string) (*User, error) {
12331233
// GetAdminUser returns the first administrator
12341234
func GetAdminUser() (*User, error) {
12351235
var admin User
1236-
has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
1236+
has, err := db.GetEngine(db.DefaultContext).
1237+
Where("is_admin=?", true).
1238+
Asc("id"). // Reliably get the admin with the lowest ID.
1239+
Get(&admin)
12371240
if err != nil {
12381241
return nil, err
12391242
} else if !has {

0 commit comments

Comments
 (0)