Skip to content

Commit d98c26b

Browse files
drsybrenjolheiser
authored andcommitted
Reliable selection of admin user (go-gitea#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 e902b98 commit d98c26b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

models/user/user.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,10 @@ func GetUserByOpenID(uri string) (*User, error) {
12271227
// GetAdminUser returns the first administrator
12281228
func GetAdminUser() (*User, error) {
12291229
var admin User
1230-
has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
1230+
has, err := db.GetEngine(db.DefaultContext).
1231+
Where("is_admin=?", true).
1232+
Asc("id"). // Reliably get the admin with the lowest ID.
1233+
Get(&admin)
12311234
if err != nil {
12321235
return nil, err
12331236
} else if !has {

0 commit comments

Comments
 (0)