Skip to content

Commit

Permalink
Reliable selection of admin user (go-gitea#22509)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drsybren authored and jolheiser committed Jan 18, 2023
1 parent e902b98 commit d98c26b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,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).
Asc("id"). // Reliably get the admin with the lowest ID.
Get(&admin)
if err != nil {
return nil, err
} else if !has {
Expand Down

0 comments on commit d98c26b

Please sign in to comment.